Sign in to follow this  
Followers 0
MacScript

Store Control name in array for use

10 posts in this topic

I have looked around but had not luck figuring out how or even if this is possible to do.

I would like to store the control IDs of many of my controls in an array so that I can more effectively change the control text based on the language I want the application to be visible in.

Below is a snipet of my code to give you an idea of what I am referring too

Func ChangeAppLanguages($appLang)
    Global $SetupLangList [9][$AppLangCount] = [ _
        [$hGUIAppSetup, "Setup", "설정"], _
        [$lblAppsetupLang, "Application Language: ", "사용 언어: "], _
        [$btnChangeAvailLang, "Change Available Languages", "가변 언어"], _
        [$grpRdoToolTip, "Tool Tips", "도움말 표시"], _
        [$rdoToolTipEnable, "Enable", "활성화"], _
        [$rdoToolTipDisable, "Disable", "비활성화"], _
        [$btnChangeChatChan, "Change Available Chat Channel", "가변 채팅 채널"], _
        [$btnAppSetupFinished, "Save and Exit", "저장하고 종료"], _
        [$btnAppSetupCancel, "Cancel Changes and Exit", "변경사항 취소하고 종료"]]

    for $x = 0 to 8
        $tmptext = guictrlsetdata($tmSetupLangList][0],$tmSetupLangList][$applang])
    Next

EndFunc

I only provided example with 2 languages and 9 controls, but in theory I need to support 6 languages and 32 controls in the code I am writing. So when they change the applications language setting I then execute a function similar to above if possible. 

However it does not currently work.

The guictrlsetdata returns a 0 at the moment.

Any suggestions would be appreciated.

Share this post


Link to post
Share on other sites



MacScript,

You have a 2D array - so you need to use 2D array addressing syntax:

; Declare the array OUTSIDE the function
Global $SetupLangList[9][$AppLangCount] = [ _
        [$hGUIAppSetup,        "Setup", "??"], _
        [$lblAppsetupLang,     "Application Language: ",        "?? ??: "], _
        [$btnChangeAvailLang,  "Change Available Languages",    "?? ??"], _
        [$grpRdoToolTip,       "Tool Tips",                     "??? ??"], _
        [$rdoToolTipEnable,    "Enable",                        "???"], _
        [$rdoToolTipDisable,   "Disable",                       "????"], _
        [$btnChangeChatChan,   "Change Available Chat Channel", "?? ?? ??"], _
        [$btnAppSetupFinished, "Save and Exit",                 "???? ??"], _
        [$btnAppSetupCancel,   "Cancel Changes and Exit",       "???? ???? ??"]]
; Col    0                      1                                2

Func ChangeAppLanguages($appLang)
    ; Use the array size for the count - then you can change it at will
    For $x = 0 To UBound($SetupLangList) - 1
        ; Now for each row
        ;                                Control = [$i][0] (first column)
        $tmptext = GUICtrlSetData($tmSetupLangList][$i][0], $tmSetupLangList][$i][$appLang])
        ;                                                             Lang = [$i][$applang]  (column for the selected languge)
    Next
EndFunc   ;==>ChangeAppLanguages
All clear? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

.....

All clear? :)

M23

HI Melba23 -

Thanks however that was my bad, when I was trying to put a code snippet together to demonstrate the issue I was having (the reason for snippet, was cause my code is 1600+ lines). So here is a snippet that actually runs but errors out when I try to do the guictrlsetdata. When the combo box changes calls the function to do the guictrlsetdata on all controls but as you can see the array does not seem to actually store the handle of the control inside it.

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>
#include <StaticConstants.au3>

Global $AppLangCount = 4
global $hGUIAppSetup,$lblAppsetupLang, $btnAppSetupFinished
Global $SetupLangList [9][$AppLangCount] = [ _
        [$hGUIAppSetup, "Setup", "설정"], _
        [$lblAppsetupLang, "Application Language: ", "사용 언어: "], _
        [$btnAppSetupFinished, "Save and Exit", "저장하고 종료"]]


Appsetup()

Func ChangeAppLanguages($tmpArray, $appLang)
    msgbox(64,"control id", $tmpArray[1][0])
    for $i = 0 to ubound($tmpArray)
        $tmptext = guictrlsetdata($tmpArray[$i][0],$tmparray[$i][$appLang])
    Next
EndFunc

