Atrax27 Posted November 28, 2020 Share Posted November 28, 2020 So I'm playing around with GUIs and would like to have alternate text display within the GUI itself, depending on what selection is made in a dropdown. I'd like the text "Red Shoes" to display differently depending on the selection. I.E. Frankie has red shoes, Robert has blue shoes, Jill has purple shoes, and so on. I know the color of each person's shoe, but perhaps this gets into arrays and can be expanded to other items (Robert has blue shoes AND green pants AND a tophat). But arrays are not something that make sense to me at the moment and I am suffering at the hands of the helpfile. I'd love to learn if arrays can help me here but don't know how to start testing easy items. #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> $Form = GUICreate("Neato 5000", 615, 100, 190, 122) $Input = GUICtrlCreateCombo("Frankie", 0, 0, 609, 21) GUICtrlSetData($Input, "Robert|Jill|Jack|Buddy|Holly|Gerald|Charles", "Frankie") $Button = GUICtrlCreateButton("Message", 0, 24, 609, 25) $Label1 = GUICtrlCreateLabel("Red Shoes", 8, 75, 100, 25) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button Example(GUICtrlRead($Input)) EndSwitch WEnd Func Example($text) Msgbox(0,"", "Bye Bye " & GUICtrlRead($Input)) Exit EndFunc I've come across some ways to change this text, but don't know how to implement when it's more than two choices (see below example) GuiCtrlSetData($label2, $stop ? "Present" : "Not Present") Link to comment Share on other sites More sharing options...
MattHiggs Posted November 29, 2020 Share Posted November 29, 2020 What you might want to do is, rather than using an array, just monitor for when the value of the combo box changes, and then modify the label text based on the selection. Something like this should work: expandcollapse popup#include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> $Form = GUICreate("Neato 5000", 615, 100, 190, 122) $Input = GUICtrlCreateCombo("Frankie", 0, 0, 609, 21) GUICtrlSetData($Input, "Robert|Jill|Jack|Buddy|Holly|Gerald|Charles", "Frankie") $Button = GUICtrlCreateButton("Message", 0, 24, 609, 25) $Label1 = GUICtrlCreateLabel("Red Shoes", 8, 75, 100, 25) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Input Switch GuiCtrlRead ( $input ) Case "Frankie" GUICtrlSetData ( $label1, "Label text for Frankie" ) Case "Robert" GUICtrlSetData ( $label1, "Label text for Robert" ) Case "Jill" GUICtrlSetData ( $label1, "Label text for Jill" ) Case "Jack" GUICtrlSetData ( $label1, "Label text for Jack" ) Case "Buddy" GUICtrlSetData ( $label1, "Label text for Buddy" ) Case "Holly" GUICtrlSetData ( $label1, "Label text for Holly" ) Case "Gerald" GUICtrlSetData ( $label1, "Label text for Gerald" ) Case "Charles" GUICtrlSetData ( $label1, "Label text for Charles" ) EndSwitch Case $Button Example(GUICtrlRead($Input)) EndSwitch WEnd Func Example($text) Msgbox(0,"", "Bye Bye " & GUICtrlRead($Input)) Exit EndFunc Atrax27 1 Link to comment Share on other sites More sharing options...
GokAy Posted November 29, 2020 Share Posted November 29, 2020 Hey, I would create an array holding personal info for all possible attributes I wish. $aPeople[x] [y] where $aPeople[$i][0] = name then create the input string for combobox by iterating the array for column 0 $sCombo = "" for $i = 0 to ubound($aPeople)-1 $sCombo &= $sCombo & $aPeople[$i][0] & "|" next $sCombo = stringtrimright($sCombo,1) ; Remove the | at the end of string GUICtrlSetData($Input, $sCombo, $aPeople[0][0]) And when a combobox change occurs I would check for index rather than value and just use it for whatever I would like to do. https://www.autoitscript.com/autoit3/docs/libfunctions/_GUICtrlComboBox_GetCurSel.htm While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Input $index = _GUICtrlComboBox_GetCurSel ( $Input ) ; Read corresponding index of array for whatever attribute ; and do stuff Case $Button Example(GUICtrlRead($Input)) EndSwitch WEnd Link to comment Share on other sites More sharing options...
Atrax27 Posted November 30, 2020 Author Share Posted November 30, 2020 22 hours ago, MattHiggs said: What you might want to do is, rather than using an array, just monitor for when the value of the combo box changes, and then modify the label text based on the selection. Something like this should work: This is fantastic and works a charm. Thank you very much Link to comment Share on other sites More sharing options...
Atrax27 Posted November 30, 2020 Author Share Posted November 30, 2020 19 hours ago, GokAy said: Hey, I would create an array holding personal info for all possible attributes I wish. $aPeople[x] [y] where $aPeople[$i][0] = name then create the input string for combobox by iterating the array for column 0 $sCombo = "" for $i = 0 to ubound($aPeople)-1 $sCombo &= $sCombo & $aPeople[$i][0] & "|" next $sCombo = stringtrimright($sCombo,1) ; Remove the | at the end of string GUICtrlSetData($Input, $sCombo, $aPeople[0][0]) And when a combobox change occurs I would check for index rather than value and just use it for whatever I would like to do. https://www.autoitscript.com/autoit3/docs/libfunctions/_GUICtrlComboBox_GetCurSel.htm While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Input $index = _GUICtrlComboBox_GetCurSel ( $Input ) ; Read corresponding index of array for whatever attribute ; and do stuff Case $Button Example(GUICtrlRead($Input)) EndSwitch WEnd Definitely gonna look into incorporating this method somehow, looks promising. Do you possibly have something like a test "array" au3 that I could play around with? No matter how many times I use the ones in the helpfile I can never seem to glean anything meaningful from them, it's just all so esoteric that I can't make the connection in my mind with what an array does and how it can be used in a way I want to. Take for example the "_ArrayDisplay" helpfile subject example script. It just cycles through a few different visualizations of data but I don't know how to extract that information in a meaningful way (i.e. Send("Array_cell_C3_from_2D_array")) Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted November 30, 2020 Share Posted November 30, 2020 (edited) @Atrax27 If you want to know what are arrays and how to use them, then take a look at this topic Credits: @TheDcoder Edited November 30, 2020 by FrancescoDiMuro TheDcoder 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Dan_555 Posted November 30, 2020 Share Posted November 30, 2020 here is an explanation video of autoit arrays: TheDcoder 1 Some of my script sourcecode Link to comment Share on other sites More sharing options...
TheDcoder Posted November 30, 2020 Share Posted November 30, 2020 @FrancescoDiMuro Thanks for mentioning my topic, I want to point out that there might be several inaccurate statements in there, especially the section about multi-dimensional arrays. Though all of the essentials should be correct. It has been a few years since I looked back at that topic, I couldn't find the time to revisit it. @Dan_555 You've linked to a great video series, it is very useful for people who aren't familiar with programming... it is the same video series I watched to learn AutoIt, which is kind of the first programming language that I learned . That being said, I do recall a few inaccurate explanations for some of the things, but nothing too harmful, stuff that you will realize as you get more experience. EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
Dan_555 Posted November 30, 2020 Share Posted November 30, 2020 13 hours ago, Atrax27 said: Do you possibly have something like a test "array" au3 that I could play around with? You can play around with this script (p.s. Spoiler Alert ! ) : expandcollapse popup#include <GuiComboBox.au3> #include <ButtonConstants.au3> #include <ComboConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Spoiler Alert !", 309, 58, 192, 124) $Radio1 = GUICtrlCreateRadio("Sex", 201, 1, 103, 23) $Radio2 = GUICtrlCreateRadio("Super Hero", 201, 33, 103, 23) $Combo1 = GUICtrlCreateCombo("", 7, 2, 167, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL,$CBS_DROPDOWNLIST)) $Label1 = GUICtrlCreateLabel("", 8, 38, 164, 15) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Dim $array[4][3] ;Define an array ;Add data to the array $array[0][0]="Bruce Wayne" $array[1][0]="Peter Parker" $array[2][0]="Clark Kent" $array[3][0]="Diana Prince" $array[0][1]="Batman" $array[1][1]="Spiderman" $array[2][1]="Superman" $array[3][1]="Wonder Woman" $array[0][2]="Male" $array[1][2]="Male" $array[2][2]="Male" $array[3][2]="Female" for $x=0 to 3 _GUICtrlComboBox_AddString($Combo1, $array[$x][0]) ;Add names to the combo box Next _GUICtrlComboBox_SetCurSel($Combo1,0) ;Select 1st combobox item GUICtrlSetState ($Radio1, $gui_checked) ;Check the 1st radio button GUICtrlSetData ($Label1, $array[_GUICtrlComboBox_GetCurSel ( $Combo1)][2]) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Radio1 GUICtrlSetData ($Label1, $array[_GUICtrlComboBox_GetCurSel ( $Combo1)][2]) Case $Radio2 GUICtrlSetData ($Label1, $array[_GUICtrlComboBox_GetCurSel ( $Combo1)][1]) Case $Combo1 $x=1+_IsChecked($Radio1) GUICtrlSetData ($Label1, $array[_GUICtrlComboBox_GetCurSel ( $Combo1)][$x]) EndSwitch WEnd Func _IsChecked($idControlID) ;Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED ;Returns true or false (oneliner) ;The lines below convert true and false to numbers - 1 and 0 local $x=BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED If $x = True Then Return 1 Return 0 EndFunc ;==>_IsChecked Some of my script sourcecode Link to comment Share on other sites More sharing options...
GokAy Posted November 30, 2020 Share Posted November 30, 2020 Hey, Another script you can play around with. Reads stuff from an ini file. Some text may be hard to read depending on the color. It's not complete, I will leave that up to you Things you can add: - Save to ini - Change attributes array with a button - Other stuff you want to practise Put the ini file at script dir. Click on "Randomize" and select a person. expandcollapse popup#NoTrayIcon #include <Array.au3> #include <File.au3> #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GuiComboBox.au3> AutoItSetOption("MustDeclareVars",1) AutoItSetOption("GUIOnEventMode", 1) Global $g_aPeople Global $g_aItems Global $g_aColors Global $g_aAttributes[1][1] Global $g_id_combo_People Global $g_id_combo_Color Global $g_id_combo_Item Global $g_id_combo_Location Global $g_id_label_Result Global $g_id_ColorBox Global $g_a_id_label_Location[1] GUI_Window() Func GUI_Window() ; Create App Window Local $hAppWindow = GUICreate("Neato 5000", 500, 500, 190, 122) GUISetOnEvent($GUI_EVENT_CLOSE, "App_Close") ; Create Dummy Button to intercept Return key press GUICtrlCreateButton("",0,0,0,0,-1,-1) ; Create CLOSE Button Local $iBtn_Close = GUICtrlCreateButton("CLOSE",400,450,80,30,-1,-1) GUICtrlSetFont(-1, 14, 400, 0, "") GUICtrlSetOnEvent($iBtn_Close, "App_Close") ; Create People ComboBox $g_id_combo_People = GUICtrlCreateCombo("",10,10,150,25,-1,-1) GUICtrlSetFont(-1, 14, 400, 0, "") GUICtrlSetOnEvent(-1, "Person_Change") ; Create Location ComboBox $g_id_combo_Location = GUICtrlCreateCombo("",10,50,150,25,-1,-1) GUICtrlSetFont(-1, 14, 400, 0, "") GUICtrlSetOnEvent(-1, "Location_Change") ; Create Item ComboBox $g_id_combo_Item = GUICtrlCreateCombo("",200,50,150,25,-1,-1) GUICtrlSetFont(-1, 14, 400, 0, "") GUICtrlSetOnEvent(-1, "Item_Change") ; Create Color ComboBox $g_id_combo_Color = GUICtrlCreateCombo("",10,90,150,25,-1,-1) GUICtrlSetFont(-1, 14, 400, 0, "") GUICtrlSetOnEvent(-1, "Color_Change") ; Create CLOSE Button GUICtrlCreateButton("RANDOMIZE",200,90,150,30,-1,-1) GUICtrlSetFont(-1, 14, 400, 0, "") GUICtrlSetOnEvent(-1, "Items_Randomize") ; Create Color Graphic $g_id_ColorBox = GUICtrlCreateGraphic(10, 130, 50, 25) ; Read ini fle Local $sIniFile = @ScriptDir & "\Attributes.ini" _ReadIni($sIniFile) ; Populate People Combo _GUICtrlComboBox_ResetContent($g_id_combo_People) for $i=0 to ubound($g_aPeople)-1 _GUICtrlComboBox_AddString($g_id_combo_People, $g_aPeople[$i][0]) ;Add names to the combo box Next _GUICtrlComboBox_SetCurSel($g_id_combo_People,0) ;Select 1st combobox item ; Populate Location Combo _GUICtrlComboBox_ResetContent($g_id_combo_Location) for $i=0 to ubound($g_aItems)-1 _GUICtrlComboBox_AddString($g_id_combo_Location, $g_aItems[$i][0]) ;Add names to the combo box Next _GUICtrlComboBox_SetCurSel($g_id_combo_Location,0) ;Select 1st combobox item ; Populate Color Combo _GUICtrlComboBox_ResetContent($g_id_combo_Color) for $i=0 to ubound($g_aColors)-1 _GUICtrlComboBox_AddString($g_id_combo_Color, $g_aColors[$i][0]) ;Add names to the combo box Next _GUICtrlComboBox_SetCurSel($g_id_combo_Color,0) ;Select 1st combobox item ; ReDim Attributes array and populate Local $i_rowsAttributes = ubound($g_aPeople) Local $i_colsAttributes = 1 + (ubound($g_aItems)-1) * 2 redim $g_aAttributes[$i_rowsAttributes][$i_colsAttributes] for $i = 0 to $i_rowsAttributes - 1 $g_aAttributes[$i][0] = $g_aPeople[$i][0] for $j = 1 to ubound($g_aItems)- 1 Local $sValue = IniRead($sIniFile,$g_aPeople[$i][0],$g_aItems[$j][0],"") Local $aValueSplit = StringSplit($sValue,"|",0) if $i = 0 Then $g_aAttributes[$i][1 + ($j-1) * 2] = $g_aItems[$j][0] $g_aAttributes[$i][2 + ($j-1) * 2] = $g_aColors[0][0] Else $g_aAttributes[$i][1 + ($j-1) * 2] = $aValueSplit[1] $g_aAttributes[$i][2 + ($j-1) * 2] = $aValueSplit[2] EndIf Next Next ; Create Labels - can be created dynamically redim $g_a_id_label_Location[ubound($g_aItems)] ; array holding label id's for $i = 1 to ubound($g_aItems) - 1 GUICtrlCreateLabel($g_aItems[$i][0] & ":", 10, 170 + ($i-1) * 40, 100, 25) GUICtrlSetFont(-1,14,0,"") $g_a_id_label_Location[$i] = GUICtrlCreateLabel("", 130, 170 + ($i-1) * 40, 200, 25) GUICtrlSetFont(-1,14,0,"") Next ; Display the App Window GUISetState(@SW_SHOW,$hAppWindow) ; MessageLoop While 1 Sleep(100) WEnd EndFunc Func Show_Details() Local $iPeople = _GUICtrlComboBox_GetCurSel ($g_id_combo_People) if $iPeople = 0 then Return for $i = 1 to ubound($g_aItems) - 1 GUICtrlSetData($g_a_id_label_Location[$i],$g_aAttributes[$iPeople][1 + ($i-1) * 2]) GUICtrlSetColor($g_a_id_label_Location[$i],$g_aAttributes[$iPeople][2 + ($i-1) * 2]) next ;~ Local $iItem = _GUICtrlComboBox_GetCurSel ($g_id_combo_Item) ;~ Local $index = _GUICtrlComboBox_GetCurSel ($g_id_combo_Color) EndFunc func Items_Randomize() ; for each person for $i = 1 to ubound($g_aAttributes) - 1 ; for each location for $j = 1 to ubound($g_aItems) - 1 ; Randomize Item Local $sItem = "" while $sItem = "" Local $index = Random(1, ubound($g_aItems,2)-1,1) $sItem = $g_aItems[$j][$index] wend ; Randomize Color Local $sColor = "" if $sItem = "Nothing" then $sColor = $GUI_BKCOLOR_TRANSPARENT Else $index = Random(2, ubound($g_aColors)-1,1) $sColor = "0x" & _RGB2HEX($g_aColors[$index][1]) EndIf $g_aAttributes[$i][1 + ($j-1) * 2] = $sItem $g_aAttributes[$i][2 + ($j-1) * 2] = $sColor Next Next _ArrayDisplay($g_aAttributes,"Attributes Table") EndFunc ;// Converter: RGB To Hexadecimal format (parameters "Red, Green, Blue" (0-255) Func _RGB2HEX($_rgb) Local $rgbComp = StringSplit( StringStripWS($_rgb, 8), ',') Return Hex( BitAND($rgbComp[1], 255), 2) & Hex( BitAND($rgbComp[2], 255), 2) & Hex( BitAND($rgbComp[3], 255), 2) EndFunc Func Person_Change() Local $index = _GUICtrlComboBox_GetCurSel ($g_id_combo_People) ConsoleWrite("Person Changed: Index(" & $index & ") - " & GUICtrlRead($g_id_combo_People) & @crlf) Show_Details() EndFunc Func Item_Change() Local $index = _GUICtrlComboBox_GetCurSel ($g_id_combo_Item) ConsoleWrite("Item Changed: Index(" & $index & ") - " & GUICtrlRead($g_id_combo_Item) & @crlf) EndFunc Func Color_Change() Local $index = _GUICtrlComboBox_GetCurSel ($g_id_combo_Color) ConsoleWrite("Color Changed: Index(" & $index & ") - " & GUICtrlRead($g_id_combo_Color) & " - " & $g_aColors[$index][1] & @crlf) Local $Color = ($index=0) ? ($GUI_BKCOLOR_TRANSPARENT) : ("0x" & _RGB2HEX($g_aColors[$index][1])) GUICtrlSetBkColor($g_id_ColorBox, $Color) EndFunc Func Location_Change() Local $index = _GUICtrlComboBox_GetCurSel ($g_id_combo_Location) ConsoleWrite("Location Changed: Index(" & $index & ") - " & GUICtrlRead($g_id_combo_Location) & @crlf) ; Populate Item Combo _GUICtrlComboBox_ResetContent($g_id_combo_Item) for $i=1 to ubound($g_aItems) - 1 if not $g_aItems[$index][$i] = "" then _GUICtrlComboBox_AddString($g_id_combo_Item, $g_aItems[$index][$i]) ;Add names to the combo box Next _GUICtrlComboBox_SetCurSel($g_id_combo_Item,0) ;Select 1st combobox item EndFunc Func _ReadIni($sIniFile) If not FileExists($sIniFile) Then ConsoleWrite("Can't find Ini File!" & @CRLF) Exit EndIf Local $aTemp $g_aPeople = _ReadIniSection($sIniFile,"People") $g_aItems = _ReadIniSection($sIniFile,"Items") $g_aColors = _ReadIniSection($sIniFile,"Colors") EndFunc Func _ReadIniSection($sIniFile, $sSection) Local $aTemp = IniReadSection($sIniFile,$sSection) If @error then ConsoleWrite("Error opening section: " & $sSection & " - " & @error & @CRLF) ; Find out max columns Local $i_columnsSection = 1 for $i = 1 to ubound($aTemp)-1 Local $sValue = $aTemp[$i][1] StringReplace($sValue,"|","") if @extended + 1 > $i_columnsSection then $i_columnsSection = @extended + 1 next ; Find out rows Local $i_rowsSection = ubound($aTemp)-1 ; Declare Return array Local $aReturn[$i_rowsSection][$i_columnsSection] ; Populate Return array for $i = 0 to $i_rowsSection-1 $sValue = $aTemp[$i+1][1] Local $aValueSplit = StringSplit($sValue,"|",0) StringReplace($sValue,"|","") for $j = 1 to @extended + 1 $aReturn[$i][$j-1] = $aValueSplit[$j] Next next Return $aReturn EndFunc Func _SaveIni($sIniPath) EndFunc func App_Close() GUIDelete(@GUI_WinHandle) Exit EndFunc test_array-combobox.au3 Attributes.ini Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now