Jump to content

Show/hide input control without looping?


Recommended Posts

Hello,

First of all, AutoIt rocks! I used to use v2 and always feared v3 is too complicated. It turned out not to be and the creation of GUI is amazingly easy, I always believed such things are restricted to windows gurus. :P Also, the forum and online documentation helped me a lot to solve a lot of questions on my first steps.

However, now I have a problem: logics. Unfortunately, I don't even know what search terms I should use really to find the solution to my problem (tried some but without success).

It may sound trivial for sure but I want to create a list with week days (just as example) and depending on what the user selects an input control is shown or hidden (the text you can enter there will be processed in a later development stage of the program). Ignore most of the declared variables, they're not used in this example

Due to the loop that is mandatory to have the GUI, the input control is created over and over and over again instead of just being created/shown once and hidden when something else is selected. I used to have an extra variable checking the show/hide state of the variable so it happens only once but it didn't work out so the example just checks if Wednesday is selected and if yes, the argument1 box will be shown, else hidden - however it's spammed (see the part after the 'do' loop starts). What is needed to avoid the spam and create/hide it once when selecting a week day?

Here's the code:

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

Example()

Func Example()
    Local $menu1, $n1, $n2, $n3, $n4, $msg, $menustate, $menutext, $test1
    Local $bname, $baddress, $bemail
    Local $chkBoxName, $chkBoxAddress
    Local $namedata, $argument1, $argument2

 Local $defaultstatus = "Ready", $status, $filemenu, $fileitem, $filehelp, $helpmenu
    Local $helpmenu, $infoitem, $exititem, $aboutitem, $helpitem



    GUICreate("paste templates"); will create a dialog box that when displayed is centered

    $filemenu = GUICtrlCreateMenu("File")
    $exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
    
    
     GUICtrlCreateLabel("Day:", 10, 0)
    
     
     GUICtrlCreateLabel("Argument1:", 10, 135)
     $argument1 = GUICtrlCreateInput("", 10, 150, 220, 20)

    $n1 = GUICtrlCreateList("", 10, 15, -1, 100);create the list with week days
    GUICtrlSetData(-1, "Monday|Tuesday|Wednesday|", "Monday")
    
    
    $n3 = GUICtrlCreateButton("Read", 10, 310, 50);not important for this example
 ;GUICtrlSetState(-1, $GUI_FOCUS); the focus is on this button
    GUICtrlSetState($n3, 144);gray out this button
    
    $n4 = GUICtrlCreateButton("Send to Clipboard", 65, 310, 100);not important for this example
    GUICtrlSetState($n4, 144);gray out this button
    
    
    GUISetState(); will display an empty dialog box
 ; Run the GUI until the dialog is closed
    
    Do
        
       
        $msg = GUIGetMsg()
        if GUICtrlRead($n1) = 'Wednesday' then;SPAMMED ENDLESSLY, I WANT TO BE SHOWN/HIDDEN ONLY ONCE!
            GUICtrlSetState($argument1, $GUI_SHOW)
        else
            GUICtrlSetState($argument1, $GUI_HIDE)
            endif
                        
 If $msg = $exititem Then ExitLoop
     
    Until $msg = $GUI_EVENT_CLOSE
EndFunc;==>Example
Edited by Automania
Link to comment
Share on other sites

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

Example()

Func Example()
    Local $menu1, $n1, $n2, $n3, $n4, $msg, $menustate, $menutext, $test1
    Local $bname, $baddress, $bemail
    Local $chkBoxName, $chkBoxAddress
    Local $namedata, $argument1, $argument2

Local $defaultstatus = "Ready", $status, $filemenu, $fileitem, $filehelp, $helpmenu
    Local $helpmenu, $infoitem, $exititem, $aboutitem, $helpitem



    GUICreate("paste templates"); will create a dialog box that when displayed is centered

    $filemenu = GUICtrlCreateMenu("File")
    $exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
    
    
     GUICtrlCreateLabel("Day:", 10, 0)
    
    
     GUICtrlCreateLabel("Argument1:", 10, 135)
     $argument1 = GUICtrlCreateInput("", 10, 150, 220, 20)

    $n1 = GUICtrlCreateList("", 10, 15, -1, 100);create the list with week days
    GUICtrlSetData(-1, "Monday|Tuesday|Wednesday|", "Monday")
    
    
    $n3 = GUICtrlCreateButton("Read", 10, 310, 50);not important for this example
  ;GUICtrlSetState(-1, $GUI_FOCUS); the focus is on this button
    GUICtrlSetState($n3, 144);gray out this button
    
    $n4 = GUICtrlCreateButton("Send to Clipboard", 65, 310, 100);not important for this example
    GUICtrlSetState($n4, 144);gray out this button
    
    
    GUISetState(); will display an empty dialog box
  ; Run the GUI until the dialog is closed
    
    Do
        
      
        $msg = GUIGetMsg()
        if GUICtrlRead($n1) = 'Wednesday' then;SPAMMED ENDLESSLY, I WANT TO BE SHOWN/HIDDEN ONLY ONCE!
            ControlEnable ( "","",$argument1)

        else
            ControlDisable( "","",$argument1)
            endif
                        
