Jump to content

set data to label from array randomly


Recommended Posts

Hi, I have an ini file with 4 sections, each section has 10 items, I want to do this:

1. Create a label for each item with the information read from the ini, if iniread returns 34, I would create 34 labels the maximum I might have is 40 labels)

Label 1 = item 1

label 2 = item 2

Label 3 = item 3

and so on

2. I need to set the information in labels randomly

for example

label 2 = item 2

Label 3 = item 1

Label 1 = item 3

I have this code but I can't figure it out

Quote
Quote
#include <GUIConstantsEx.au3>
Opt("GUIOnEventMode", 1)
Example()

Func Example()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example")
    WinSetState($hGUI, "", @SW_MAXIMIZE)
    Global $idOK = GUICtrlCreateButton("OK", 310, 370, 85, 25)
    GUICtrlSetOnEvent(-1, "SpecialEvents")

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Loop until the user exits.
    While 1

    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example


Func SpecialEvents()
   Select
   Case @gui_ctrlid = $idok
      Local $File = @ScriptDir & "\File.ini"
   Global $Section = IniReadSectionNames($File)
   Global $ItemLabel[5][41]
   Global $Information[5][41]
   Global $NumberOfItems = 0
   Local $Height = 20
   ;$i = section
   ;$a = item
   For $i = 1 To UBound($Section) - 1
      For $a = 1 To 10
         If IniRead($File, $Section[$i], "item " & $a, "") <> "" Then
            $ItemLabel[$i][$a] = GUICtrlCreateLabel("", 10, $Height, @DesktopWidth * 0.04, @DesktopHeight * 0.025)
            $Height += 20
            $information[$i][$a] = IniRead($File, $Section[$i], "item " & $a, "")
            $NumberOfItems += 1
         EndIf
      Next
   Next


For $i = 1 To UBound($Section) - 1
Local $a = 0
   Do
      Local $Random = Random(1, 10, 1)
      If GUICtrlRead($ItemLabel[$i][$random]) = "" Then
         GUICtrlSetData($ItemLabel[$i][$random], $information[$i][$random])
         $a += 1
      EndIf
   Until $a = 10
Next
EndSelect
EndFunc

 

 

 

Edited by luis713
Link to comment
Share on other sites

You could use something like:

#include <Array.au3>
Local $aLabels[1][2]
    _ArrayConcatenate($aLabels, IniReadSection(@ScriptDir & "\Inifile.ini", "Section1"), 1)
    _ArrayConcatenate($aLabels, IniReadSection(@ScriptDir & "\Inifile.ini", "Section2"), 1)
    _ArrayConcatenate($aLabels, IniReadSection(@ScriptDir & "\Inifile.ini", "Section3"), 1)
    _ArrayConcatenate($aLabels, IniReadSection(@ScriptDir & "\Inifile.ini", "Section4"), 1)
$aLabels[0][0] = UBound($aLabels) - 1

_ArrayDisplay($aLabels, ":Before Shuffle:")
    _ArrayShuffle($aLabels, 1, 0, 0)
    _ArrayShuffle($aLabels, 1, 0, 1)
_ArrayDisplay($aLabels, ":After Shuffle:")

Local $iLabels = IniRead(@ScriptDir & "\Inifile.ini", "Configuration", "Label Count", $aLabels[0][0])
For $i = 1 To $iLabels
    GUICtrlCreateLabel($aLabels[$i][1], ...)
Next

 

Link to comment
Share on other sites

18 hours ago, Subz said:

You could use something like:

#include <Array.au3>
Local $aLabels[1][2]
    _ArrayConcatenate($aLabels, IniReadSection(@ScriptDir & "\Inifile.ini", "Section1"), 1)
    _ArrayConcatenate($aLabels, IniReadSection(@ScriptDir & "\Inifile.ini", "Section2"), 1)
    _ArrayConcatenate($aLabels, IniReadSection(@ScriptDir & "\Inifile.ini", "Section3"), 1)
    _ArrayConcatenate($aLabels, IniReadSection(@ScriptDir & "\Inifile.ini", "Section4"), 1)
$aLabels[0][0] = UBound($aLabels) - 1

_ArrayDisplay($aLabels, ":Before Shuffle:")
    _ArrayShuffle($aLabels, 1, 0, 0)
    _ArrayShuffle($aLabels, 1, 0, 1)
_ArrayDisplay($aLabels, ":After Shuffle:")

Local $iLabels = IniRead(@ScriptDir & "\Inifile.ini", "Configuration", "Label Count", $aLabels[0][0])
For $i = 1 To $iLabels
    GUICtrlCreateLabel($aLabels[$i][1], ...)
Next

 

Thank you this is what I was looking foor, just I have another doubt, I was trying to set data from labels using a button but I couldn't I used the same function below but didn't work , could you give me a hand?

For $i = 1 To $iLabels

