Sign in to follow this  
Followers 0
ASut

Need help in GUI

10 posts in this topic

#1 ·  Posted (edited)

I have follow the e.g given by Tvern in post http://www.autoitscript.com/forum/index.php?showtopic=119508&view=findpost&p=830560

But it won't work for the input field, at the beginning the button is not disable,in order to make it disable, we need to type something,and delete them. how can I make the button disable by default.

#include <GUIConstants.au3>

$Form1 = GUICreate("Form1", 269, 179, 193, 115)
$input = GUICtrlCreateinput("", 80, 48, 97, 17)
$Button = GUICtrlCreateButton("Button", 96, 88, 75, 25, 0)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            
        case $input
        if StringLen(GUICtrlRead($Input))<> 0 then
                GUICtrlSetState($Button,$GUI_ENABLE)
            Else
                GUICtrlSetState($Button,$GUI_DISABLE)
        EndIf
    EndSwitch
WEnd
Edited by ASut

Share this post


Link to post
Share on other sites



Have it use GUICtrlSetState($Button,$GUI_DISABLE) at the beginning of your code, such as how you would normally set a variable starting value.

The variable will set that state at the beginning so when it is displayed it will display disabled.

Share this post


Link to post
Share on other sites

Have it use GUICtrlSetState($Button,$GUI_DISABLE) at the beginning of your code, such as how you would normally set a variable starting value.

The variable will set that state at the beginning so when it is displayed it will display disabled.

While this is correct for making sure the button starts disabled, the button still won't update properly, because an input does not set GUIGetMsg() every time it's contents change.

The easiest way to solve this is to check the contents of the input on each loop and check if it has changed, then take action accordingly, like this:

#include <GUIConstants.au3>
Local $sInput, $sOldInput = 1 ;make the value of $sOldInput different from $sInput to trigger an update when it forst runs.
$Form1 = GUICreate("Form1", 269, 179, 193, 115)
$input = GUICtrlCreateinput("", 80, 48, 97, 17)
$Button = GUICtrlCreateButton("Button", 96, 88, 75, 25, 0)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
        ;Case $input is removed because it's unreliable for what you want to do.
    EndSwitch

    $sInput = GUICtrlRead($input)
    If $sInput <> $sOldInput Then ;only run when the input has changed to prevent flicker
        If $sInput Then
            GUICtrlSetState($Button,$GUI_ENABLE)
        Else
            GUICtrlSetState($Button,$GUI_DISABLE)
        EndIf
        $sOldInput = $sInput ;allows to check if the content has changed on the next run.
    EndIf

WEnd

Share this post


Link to post
Share on other sites

thanks all for helping me to slove my probelm, here is the new code wrote by myself,is it make sense? and how can I highlight the code in the forum?

#include <GUIConstants.au3>
$Form1 = GUICreate("Form1", 269, 179, 193, 115)
$input = GUICtrlCreateinput("", 80, 48, 97, 17)
$Button = GUICtrlCreateButton("Button", 96, 88, 75, 25, 0)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
        ;Case $input is removed because it's unreliable for what you want to do.
    EndSwitch

    if GUICtrlRead($input)="" then
        if BitAND(GUICtrlgetState($Button),$GUI_DISABLE) then GUICtrlSetState($Button,$GUI_ENABLE)
    Else
        if BitAND(GUICtrlgetState($Button),$GUI_ENABLE) then GUICtrlSetState($Button,$GUI_DISABLE)  
    EndIf
WEnd

Share this post


Link to post
Share on other sites

Yeah that's also a good way of doing it, except that it's the exact opposite from what I thought you wanted. (the button enables when the input is empty)

To highlight the code you can use the [ autoit] and [ /autoit] tags (minus the space)

Share this post


Link to post
Share on other sites

Yeah that's also a good way of doing it, except that it's the exact opposite from what I thought you wanted. (the button enables when the input is empty)

To highlight the code you can use the [ autoit] and [ /autoit] tags (minus the space)

thanks ;)

Share this post


Link to post
Share on other sites

New question to the $input = GUICtrlCreateinput("", 80, 48, 97, 17)

how can I limit it can only type letters and numbers, any symbol is not allowed, if the user have input some symbol, when he press the button, the warming msg box will appear or just keep the button in disable satate.

Share this post


Link to post
Share on other sites

You can do this in at least 3 ways:

1. Whenever the input has changed, check if the new string has unwanted symbols, remove them and set the text in the input to the result string.

2. Whenever the input has changed, check if the new string has unwanted symbols, leave them in the string, but disable the button.

3. Whenever the button is pressed, check if the new string has unwanted symbols, show a messagebox and to the main loop.

For option 1 you're best off using StringRegExpReplace. It might seem a little confusing at first, but it's very usefull. To write the string back to the input, use GuiCtrlSetData.

For option 2 and 3 just check if the string is valid with StringIsAlNum(

Share this post


Link to post
Share on other sites

You can do this in at least 3 ways:

1. Whenever the input has changed, check if the new string has unwanted symbols, remove them and set the text in the input to the result string.

2. Whenever the input has changed, check if the new string has unwanted symbols, leave them in the string, but disable the button.

3. Whenever the button is pressed, check if the new string has unwanted symbols, show a messagebox and to the main loop.

For option 1 you're best off using StringRegExpReplace. It might seem a little confusing at first, but it's very usefull. To write the string back to the input, use GuiCtrlSetData.

For option 2 and 3 just check if the string is valid with StringIsAlNum(

thanks by using option 2,I got what I want.

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