If $msg = $exititem Then ExitLoop
    Sleep(25)
    Until $msg = $GUI_EVENT_CLOSE
EndFunc ;==>Example

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

Thank you very much, that is a really nice solution to my problem, I didn't know that command yet. It will do to continue my program. :P

Is there a way to even show/hide the input control instead of graying it out?

edit: solved it myself, got curious looking at the other control... functions and CONTROLSHOW / CONTROLHIDE did the trick (and the parameters can even stay the same). Thanks a lot again!

Edited by Automania
Link to comment
Share on other sites

Thank you very much, that is a really nice solution to my problem, I didn't know that command yet. It will do to continue my program. :P

Is there a way to even show/hide the input control instead of graying it out?

edit: solved it myself, got curious looking at the other control... functions and CONTROLSHOW / CONTROLHIDE did the trick (and the parameters can even stay the same). Thanks a lot again!

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

Example()

Func Example()
    Local $menu1, $n1, $n2, $n3, $n4, $msg, $menustate, $menutext, $test1
    Local $bname, $baddress, $bemail
    Local $chkBoxName, $chkBoxAddress

Local $defaultstatus = "Ready", $status, $filemenu, $fileitem, $filehelp, $helpmenu
    Local $helpmenu, $infoitem, $exititem, $aboutitem, $helpitem
    Local $namedata, $argument1, $argument2



    GUICreate("paste templates"); will create a dialog box that when displayed is centered

    $filemenu = GUICtrlCreateMenu("File")
    $exititem = GUICtrlCreateMenuItem("Exit", $filemenu)
    
    
     GUICtrlCreateLabel("Day:", 10, 0)
    
    
     GUICtrlCreateLabel("Argument1:", 10, 135)
     $argument1 = GUICtrlCreateInput("", 10, 150, 220, 20)

    $n1 = GUICtrlCreateList("", 10, 15, -1, 100);create the list with week days
    GUICtrlSetData(-1, "Monday|Tuesday|Wednesday|", "Monday")
    
    
    $n3 = GUICtrlCreateButton("Read", 10, 310, 50);not important for this example
 ;GUICtrlSetState(-1, $GUI_FOCUS); the focus is on this button
    GUICtrlSetState($n3, 144);gray out this button
    
    $n4 = GUICtrlCreateButton("Send to Clipboard", 65, 310, 100);not important for this example
    GUICtrlSetState($n4, 144);gray out this button
    
    
    GUISetState(); will display an empty dialog box
 ; Run the GUI until the dialog is closed
    
    Do
        
      
        $msg = GUIGetMsg()
        if GUICtrlRead($n1) = 'Wednesday' then;SPAMMED ENDLESSLY, I WANT TO BE SHOWN/HIDDEN ONLY ONCE!
            ControlShow( "","",$argument1)

        else
            ControlHide( "","",$argument1)
            endif
                        
If $msg = $exititem Then ExitLoop
    Sleep(25)
    Until $msg = $GUI_EVENT_CLOSE
EndFunc;==>Example

When the words fail... music speaks.

Link to comment
Share on other sites

  • 3 weeks later...

Hello again,

Thanks to the help I received here (and to the online documentation) the progress is really nice but now I ran into a similar problem which is related to logics again (probably). I always had problems with GUIs and games since they always rely on loops and when things should happen only once I always stumble into the same problem to make it only occur once (as in my first posting here, the solution worked out perfectly).

I made a simple program to show the problem: I create a box with GUICtrlCreateList and depending on what the user selects, a 2nd list window pops up. So far so good. But depending on what category is chosen in the first box the 2nd box which pops up should have different content. So if you select "cars" as category it should list car models while selecting "bikes" shows different motorbike models.

However, by clearing the list variable and adding new values it keeps refreshing/spamming within the 2nd box and slows down the program. Is there a way to handle this in a more efficient way?

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

Example()

Func Example()
    Local $mylist, $msg, $mylist2
   
    GUICreate("My GUI list"); will create a dialog box that when displayed is centered

    $mylist = GUICtrlCreateList("", 176, 32, 121, 97)
    GUICtrlSetData($mylist, "1 nothing|2 bikes|3 cars|", "1 nothing")
     
    $mylist2 = GUICtrlCreateList("", 176, 232, 121, 97)

    GUISetState()
    
    Controlhide( "","",$mylist2)
    
    $msg = 0
    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()

        Select
        
        Case GUICtrlRead($mylist) = "1 nothing"
            Controlhide( "","",$mylist2)
            
        Case GUICtrlRead($mylist) = "2 bikes"
            Controlshow( "","",$mylist2)
            GUICtrlSetData($mylist2, "")
            GUICtrlSetData($mylist2, "Harley Davidson|Y2K")
                                
        Case GUICtrlRead($mylist) = "3 cars"
            Controlshow( "","",$mylist2)
            GUICtrlSetData($mylist2, "")
            GUICtrlSetData($mylist2, "Ferrari|Porsche")
                    
        EndSelect
    WEnd
EndFunc  ;==>Example
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...