Queener

Use variables from another function

4 posts in this topic

#1 ·  Posted (edited)

I'm working with multiple gui. So say I create 2 gui. The Primary GUI has a button and inputbox. Child GUI has a button($btest) and input($input) as well... When pressing ($btest) in Child GUI, it takes me to a function call Test. Inside Test function, I want it to check what's written in ($input) in child GUI. But Test function does not know what that is because it's outside of Child GUI function. Is there a way I can pass both the button and input variable to the Test function?

 

Use this one for example:

Func Example()

$hChild = GUICreate("Adding",372,284,-1,-1,-1,-1)
$InputHost = GUICtrlCreateInput("",8,131,357,28,-1,512)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
GUICtrlCreateLabel("Computer Hostname:",8,109,163,20,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
GUICtrlSetBkColor(-1,"-2")
GUICtrlCreateLabel("ID:",8,179,163,20,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
GUICtrlSetBkColor(-1,"-2")
$inputid = GUICtrlCreateInput("",8,202,357,28,-1,512)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
$badd = GUICtrlCreateButton("A D D",8,242,100,36,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
$inputsearch2 = GUICtrlCreateInput("aa",8,31,240,28,-1,512)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
GUICtrlCreateLabel("Search By Hostname:",8,7,163,20,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
GUICtrlSetBkColor(-1,"-2")
$bsearch2 = GUICtrlCreateButton("S E A R C H",258,31,100,28,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
$bless = GUICtrlCreateButton("<",8,68,32,30,-1,-1)
$bgreater = GUICtrlCreateButton(">",216,68,32,30,-1,-1)
$bdelete = GUICtrlCreateButton("R E M O V E",132,242,114,36,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
$exit = GUICtrlCreateButton("E X I T",265,242,100,36,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                GUIDelete($hChild)
                ExitLoop
            Case $exit
                GUIDelete($hChild)
                Return
            Case $badd
                Addrecords()
        EndSwitch
    WEnd

EndFunc

Func Addrecords() ; This is where it doesn't know what $inputsearch2 is because $inputsearch2 doesn't exist in this function. Only exist in Example Function
    $text11 = GUICtrlRead($inputsearch2)
    
    MsgBox(0, "TEST", $text11)
EndFunc
Edited by asianqueen

Msgbox(0, "Hate", "Just hate it when I post a question and find my own answer after a couple tries. But if I don't post the question, I can't seem to resolve it at all.")

Share this post


Link to post
Share on other sites



use global $inputsearch2

global $inputsearch2
Func Example()

$hChild = GUICreate("Adding",372,284,-1,-1,-1,-1)
$InputHost = GUICtrlCreateInput("",8,131,357,28,-1,512)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
GUICtrlCreateLabel("Computer Hostname:",8,109,163,20,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
GUICtrlSetBkColor(-1,"-2")
GUICtrlCreateLabel("ID:",8,179,163,20,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
GUICtrlSetBkColor(-1,"-2")
$inputid = GUICtrlCreateInput("",8,202,357,28,-1,512)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
$badd = GUICtrlCreateButton("A D D",8,242,100,36,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
$inputsearch2 = GUICtrlCreateInput("aa",8,31,240,28,-1,512)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
GUICtrlCreateLabel("Search By Hostname:",8,7,163,20,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
GUICtrlSetBkColor(-1,"-2")
$bsearch2 = GUICtrlCreateButton("S E A R C H",258,31,100,28,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
$bless = GUICtrlCreateButton("<",8,68,32,30,-1,-1)
$bgreater = GUICtrlCreateButton(">",216,68,32,30,-1,-1)
$bdelete = GUICtrlCreateButton("R E M O V E",132,242,114,36,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
$exit = GUICtrlCreateButton("E X I T",265,242,100,36,-1,-1)
GUICtrlSetFont(-1,12,400,0,"MS Sans Serif")
GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                GUIDelete($hChild)
                ExitLoop
            Case $exit
                GUIDelete($hChild)
                Return
            Case $badd
                Addrecords()
        EndSwitch
    WEnd

EndFunc

Func Addrecords() ; This is where it doesn't know what $inputsearch2 is because $inputsearch2 doesn't exist in this function. Only exist in Example Function
    $text11 = GUICtrlRead($inputsearch2)
    
    MsgBox(0, "TEST", $text11)
EndFunc

Saludos

Share this post


Link to post
Share on other sites

oh wow, so If I have other variables such as arrays, I would need to set all of them as global as well?


Msgbox(0, "Hate", "Just hate it when I post a question and find my own answer after a couple tries. But if I don't post the question, I can't seem to resolve it at all.")

Share this post


Link to post
Share on other sites

or pass the value to the function...

example()

Func Example()

    $hChild = GUICreate("Adding", 372, 284, -1, -1, -1, -1)
    $InputHost = GUICtrlCreateInput("", 8, 131, 357, 28, -1, 512)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlCreateLabel("Computer Hostname:", 8, 109, 163, 20, -1, -1)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlSetBkColor(-1, "-2")
    GUICtrlCreateLabel("ID:", 8, 179, 163, 20, -1, -1)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlSetBkColor(-1, "-2")
    $inputid = GUICtrlCreateInput("", 8, 202, 357, 28, -1, 512)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $badd = GUICtrlCreateButton("A D D", 8, 242, 100, 36, -1, -1)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $inputsearch2 = GUICtrlCreateInput("aa", 8, 31, 240, 28, -1, 512)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlCreateLabel("Search By Hostname:", 8, 7, 163, 20, -1, -1)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlSetBkColor(-1, "-2")
    $bsearch2 = GUICtrlCreateButton("S E A R C H", 258, 31, 100, 28, -1, -1)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $bless = GUICtrlCreateButton("<", 8, 68, 32, 30, -1, -1)
    $bgreater = GUICtrlCreateButton(">", 216, 68, 32, 30, -1, -1)
    $bdelete = GUICtrlCreateButton("R E M O V E", 132, 242, 114, 36, -1, -1)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $exit = GUICtrlCreateButton("E X I T", 265, 242, 100, 36, -1, -1)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()
            Case -3
                GUIDelete($hChild)
                ExitLoop
            Case $exit
                GUIDelete($hChild)
                Return
            Case $badd
                Addrecords(guictrlread($inputsearch2))   ;  <----- pass as parm
        EndSwitch
    WEnd

EndFunc   ;==>Example

Func Addrecords($inrec) ; change to accept parm

    MsgBox(0, "TEST", $inrec)

EndFunc   ;==>Addrecords

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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