Func AppSetup() ;-30a-  ; added variable to the function
    Local $iniConfigRead = ""
    Local $popWidth = 300
    Local $popHeight = 200
    Local $ListforCmb, $tmpArray, $tmpSelected
    local $Applang = 2
    $hGUIAppSetup = GUICreate($SetupLangList[0][$AppLang], $popWidth, $popHeight, -1, -1,  BitOR($WS_CAPTION, $WS_POPUPWINDOW))
    $lblAppsetupLang = GUICtrlCreateLabel ( $SetupLangList[1][$AppLang],1,12,110,21,$SS_RIGHT)

    Global $cmbAppLang = GUICtrlCreateCombo("", 115, 10, 70, 21, 2097155)   ;-36a- Changed to global from local
    GUICtrlSetData($cmbAppLang, "English|Korean|Spanish")
    $iniConfigRead = "English"
    $iniConfigRead = _GUICtrlComboBox_FindStringExact($cmbAppLang, $iniConfigRead)
    _GUICtrlComboBox_SetCurSel($cmbAppLang, $iniConfigRead)

    $btnAppSetupFinished = GUICtrlCreateButton($SetupLangList[7][$AppLang],($popWidth /4) - 40 ,$popHeight - 21, 80,21) ;-36a- Changed to global from local
    GUICtrlSetResizing(-1, 802)

    GUISetState(@SW_SHOW, $hGUIAppSetup) ; Makes GUI Visible

    While True
        $tmpLang = GUICtrlRead($cmbAppLang)
        Switch $tmpLang
            Case "English"
                $AppLang = 1
            Case "Korean"
                $AppLang = 2
            Case "Spanish"
                $AppLang = 3
        EndSwitch
        $tmpArray = ""
        $msgAppsetup = GUIGetMsg()
        Select
            Case $msgAppsetup = $cmbAppLang
                ChangeAppLanguages($SetupLangList, $appLang)
            Case $msgAppsetup = $btnAppSetupFinished
                GUIDelete($hGUIAppSetup)
                ExitLoop
            Case Else
                Sleep (50)
        EndSelect
    WEnd
EndFunc ;-30a-

Thanks again in advance for the all help / suggestions provided.

Edited by MacScript

Share this post


Link to post
Share on other sites

MacScript,

I have altered the script significantly - it now works for me: ;)

#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>
#include <StaticConstants.au3>

Global $AppLangCount = 3

; You cannot just name variables here and expect them to be updated - you need to assign the correct values to the array elements
Global $SetupLangList[3][$AppLangCount + 1] = [ _
        [0, "Setup", "11", "99"], _
        [0, "Application Language: ", "22 22: ", "88 88:"], _
        [0, "Save and Exit", "3333 33", "7777 77"]]

AppSetup()

Func AppSetup() ;-30a-  ; added variable to the function
    Local $iniConfigRead = ""
    Local $popWidth = 300
    Local $popHeight = 200
    Local $ListforCmb, $tmpArray, $tmpSelected

    ; And you assign the correct values when you create the GUI and controls - but leave the text blank
    $SetupLangList[0][0] = GUICreate("", $popWidth, $popHeight, -1, -1, BitOR($WS_CAPTION, $WS_POPUPWINDOW))
    $SetupLangList[1][0] = GUICtrlCreateLabel("", 1, 12, 110, 21, $SS_RIGHT)

    Global $cmbAppLang = GUICtrlCreateCombo("", 115, 10, 70, 21, 2097155) ;-36a- Changed to global from local
    GUICtrlSetData($cmbAppLang, "English|Korean|Spanish")
    $iniConfigRead = "English"
    $iniConfigRead = _GUICtrlComboBox_FindStringExact($cmbAppLang, $iniConfigRead)
    _GUICtrlComboBox_SetCurSel($cmbAppLang, $iniConfigRead)

    $SetupLangList[2][0] = GUICtrlCreateButton("", ($popWidth / 4) - 40, $popHeight - 21, 80, 21) ;-36a- Changed to global from local
    GUICtrlSetResizing(-1, 802)

    ; Now set the text
    ChangeAppLanguages(1)

    GUISetState(@SW_SHOW, $SetupLangList[0][0]) ; Makes GUI Visible

    While 1

        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $SetupLangList[2][0]
                GUIDelete($SetupLangList[0][0])
                ExitLoop
            Case $cmbAppLang
                ; There is no need to read the combo on every pass
                Switch GUICtrlRead($cmbAppLang)
                    Case "English"
                        $appLang = 1
                    Case "Korean"
                        $appLang = 2
                    Case "Spanish"
                        $applang = 3
                EndSwitch
                ChangeAppLanguages($appLang)
            ; And no need for a Sleep as GUIGetMsg has a builtin pause
        EndSwitch
    WEnd
