Jump to content

Call a Func via a Var from a Dictionary


Recommended Posts

Hello,

I am very new to all of this, but I will cut to the chase.

I have a dropdown box, and when different items are selected I want a different function to be called depending on what was selected. I want to avoid "IF" statements because that can become very cumbersome later. Here's the script

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $comboread, $var1 = 0

$dict = ObjCreate("Scripting.Dictionary")       ;make dictionary and object (necessary for some reason)

GUICreate ("Test", 300, 300)
$button1 = GUICtrlCreateButton ("Close", 225, 250, 75, 50) ;Close button
$edit1 = GUICtrlCreateEdit("Select something", 0, 100, 300, 100, 0x0800) ;the text area i want to change
$combobox1 = GUICtrlCreateCombo ("Crawl", 50, 50, 200, 100, 0x0003) ;Unchangeable dropdown box!!!!!!
GUICTRLSetData($combobox1, "Walk|Run")          ; add walk and run to combobox

$dict.Add ("Crawl", Crawl())
$dict.Add ("Walk", Walk())                      ;add corresponding Func's to dictionary
$dict.Add ("Run", Running())

GUISetState ()
While 1
    $msg = GUIGetMsg()
    IF $msg = $GUI_EVENT_CLOSE Then ExitLoop    ;close if X button pressed

    IF $msg = $button1 Then ExitLoop            ;close if "Close" button pressed

    IF $msg = $combobox1 Then                   ;if drowpdown pressed
        $comboread = GuiCTrlRead ($combobox1)   ;read what was selected in the dropdown
        Call($dict ($comboread))            ;run the func according to what was pressed
        GUICtrlSetdata ($edit1, $var1)          ;place the new/updated var into the edit box
    EndIf
    
WEnd


;My three functions made just to change the variable $var1 to the desired text
Func Running()
    $var1 = "Running is a lot of fun however"
EndFunc

Func Crawl()
    $var1 = "To walk you must learn to crawl"
EndFunc

Func Walk()
    $var1 = "Walking is faster than crawling but slower than running"
EndFunc

The desired outcome would be to have $edit1 change according to which dropdown was selected. I seem to have something wrong along line27 or in the way that I am defining things. Please help! I have tried looking all over the place, but right now, I don't even know what I should be looking for anymore. Even that sort of direction would be nice.

Thank you for any help you can provide.

Link to comment
Share on other sites

try this one, by the way you can delete this

$dict = ObjCreate("Scripting.Dictionary")

$dict.Add ("Crawl", Crawl())

$dict.Add ("Walk", Walk())

$dict.Add ("Run", Running())

Call($dict ($comboread))

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $comboread, $var1 = 0

$dict = ObjCreate("Scripting.Dictionary")       ;make dictionary and object (necessary for some reason)

GUICreate ("Test", 300, 300)
$button1 = GUICtrlCreateButton ("Close", 225, 250, 75, 50) ;Close button
$edit1 = GUICtrlCreateEdit("Select something", 0, 100, 300, 100, 0x0800) ;the text area i want to change
$combobox1 = GUICtrlCreateCombo ("Crawl", 50, 50, 200, 100, 0x0003) ;Unchangeable dropdown box!!!!!!
GUICTRLSetData($combobox1, "Walk|Run")          ; add walk and run to combobox

$dict.Add ("Crawl", Crawl())
$dict.Add ("Walk", Walk())                      ;add corresponding Func's to dictionary
$dict.Add ("Run", Running())

GUISetState ()
While 1
    $msg = GUIGetMsg()
    IF $msg = $GUI_EVENT_CLOSE Then ExitLoop    ;close if X button pressed

    IF $msg = $button1 Then ExitLoop            ;close if "Close" button pressed
    $check=GUICtrlRead ($combobox1)
    ;
    ;
    If $check = "Crawl" Then Crawl()
    If $check = "Walk" Then Walk()
    If $check = "Run" Then Running()
    ;
    ;
    IF $msg = $combobox1 Then                   ;if drowpdown pressed
        $comboread = GuiCTrlRead ($combobox1)   ;read what was selected in the dropdown
        Call($dict ($comboread))            ;run the func according to what was pressed
        GUICtrlSetdata ($edit1, $var1)          ;place the new/updated var into the edit box
    EndIf

WEnd


;My three functions made just to change the variable $var1 to the desired text
Func Running()
    $var1 = "Running is a lot of fun however"
EndFunc

Func Crawl()
    $var1 = "To walk you must learn to crawl"
EndFunc

Func Walk()
    $var1 = "Walking is faster than crawling but slower than running"
EndFunc
Link to comment
Share on other sites

Thank you for your reply DCCD, but I was really looking to avoid "IF" statements to call the functions. The reason being that if I had 50 or 100 things in my Combobox, then I would have to write 50-100 IF Statements, whereas if I can get it to call from a variable, then I only need a few lines. That is why I had the $Dict variable, I wanted trying to see if something like that would work.

I hope that makes sense, and thank you again.

Edited by RegularGuy
Link to comment
Share on other sites

Name your functions the same as the entries in the combo.

This is one of those few occasions where Call() as suggested above may be the best way to go.

Example for usage in a Msg loop

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case -3
            Exit
        Case $MyCombo
            Call(GUICtrlRead($Msg))
    EndSwitch
WEnd

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Name your functions the same as the entries in the combo.

This is one of those few occasions where Call() as suggested above may be the best way to go.

Example for usage in a Msg loop

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case -3
            Exit
        Case $MyCombo
            Call(GUICtrlRead($Msg))
    EndSwitch
WEnd

Thanks, That's exactly what I needed, that answered my question/solved my problem. Edited by RegularGuy
Link to comment
Share on other sites

Name your functions the same as the entries in the combo.

This is one of those few occasions where Call() as suggested above may be the best way to go.

Example for usage in a Msg loop

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case -3
            Exit
        Case $MyCombo
            Call(GUICtrlRead($Msg))
    EndSwitch
WEnd

Best way
Link to comment
Share on other sites

This is how, with the help provided, I got it working. For those interested

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $var1 = 0

GUICreate ("Test", 300, 300)
$button1 = GUICtrlCreateButton ("Close", 225, 250, 75, 50)          ;Close button
$combobox1 = GUICtrlCreateCombo ("Crawl", 50, 50, 200, 100, 0x0003) ;Unchangeagle dropdown box!!!!!!
$edit1 = GUICtrlCreateEdit("Select something", 0, 100, 300, 100, 0x0800) ;the text area i want to change
GUICTRLSetData($combobox1, "Walk|Running")                          ; add walk and run to combobox

GUISetState ()
While 1
    $msg = GUIGetMsg()
    Switch $Msg
        Case -3
            Exit
        Case $button1
            Exit
        Case $combobox1
            Call(GUICtrlRead($msg))
            GuiCtrlSetData($edit1, $var1)
    EndSwitch
    
WEnd


Func Running()
    $var1 = "Running is a lot of fun however"
EndFunc

Func Crawl()
    $var1 = "To walk you must learn to crawl"
EndFunc

Func Walk()
    $var1 = "Walking is faster than crawling but slower than running"
EndFunc

;My three functions made just to change the variable $var1 to the desired text

Thanks one more time to DCCD & GEOSoft for your help.

Edited by RegularGuy
Link to comment
Share on other sites

Glad it worked out. We generally don't recommend the use of Call() but there are times when it is the better method.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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...