Jump to content

While loop while input field is active not working as expected?


lavascript
 Share

Recommended Posts

Hopefully this is a dumb, easy question.

I have three input fields, and I would like to parse the data entered in the first field to generate default data for the other two. But I only want that to run while the first field has focus, or else the data could not be changed.

So I imagined I could use a While loop testing the state of $name, expecting it to be 256 (The value of $GUI_FOCUS from GUIConstantsEx.au3). But when I do that, the functions never run. I checked the state of the field with a Label, and it shows that the state is 80. (There's no GUI Constant for 80.) It seems to return 80, though, regardless of whether the field has focus or not.

When I While Loop on 80, as soon as the field loses focus, it locks up.

What gives?

GUICreate("New Account",265,110,-1,-1,-1,-1,$mainwindow)
GUICtrlCreateLabel("Full Name:",5,7)
GUICtrlCreateLabel("Username:",5,32)
GUICtrlCreateLabel("Password:",5,57)
Dim $name = GUICtrlCreateInput("",60,5,200,20)
Dim $uname = GUICtrlCreateInput("",60,30,200,20)
Dim $pass = GUICtrlCreateInput("",60,55,200,20)
Dim $newacctbtn = GUICtrlCreateButton("Create Account",5,80,100,25)
Dim $state = GUICtrlCreateLabel("",230,90)
GUISetState(@SW_SHOW)

While True
Dim $msg2 = GUIGetMsg()
;GUICtrlSetData($state,GUICtrlGetState($name))
While GUICtrlGetState($name) = XXX
GUICtrlSetData($uname,NameToUname(GUICtrlRead($name)))
GUICtrlSetData($pass,GenPass(GUICtrlRead($name)))
WEnd
If $msg2 = $newacctbtn Then MsgBox(0,"","snth")
If $msg2 = -3 Then ExitLoop
WEnd

Func NameToUname($input)
If $input <> "" And StringInStr($input," ") > 0 Then
Local $finit = StringLeft($input,1)
Local $array = StringSplit($input," ")
Local $lname = $array[$array[0]]
Return StringLower($finit & $lname)
Else
Return ""
EndIf
EndFunc

Func GenPass($input)
If $input <> "" And StringInStr($input," ") > 0 Then
Local $finit = StringLeft($input,1)
Local $array = StringSplit($input," ")
Local $linit = StringLeft($array[$array[0]],1)
Return StringLower($finit & $linit & @YEAR)
Else
Return ""
EndIf
EndFunc
Link to comment
Share on other sites

State values can be summed up as for example $GUI_DISABLE + $GUI_HIDE sets the control in an disabled and hidden state.

There fore you should use

BitAnd()
to get your result

More preferably you could use _Winapi_GetFocus() with the GuiCtrlGetHandle of the Input/Label whichever you want

Hope it helps

Feel free to ask if any doubts are there

Regards

Phoenix XL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

You might have better luck getting what you want by intercepting the WM_COMMAND message.

I stuck some beeps() and tooltips() in this example to make it more entertaining:

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

Dim $msg2, $update_flag

GUICreate("New Account",265,110,-1,-1,-1,-1)
GUICtrlCreateLabel("Full Name:",5,7)
GUICtrlCreateLabel("Username:",5,32)
GUICtrlCreateLabel("Password:",5,57)
Dim $name = GUICtrlCreateInput("",60,5,200,20)
Dim $uname = GUICtrlCreateInput("",60,30,200,20)
Dim $pass = GUICtrlCreateInput("",60,55,200,20)
Dim $newacctbtn = GUICtrlCreateButton("Create Account",5,80,100,25)
Dim $state = GUICtrlCreateLabel("",230,90)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While True
    If $update_flag Then
        Beep(800,50)
        $sName = GUICtrlRead($name)
        GUICtrlSetData($uname,NameToUname($sName))
        GUICtrlSetData($pass,GenPass($sName))
        $update_flag = 0
    EndIf
    $msg2 = GUIGetMsg()
    If $msg2 = $newacctbtn Then MsgBox(0,"","snth")
    If $msg2 = -3 Then ExitLoop
WEnd

Func NameToUname($input)
If $input <> "" And StringInStr($input," ") > 0 Then
Local $finit = StringLeft($input,1)
Local $array = StringSplit($input," ")
Local $lname = $array[$array[0]]
Return StringLower($finit & $lname)
Else
Return ""
EndIf
EndFunc

Func GenPass($input)
If $input <> "" And StringInStr($input," ") > 0 Then
Local $finit = StringLeft($input,1)
Local $array = StringSplit($input," ")
Local $linit = StringLeft($array[$array[0]],1)
Return StringLower($finit & $linit & @YEAR)
Else
Return ""
EndIf
EndFunc

Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    $nNotifyCode    = BitShift($wParam, 16)    ;HiWord
    $nID            = BitAnd($wParam, 0x0000FFFF) ;LoWord
    Switch $nID
        Case $name
            Switch $nNotifyCode
                Case 256 ; gained focus
                 ToolTip("Gained Focus")
                Case 512 ; lost focus
                    ToolTip("Lost Focus")
                Case 768 ; data updated
                 ToolTip("Data Updated")
                 $update_flag = 1
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc
Link to comment
Share on other sites

  • Moderators

The list of Windows Message Codes is in the help file, or you can look here. Then, for specific info, I would suggest searching MSDN:

http://msdn.microsoft.com/en-us/library/windows/desktop

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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

×
×
  • Create New...