EndFunc   ;==>AppSetup

Func ChangeAppLanguages($appLang)
    ; The GUI is not a control so you need this function to change the title
    WinSetTitle($SetupLangList[0][0], "", $SetupLangList[0][$appLang])
    ; The others are controls
    For $i = 1 To UBound($SetupLangList) - 1
        GUICtrlSetData($SetupLangList[$i][0], $SetupLangList[$i][$appLang])
    Next
EndFunc   ;==>ChangeAppLanguage
Please ask if you have any questions about what I have modified or why I did so. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Please ask if you have any questions about what I have modified or why I did so. :)

M23

 

 

Hi M23

Thank you for your help. I was wondering if I was going to have to create each control that way. The only issue I see with this, does not make for easy reading of the code when working on it at all.  No way having ->$SetupLangList[2][0]<- and saying Oh this is X control compared too ->$btnChangeAvailLang<-.  I am wondering if I am just better off putting in the ChangeAppLanguages(  function a line for each and every control instead of using a loop.

I was thinking using an array would make the code cleaner, but having second thoughts.

Any thoughts

Share this post


Link to post
Share on other sites

MacScript,

Use Enums - that allows you to easily identify the different elements. Look for the <<<<<<<<<< lines: ;)

#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>
#include <StaticConstants.au3>

Global Enum $eGUI, $eLabel, $eButton, $eMax ; These are automatically numbered 0-3 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Global $AppLangCount = 3

; You cannot just name variables here and expect them to be updated - you need to assign the correct values to the array elements
; See how easy it becomes to size the array? <<<<<<<<<<<<<<<<<<<<<<<<<<
Global $SetupLangList[$eMax][$AppLangCount + 1] = [ _
        [0, "Setup", "11", "99"], _
        [0, "Application Language: ", "22 22: ", "88 88:"], _
        [0, "Save and Exit", "3333 33", "7777 77"]]

AppSetup()

Func AppSetup() ;-30a-  ; added variable to the function
    Local $iniConfigRead = ""
    Local $popWidth = 300
    Local $popHeight = 200
    Local $ListforCmb, $tmpArray, $tmpSelected

    ; And you assign the correct values when you create the GUI and controls - but leave the text blank
    $SetupLangList[$eGUI][0] = GUICreate("", $popWidth, $popHeight, -1, -1, BitOR($WS_CAPTION, $WS_POPUPWINDOW)) ; Use Enum to identify <<<<<<<<<<<<<<<
    $SetupLangList[$eLabel][0] = GUICtrlCreateLabel("", 1, 12, 110, 21, $SS_RIGHT) ; Use Enum to identify <<<<<<<<<<<<<<<

    Global $cmbAppLang = GUICtrlCreateCombo("", 115, 10, 70, 21, 2097155) ;-36a- Changed to global from local
    GUICtrlSetData($cmbAppLang, "English|Korean|Spanish")
    $iniConfigRead = "English"
    $iniConfigRead = _GUICtrlComboBox_FindStringExact($cmbAppLang, $iniConfigRead)
    _GUICtrlComboBox_SetCurSel($cmbAppLang, $iniConfigRead)

    $SetupLangList[$eButton][0] = GUICtrlCreateButton("", ($popWidth / 4) - 40, $popHeight - 21, 80, 21) ; Use Enum to identify <<<<<<<<<<<<<<<
    GUICtrlSetResizing(-1, 802)

    ; Now set the text
    ChangeAppLanguages(1)

    GUISetState(@SW_SHOW, $SetupLangList[0][0]) ; Makes GUI Visible

    While 1

        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $SetupLangList[2][0]
                GUIDelete($SetupLangList[0][0])
                ExitLoop
            Case $cmbAppLang
                ; There is no need to read the combo on every pass
                Switch GUICtrlRead($cmbAppLang)
                    Case "English"
                        $appLang = 1
                    Case "Korean"
                        $appLang = 2
                    Case "Spanish"
                        $applang = 3
                EndSwitch
                ChangeAppLanguages($appLang)
            ; And no need for a Sleep as GUIGetMsg has a builtin pause
        EndSwitch
    WEnd
EndFunc   ;==>AppSetup

Func ChangeAppLanguages($appLang)
    ; The GUI is not a control so you need this function to change the title
    WinSetTitle($SetupLangList[0][0], "", $SetupLangList[0][$appLang])
    ; The others are controls
    For $i = 1 To UBound($SetupLangList) - 1
        GUICtrlSetData($SetupLangList[$i][0], $SetupLangList[$i][$appLang])
    Next
