Sign in to follow this  
Followers 0
rooteam

Show Tooltip on wrong Input

6 posts in this topic

Hi,

what i want is to create a form with an edit field. If the edit field lost the focus the content is checked for valid input. If there's no valid input (currently) a msgbox is displayed. But what i really want, is to display a tooltip at the edit field. Of course i know the Tooltip()-Function. But if the Form is moved, the Tooltip doesn't. So i played around with the _GUITooltip.. UDFs but i i can't figure out how it works correctly. Any help is appreciated!

TIA

--

Andre

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


Opt("GUIOnEventMode", 1)

$hForm = GUICreate("Example", 411, 200, 302, 218, -1, BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
GUISetOnEvent($GUI_EVENT_CLOSE, "_Cancel_ButtonClick")

$hCustomerID_Label = GUICtrlCreateLabel("Customer ID:", 10, 109, 110, 17, $SS_RIGHT)
$hCustomerID_Edit = GUICtrlCreateInput("", 125, 106, 180, 21)
GUICtrlSetOnEvent(-1, "_CustomerID_EditChange")
$hCustomerName_Label = GUICtrlCreateLabel("Customer:", 10, 140, 110, 17, $SS_RIGHT)
$hCustomerName_Edit = GUICtrlCreateInput("", 125, 137, 180, 21)
$hCancel_Button = GUICtrlCreateButton("Cancel", 320, 15, 75, 25)
GUICtrlSetOnEvent(-1, "_Cancel_ButtonClick")
GUISetState(@SW_SHOW)

While 1
    Sleep(100)
WEnd

Func _Cancel_ButtonClick()
    Exit
EndFunc

Func _CustomerID_EditChange()
    ; Check for coorrect input e.g.
    $avCurrentInput = StringSplit(GUICtrlRead($hCustomerID_Edit), "", 2)
    For $vElem In $avCurrentInput
        If Asc($vElem)>57 Or Asc($vElem)<48 Then
            MsgBox(16, "Error", "No valid Customer ID!", 0, $hForm)
            ExitLoop
        EndIf
    Next

EndFunc

Share this post


Link to post
Share on other sites



rooteam,

Try this...

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <editconstants.au3>
#include <GuiEdit.au3>

Opt("GUIOnEventMode", 1)

$hForm = GUICreate("Example", 411, 200, 302, 218, -1, BitOR($WS_EX_TOPMOST, $WS_EX_WINDOWEDGE))
GUISetOnEvent($GUI_EVENT_CLOSE, "_Cancel_ButtonClick")

$hCustomerID_Label = GUICtrlCreateLabel("Customer ID:", 10, 109, 110, 17, $SS_RIGHT)
$hCustomerID_Edit = GUICtrlCreateInput("", 125, 106, 180, 21)
$hCustomerName_Label = GUICtrlCreateLabel("Customer:", 10, 140, 110, 17, $SS_RIGHT)
$hCustomerName_Edit = GUICtrlCreateInput("", 125, 137, 180, 21)
$hCancel_Button = GUICtrlCreateButton("Cancel", 320, 15, 75, 25)
GUICtrlSetOnEvent(-1, "_Cancel_ButtonClick")
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')

While 1
    Sleep(100)
WEnd

Func _Cancel_ButtonClick()
    Exit
EndFunc   ;==>_Cancel_ButtonClick

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    Switch BitShift($wParam, 16)
        Case $EN_CHANGE
            Switch BitAND($wParam, 0xFFFF)
                Case $hCustomerID_Edit
                    ; adapted from code by Smoke_N
                    $sText = _GUICtrlEdit_GetText($hCustomerID_Edit)
                    If Not StringRegExp($sText, "(?i)^[0-9]+\z") Then
                        _GUICtrlEdit_SetText($hCustomerID_Edit, StringTrimRight($sText, 1))
                        _GUICtrlEdit_SetSel($hCustomerID_Edit, StringLen($sText), StringLen($sText))
                        _GUICtrlEdit_ShowBalloonTip($hCustomerID_Edit, "Error", "You can only use characters 0-9", $TTI_ERROR)
                    EndIf
                Case $hCustomerName_Edit
                    ;
                    ; whatever you want here...remember, minimize processing in registered functions
                    ;
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

kylomas


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

Wow, that's pretty cool! Much more than i expected! Thanks a lot, kylomas!

--

Andre

Share this post


Link to post
Share on other sites

rooteam,

This is better technique.  Per the Help file, time inside a registered function should be limited.  Note the dummy controls actioned by the WM_COMMAND routine.  Edits done by this routine are for valid input only.  Other edits should be done in a separate routine ( see _CustomerName_Edit2() ).

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <editconstants.au3>
#include <GuiEdit.au3>

Opt("GUIOnEventMode", 1)

$hForm = GUICreate("Example", 411, 200, 302, 218, -1, BitOR($WS_EX_TOPMOST, $WS_EX_WINDOWEDGE))
GUISetOnEvent($GUI_EVENT_CLOSE, "_Cancel_ButtonClick")

$hCustomerID_Label = GUICtrlCreateLabel("Customer ID:", 10, 109, 110, 17, $SS_RIGHT)
$hCustomerID_Edit = GUICtrlCreateInput("", 125, 106, 180, 21)
$hCustomerName_Label = GUICtrlCreateLabel("Customer:", 10, 140, 110, 17, $SS_RIGHT)
$hCustomerName_Edit = GUICtrlCreateInput("", 125, 137, 180, 21)
GUICtrlSetOnEvent(-1,'_CustomerName_Edit2'
$hCancel_Button = GUICtrlCreateButton("Cancel", 320, 15, 75, 25)
GUICtrlSetOnEvent(-1, "_Cancel_ButtonClick")

; added these two dummy controls which will be actioned by $EN_CHANGE as the user types
; This let's you get out of the registered function as quickly as possible as per the Help Doc

$hDummyCustomerID_Edit = guictrlcreatedummy()
guictrlsetonevent(-1,'_CustomerID_Edit')
$hDummyCustomerName_Edit1 = guictrlcreatedummy()
guictrlsetonevent(-1,'_CustomerName_Edit1')
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, 'WM_COMMAND')

While 1
    Sleep(100)
WEnd

Func _Cancel_ButtonClick()
    Exit
EndFunc   ;==>_Cancel_ButtonClick

func _CustomerID_Edit()

    ; adapted from code by Smoke_N
    $sText = _GUICtrlEdit_GetText($hCustomerID_Edit)
    if $sText = '' then return                          ; added in case the first char typed is invalid which caused loop due to $EN_CHANGE
    If Not StringRegExp($sText, "(?i)^[0-9]+\z") Then
        _GUICtrlEdit_SetText($hCustomerID_Edit, StringTrimRight($sText, 1))
        _GUICtrlEdit_SetSel($hCustomerID_Edit, StringLen($sText), StringLen($sText))
        _GUICtrlEdit_ShowBalloonTip($hCustomerID_Edit, "Error", "You can only use characters 0-9", $TTI_ERROR)
    EndIf

endfunc

func _CustomerName_Edit1()

    ; edit for valid chars only
    $sText = _GUICtrlEdit_GetText($hCustomerName_Edit)
    if $sText = '' then return                          ; added in case the first char typed is invalid which caused loop due to $EN_CHANGE
    if not stringregexp($sText,'(?i)^[[:alpha:] ,]+\z') then
        _GUICtrlEdit_SetText($hCustomerName_Edit, StringTrimRight($sText, 1))
        _GUICtrlEdit_SetSel($hCustomerName_Edit, StringLen($sText), StringLen($sText))
        _GUICtrlEdit_ShowBalloonTip($hCustomerName_Edit, "Error", "You can only enter letters, spaces or a comma", $TTI_ERROR)
    endif

endfunc

func _CustomerName_Edit2()
    ;
    ; do other kinds of edits here (actioned after control looses focus)
    ;
endfunc

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    Switch BitShift($wParam, 16)
        Case $EN_CHANGE
            Switch BitAND($wParam, 0xFFFF)
                Case $hCustomerID_Edit
                    guictrlsendtodummy($hDummyCustomerID_Edit)
                Case $hCustomerName_Edit
                    guictrlsendtodummy($hDummyCustomerName_Edit1)
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

kylomas


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

kylomas,

you're right! That's better! BTW, i noticed a little error in _CustomerID_Edit(). The error occurs, if the user set the cursor inside an existing CustomerID-String and tries to enter a non-numeric character. I changed the function to this:

func _CustomerID_Edit()

    ; adapted from code by Smoke_N
    $sText = _GUICtrlEdit_GetText($hCustomerID_Edit)
    if $sText = '' then return                          ; added in case the first char typed is invalid which caused loop due to $EN_CHANGE
    If Not StringRegExp($sText, "(?i)^[0-9]+\z") Then
    $avSelected = _GUICtrlEdit_GetSel($hCustomerID_Edit)
    _GUICtrlEdit_SetText($hCustomerID_Edit, StringRegExpReplace($sText,"[^0-9]",""))                        ; new

        ; removed      _GUICtrlEdit_SetText($hCustomerID_Edit, StringTrimRight($sText, 1))
        ; removed      _GUICtrlEdit_SetSel($hCustomerID_Edit, StringLen($sText), StringLen($sText))

    _GUICtrlEdit_SetSel($hCustomerID_Edit, $avSelected[0]-1, $avSelected[0]-1)                              ; new
        _GUICtrlEdit_ShowBalloonTip($hCustomerID_Edit, "Error", "You can only use characters 0-9", $TTI_ERROR)
    EndIf

endfunc

Thanks again

--

rooTeam

Share this post


Link to post
Share on other sites

Excellent, glad it worked out!


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
Sign in to follow this  
Followers 0