Sign in to follow this  
Followers 0
lavascript

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

6 posts in this topic

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Sorry, brain fart. Of course these numbers are binary!

So my GUICtrlGetState is returning 80, which is 64+16, which is $GUI_ENABLE + $GUI_SHOW. But if the field has focus, shouldn't that also be $GUI_FOCUS?, so 336?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Great! I always think about things in a backwards sort of way.

If you wouldn't mind, can you explain a little about what's going on here? Or point me to a resource where I can learn more about Windows Message Codes?

Thanks!

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

  • Similar Content

    • nacerbaaziz
      By nacerbaaziz
      hello
      Greetings to all
      dears i have a big problem in the bass.dll
      e.g
      If you play an audio file using the bass.dll
      and  You changed the audio output
      Such as setting or removing headphones
      The sound output of bass.dll does not change
      Is there any solution
      If it exists, please give it to me
      Greetings and thanks in advance
      I apologize for my bad English
    • zetaimmersion
      By zetaimmersion
      I have 2 items (a field box and a bypass checkbox). every time the box is checked i need the field to become writable. unchecked is read only displaying some text.
      this is as far as i got as I am stuck at making it read/write toggle
      #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <EditConstants.au3> #include <WindowsConstants.au3> $hGUI = GUICreate("Test", 500, 500) Global $hCombo = GUICtrlCreateInput("", 10, 10, 200, 20, BitOR($ES_AUTOHSCROLL,$ES_READONLY)) GUICtrlSetBkColor($hCombo,0xe7e5e5) Global $cbox = GUICtrlCreateCheckbox ("", 40,50,10,20) GUICtrlSetState($cbox, $GUI_Unchecked) GUISetState() Global $sCurrCombo = "" While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cbox If GUICtrlRead($cbox) <> $sCurrCombo Then $sCurrCombo = GUICtrlRead($cbox) GUICtrlSetStyle ($hCombo, $SS_LEFTNOWORDWRAP) GUICtrlSetBkColor($hCombo,0xFFFFFF) MsgBox(0, "Choice", "PLease enter the text") EndIf EndSwitch WEnd  
    • InunoTaishou
      By InunoTaishou
      I know on other languages when you try to use a string like this
      Global $sString = "Some String" If ($sString) Then ; ... EndIf The compiler will fail because string cannot be used in an expression where it needs a bool (something along those lines). But I see instances in other languages where using $sString in an expression will work and implicitly use the length of the string (or because there is some value in that variable).
      I'm wondering why AutoIt doesn't do the same. If the $sString is used like in my example (or some other expression where it wants a bool type) implicitly use the length of the string instead of the converting to 0?
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good morning
      I'm working with a popup GUI, which shows some data, that is read-only display... On the event "click on a button", I'd like to change the style of the Input to Read-only to Editable...
      Any suggestions? Thanks  
    • Genotypek
      By Genotypek
      Hello.
      I've got a problem. I want to center my input vertically, but I don't have any single idea how to do it.
      There's my example code:
      #include <GUIConstantsEx.au3> $GUI = GUICreate("Gui", 237, 93, 192, 124) $Input1 = GUICtrlCreateInput("Center vertically isn't correct", 16, 16, 201, 62) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd What could I do?
      Greetings, "genotypek".