guictrldete($label[$i][1]

Link to comment
Share on other sites

To set data you use GuiCtrlSetData, what is that guictrldete?

What do you mean by set data? just changing the label data on a particular one?

If that's the case, you could have a context menu and have a function to bring up an input box to write what you want and then it sets what you wrote, into the label.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

The following is an example of how you can shuffle and add/remove labels, just use the slider to add or remove the number of labels you want to be shown.

#include <Array.au3>
#include <GUIConstantsEx.au3>

Opt("GUIResizeMode", 802)
Global $g_sIniFile = @ScriptDir & "\IniFile.ini"
Global $g_aLabels[1][2]
    _ArrayConcatenate($g_aLabels, IniReadSection($g_sIniFile, "Section1"), 1)
    _ArrayConcatenate($g_aLabels, IniReadSection($g_sIniFile, "Section2"), 1)
    _ArrayConcatenate($g_aLabels, IniReadSection($g_sIniFile, "Section3"), 1)
    _ArrayConcatenate($g_aLabels, IniReadSection($g_sIniFile, "Section4"), 1)
    $g_aLabels[0][0] = UBound($g_aLabels) - 1
Global $g_iLabelCount = IniRead($g_sIniFile, "Configuration", "Label Count", $g_aLabels[0][0])

Global $g_aCtrlLabelPos[5] = [10, 10, 100, 20, 25]
Global $g_aCtrlUpdatePos[5] = [10, 10 + ($g_iLabelCount * $g_aCtrlLabelPos[4]), 100, 30, 35]
Global $g_aCtrlSliderPos[5] = [10, 10 + ($g_iLabelCount * $g_aCtrlLabelPos[4]) + $g_aCtrlUpdatePos[4], 100, 20, 25]

Example()

Func Example()
    Local $aCtrlLabel[$g_iLabelCount + 1] = [$g_iLabelCount]
    Local $hGUI = GUICreate("Example", 120, $g_aCtrlLabelPos[1] + ($g_iLabelCount * $g_aCtrlLabelPos[4]) + ($g_aCtrlUpdatePos[4] + $g_aCtrlSliderPos[4]) + $g_aCtrlLabelPos[1])
    For $i = 1 To $g_iLabelCount
        $aCtrlLabel[$i] = GUICtrlCreateLabel($g_aLabels[$i][1], $g_aCtrlLabelPos[0], $g_aCtrlLabelPos[1], $g_aCtrlLabelPos[2], $g_aCtrlLabelPos[3])
            $g_aCtrlLabelPos[1] += 25
    Next
    Local $idUpdate = GUICtrlCreateButton("Shuffle", $g_aCtrlUpdatePos[0], $g_aCtrlUpdatePos[1], $g_aCtrlUpdatePos[2], $g_aCtrlUpdatePos[3])
    Local $idSlider = GUICtrlCreateSlider($g_aCtrlSliderPos[0], $g_aCtrlSliderPos[1], $g_aCtrlSliderPos[2], $g_aCtrlSliderPos[3])
        GUICtrlSetLimit($idSlider, 40, 10)
        GUICtrlSetData($idSlider, $g_iLabelCount)
    GUISetState(@SW_SHOW)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $idUpdate
                _ArrayShuffle($g_aLabels, 1, 0, 1)
                $_iSliderCount = GUICtrlRead($idSlider)
                Switch $_iSliderCount
                    Case $_iSliderCount < $g_iLabelCount
                        For $i = $g_iLabelCount To 1 Step - 1
                            If $i > $_iSliderCount Then
                                $g_iLabelCount -= 1
                                $g_aCtrlLabelPos[1] -= 25
                                GUICtrlDelete($aCtrlLabel[$i])
                                _ArrayDelete($aCtrlLabel, $i)
                            ElseIf $i <= $_iSliderCount Then
                                GUICtrlSetData($aCtrlLabel[$i], $g_aLabels[$i][1])
                            EndIf
                        Next
                    Case $_iSliderCount > $g_iLabelCount
                        ReDim $aCtrlLabel[$_iSliderCount + 1]
                            $aCtrlLabel[0] = $_iSliderCount
                        For $i = 1 To $_iSliderCount
                            If $i > $g_iLabelCount Then
                                $aCtrlLabel[$i] = GUICtrlCreateLabel($g_aLabels[$i][1], $g_aCtrlLabelPos[0], $g_aCtrlLabelPos[1], $g_aCtrlLabelPos[2], $g_aCtrlLabelPos[3])
                                $g_aCtrlLabelPos[1] += 25
                                $g_iLabelCount += 1
                            Elseif $i <= $g_iLabelCount Then
                                GUICtrlSetData($aCtrlLabel[$i], $g_aLabels[$i][1])
                            EndIf
                        Next
                EndSwitch
                GUICtrlSetPos($idUpdate, $g_aCtrlUpdatePos[0], $g_aCtrlLabelPos[1], $g_aCtrlUpdatePos[2], $g_aCtrlUpdatePos[3])
                GUICtrlSetPos($idSlider, $g_aCtrlSliderPos[0], $g_aCtrlLabelPos[1] + $g_aCtrlUpdatePos[4], $g_aCtrlSliderPos[2], $g_aCtrlSliderPos[3])
                WinMove($hGUI, "", Default, Default, Default, $g_aCtrlLabelPos[1] + $g_aCtrlLabelPos[4] + $g_aCtrlUpdatePos[4] + $g_aCtrlSliderPos[4] + 10)
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...