EndFunc   ;==>ChangeAppLanguage
Easier to understand now? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

M23 -

Yes that does make it clearer then it was. However, if I need to work on the code and only am seeing stuff like $SetupLangList[2][0] .
I will be spending way more time trying to find/work on the code then I would if a control is called $btnSendChat for instance.

The methods you have shown, have provided me with great learning experience. I wish I could find a compromise that does not take away from the readability of the script in comparison to using these coded words for each time I reference a control.

Any additions suggestions would be appreciated and if you have reached the end of the possibilities then thank you very much for the time you have invested helping me out.

Thanks

Share this post


Link to post
Share on other sites

MacScript,

I only changed the first few index instances for Enums - I left the rest of the changes as an exercise for the student. ;)

At present, using Enums is the easiest way to have a "quasi-associative array" - that is one which holds values identified by a name rather then a number. I use this trick all the time - and guinness is developing a pre-processor which will clear them all out before compilation to save space and speed up execution. :)

If you wanted to simplify things a little, you could have 2 arrays - one to hold the identities and another for the data (I have changed all the index values this time):

#include <FileConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiComboBox.au3>
#include <StaticConstants.au3>

Global Enum $eGUI, $eLabel, $eButton, $eMax ; These are automatically numbered 0-3 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Global $AppLangCount = 3

Global $aControls[$eMax]
Global $SetupLangList[$eMax][$AppLangCount] = [ _
        ["Setup", "11", "99"], _
        ["Application Language: ", "22 22: ", "88 88:"], _
        ["Save and Exit", "3333 33", "7777 77"]]

AppSetup()

Func AppSetup() ;-30a-  ; added variable to the function
    Local $iniConfigRead = ""
    Local $popWidth = 300
    Local $popHeight = 200
    Local $ListforCmb, $tmpArray, $tmpSelected

    ; And you assign the correct values when you create the GUI and controls - but leave the text blank
    $aControls[$eGUI] = GUICreate("", $popWidth, $popHeight, -1, -1, BitOR($WS_CAPTION, $WS_POPUPWINDOW)) ; Use Enum to identify <<<<<<<<<<<<<<<
    $aControls[$eLabel] = GUICtrlCreateLabel("", 1, 12, 110, 21, $SS_RIGHT) ; Use Enum to identify <<<<<<<<<<<<<<<

    Global $cmbAppLang = GUICtrlCreateCombo("", 115, 10, 70, 21, 2097155) ;-36a- Changed to global from local
    GUICtrlSetData($cmbAppLang, "English|Korean|Spanish")
    $iniConfigRead = "English"
    $iniConfigRead = _GUICtrlComboBox_FindStringExact($cmbAppLang, $iniConfigRead)
    _GUICtrlComboBox_SetCurSel($cmbAppLang, $iniConfigRead)

    $aControls[$eButton] = GUICtrlCreateButton("", ($popWidth / 4) - 40, $popHeight - 21, 80, 21) ; Use Enum to identify <<<<<<<<<<<<<<<
    GUICtrlSetResizing(-1, 802)

    ; Now set the text
    ChangeAppLanguages(0)

    GUISetState(@SW_SHOW, $aControls[$eGUI]) ; Makes GUI Visible

    While 1

        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $aControls[$eButton]
                GUIDelete($aControls[$eGUI])
                ExitLoop
            Case $cmbAppLang
                ; There is no need to read the combo on every pass
                Switch GUICtrlRead($cmbAppLang) ; Note different values as the array is differently structured
                    Case "English"
                        $appLang = 0
                    Case "Korean"
                        $appLang = 1
                    Case "Spanish"
                        $applang = 2
                EndSwitch
                ChangeAppLanguages($appLang)
            ; And no need for a Sleep as GUIGetMsg has a builtin pause
        EndSwitch
    WEnd
EndFunc   ;==>AppSetup

Func ChangeAppLanguages($appLang)
    ; The GUI is not a control so you need this function to change the title
    WinSetTitle($aControls[$eGUI], "", $SetupLangList[$eGUI][$appLang])
    ; The others are controls
    For $i = 1 To UBound($aControls) - 1
        GUICtrlSetData($aControls[$i], $SetupLangList[$i][$appLang])
    Next
EndFunc   ;==>ChangeAppLanguage
Any better? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Yes that is better and appreciate your time. I am going to try this out and see if it effects my time when working on the code .

Thanks

Mac

Share this post


Link to post
Share on other sites

MacScript,

Delighted I could help. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0