Jump to content

Button properties - Default and Cancel


trids
 Share

Recommended Posts

How does one set the ".Default" and ".Cancel" properties for specific buttons on a GUI form?

In VB, each button has both properties, all set to False. When one button has its ".Default" set to True, this indicates that it will receive an automatic "Click" event if focus is elsewhere on a form (like in a textbox) and the user presses {ENTER}.

The regular InputBox() dialog is a good example: your cursor might be in the input area when you press {ENTER}, but it's the same as clicking the OK button. Similarly for Escape .. one would set the ".Cancel" property on the "Cancel" button to True, in order for that button to receive an automatic "Click" event when the user presses the {Esc} key.

If there is no such feature for AU3 GUI, then does anyone have any ideas on simulating the effect?

TIA

:P

Link to comment
Share on other sites

  • Moderators

How does one set the ".Default" and ".Cancel" properties for specific buttons on a GUI form?

In VB, each button has both properties, all set to False. When one button has its ".Default" set to True, this indicates that it will receive an automatic "Click" event if focus is elsewhere on a form (like in a textbox) and the user presses {ENTER}.

The regular InputBox() dialog is a good example: your cursor might be in the input area when you press {ENTER}, but it's the same as clicking the OK button. Similarly for Escape .. one would set the ".Cancel" property on the "Cancel" button to True, in order for that button to receive an automatic "Click" event when the user presses the {Esc} key.

If there is no such feature for AU3 GUI, then does anyone have any ideas on simulating the effect?

TIA

:P

Made an example ... even though I know you found it:
#include <guiconstants.au3>
Global $aCID[4]
$hGUI = GUICreate('Input Box', 200, 80)
$aCID[1] = GUICtrlCreateInput('', 10, 10, 180, 20)
$aCID[2] = GUICtrlCreateButton('OK', 45, 40, 50, 30, $BS_DEFPUSHBUTTON)
$aCID[3] = GUICtrlCreateButton('Cancel', 105, 40, 50, 30)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case - 3, $aCID[3]
            ExitLoop
        Case $aCID[2]
            MsgBox(64, 'Info', 'You pressed "Enter" or the button itself')
    EndSwitch
WEnd

MsgBox(64, 'Info', 'You either pressed the "Cancel" button or "Esc"')

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

I don't know how close this is to a working example, I tested it twice and it seems to work fine:

#include <guiconstants.au3>
MsgBox(0, 'Info', _InPutBoxEx('My Input Box'))
MsgBox(0, 'Info', _InPutBoxEx('My Input Box', 'Some Text', 300))

Func _InPutBoxEx($sTitle, $sText = '', $iWidth = 200, $iHeight = 80)
    Local $aCID[5], $iY = 0
    If $sText <> '' Then $iY = 20
    Local $hGUI = GUICreate($sTitle, $iWidth, $iHeight + $iY)
    If $sText <> '' Then $aCID[1] = GUICtrlCreateLabel($sText, 10, 10, $iWidth - 20, 20, $ES_CENTER)
    $aCID[2] = GUICtrlCreateInput('', 10, 10 + $iY, $iWidth - 20, 20)
    $aCID[3] = GUICtrlCreateButton('OK', (($iWidth - 100) / 2), 40 + $iY, 50, 30, $BS_DEFPUSHBUTTON)
    $aCID[4] = GUICtrlCreateButton('Cancel', (($iWidth - 100) / 2) + 55, 40 + $iY, 50, 30)
    GUISetState()
    While 1
        Switch GUIGetMsg()
            Case - 3, $aCID[4]
                Return ''
            Case $aCID[3]
                Return GUICtrlRead($aCID[2])
        EndSwitch
    WEnd
EndFunc
But if you're looking for a custom InputBox due to the char issue from yesterday, this could possibly work for you.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Thanks smokey .. I'll see how it works :nuke:

Hope it works for you, one thing I just noticed I didn't delete the GUI before the return and I didn't give the option to add styles (You may want $ES_PASSWORD)... This should fix that:
#include <guiconstants.au3>
MsgBox(0, 'Info', _InPutBoxEx('My Input Box'))
MsgBox(0, 'Info', _InPutBoxEx('My Input Box', 'Some Text', 300))



Func _InPutBoxEx($sTitle, $sText = '', $iWidth = 200, $iHeight = 75, $iOnTop = 1, $nInStyle = -1, $nInStyleEx = -1)
    Local $aCID[5], $iY = 0, $sRead
    If $iWidth < 175 Then $iWidth = 175
    If $iHeight < 75 Then $iHeight = 75
    If $sText <> '' Then $iY = 20
    Local $hGUI = GUICreate($sTitle, $iWidth, $iHeight + $iY)
    If $sText <> '' Then $aCID[1] = GUICtrlCreateLabel($sText, 10, 10, $iWidth - 20, 20, $ES_CENTER)
    $aCID[2] = GUICtrlCreateInput('', 10, 10 + $iY, $iWidth - 20, 20, $nInStyle, $nInStyleEx)
    $aCID[3] = GUICtrlCreateButton('OK', (($iWidth - 150) / 2), 40 + $iY, 75, 25, $BS_DEFPUSHBUTTON)
    $aCID[4] = GUICtrlCreateButton('Cancel', (($iWidth - 150) / 2) + 80, 40 + $iY, 75, 25)
    WinSetOnTop($hGUI, '', $iOnTop)
    GUISetState()
    While 1
        Switch GUIGetMsg()
            Case - 3, $aCID[4]
                GUIDelete($hGUI)
                Return ''
            Case $aCID[3]
                $sRead = GUICtrlRead($aCID[2])
                GUIDelete($hGUI)
                Return $sRead
        EndSwitch
    WEnd
EndFunc

Edit:

Added WinSetOnTop option... Default is to Set it on top.

Edit2:

Made buttons a smaller height, changed params

Edit3:

Made sure there was a true default width and height so buttons don't overlap.

>>>Ok I'm done now :P<<<

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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