WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Dialog Editor version 6.2
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Focus Change

 Keywords: Focus Loss Change Event Key Keystroke WasFocusLossEvent StatusBar  

Sometimes people are suprised to find that the EditBox event (number 5) fires for every keystroke -- what they really wanted to know is when the user is DONE changing the EditBox. This can be detected by watching when the focus moves away from the control - the "focus lost" event. Then we can look at the data and see if any action is needed.

Also here is a simple and handy StatusBar function.

#DefineFunction DProc(DH,DM,DN,DE,DC)
  ; Demonstration of WasFocusLossEvent and StatusBar

  ; detect the focus loss pseudo-event
  If WasFocusLossEvent(DH,&Controlname)
    ; only if the control name starts with "EditBox"
    If StrIndex(controlname,"EditBox",1,0)
      ; what did it contain?
      t = DialogControlGet(DH, controlname, 3)
      ; put all that in the status bar
      StatusBar(2, controlname:" lost focus, contains ":t)
    EndIf
  EndIf

  ; normal event processing Switch
  Switch DM
    Case 0 ; initialization event
      DialogProcOptions(DH,1,200)    ; timer events every 200 ms
      DialogProcOptions(DH,2,@TRUE)  ; pushbutton events
      StatusBar(DH, "75,")           ; set up a two-section statusbar
      Return(-1)
    Case 1 ; timer event
      ; display the time in the status bar
      StatusBar(1, StrSub(TimeYmdHms(),12,8))
      Return(-1)
    Case 2 ; pushbutton
      Message(DN,'exiting')
      Return(-1)
  EndSwitch
  Return(-1)
#EndFunction


#DefineFunction WasFocusLossEvent(DiaHan,ptrControlname)
  ; Detects a focus loss pseudo event
  ;
  ; if function returns @TRUE, then Controlname will be
  ;   set to the control that just lost focus.  Controlname
  ;   must be passed as a pointer (using the ampersand).
  ;
  ; Be sure dialog has a timer event of 500ms or less.
  ; Add to dialog callback BEFORE normal event handling.

  ; set up to remember control
  ptrLastFocus = PtrPersistent(LastFocus,"")
  ; see what control currently has focus
  DCSTATE_GETFOCUS = 5
  ThisFocus = DialogControlState(DiaHan,0,DCSTATE_GETFOCUS,0)
  ; same control?
  If ThisFocus == *ptrLastFocus
    ; return nothing, no focus change
    Return @FALSE
  Else
    ; no previous known control?
    If *ptrLastFocus == ""
      *ptrLastFocus = ThisFocus ; reset for this new control
      Return @FALSE
    EndIf
    Terminate(IsDefined(*ptrControlname)==-1,'WasFocusLossEvent','Controlname must be pointer')
    *ptrControlname = *ptrLastFocus  ; control that lost focus
    *ptrLastFocus = ThisFocus        ; reset for this new control
    Return @TRUE
  EndIf
#EndFunction
;
; Note: the WasFocusLossEvent function requires a timer
; event because it is essentially polling to discover the
; change in focus.  It is not necessary to do anything
; with the timer event if you do not want to.  Fire the
; event every half second or less to get a snappy response
; to focus changes.
;
; Note: the WasFocusLossEvent function should be called
; ahead of the normal dialog event processing in order to
; detect and process the focus change before processing the
; event that changed it.  For example, if clicking a Save
; button changes the focus, you want to first deal with the
; focus change, then deal with the Save operation.


#DefineFunction StatusBar(N,text)
    ; Super easy status bar on winbatch dialogs
    ; (repackaged from article W15451 in the database)
    ;
    ; on first call,       N is the Dialog Handle and
    ;                      text is "width,width,width..."
    ;                      which establishes the number of sections
    ;                      and gives the width in pixels for each section
    ; on subsequent calls, N is the section number (1 based) and
    ;                      text is content for that section
    ;
    user32=StrCat(DirWindows(1),"user32.dll")
    WM_USER=1024
    SB_SETPARTS=WM_USER+4
    SB_SETTEXT=WM_USER+1
    h = PtrPersistent(handle,0) ; remember the status bar handle between calls
    If *h == 0 ; create mode?
      hinst=DllHinst("")
      style = 0
      *h = DllCall(user32,long:"CreateWindowExA",long:512,lpstr:"msctls_statusbar32",lpstr:"",long:1073741824|268435456|style,long:0,long:0,long:0,long:0,long:N,long:0,long:hinst,long:0)
      n = ItemCount(text,',')
      awidths=BinaryAlloc(n*4)
      t = 0
      For i = 0 To n-2
        x = ItemExtract(i+1,text,',')
        If !IsNumber(x) Then x = 0
        BinaryPoke4(awidths,i*4,t+x)
        t = t + x
      Next i
      BinaryPoke4(awidths,i*4,-1)
      DllCall(user32, long:"SendMessageA", long:*h, long:SB_SETPARTS, long:n, lpbinary:awidths)
    Else
      DllCall(user32,long:"SendMessageA",long:*h,long:SB_SETTEXT,long:N-1,lpstr:text)
    EndIf
#EndFunction


MyDialogFormat=`WWWDLGED,6.2`

MyDialogCaption=`WIL Dialog 1`
MyDialogX=167
MyDialogY=052
MyDialogWidth=146
MyDialogHeight=127
MyDialogNumControls=006
MyDialogProcedure=`DProc`
MyDialogFont=`DEFAULT`
MyDialogTextColor=`DEFAULT`
MyDialogBackground=`DEFAULT,DEFAULT`
MyDialogConfig=0

MyDialog001=`051,081,036,012,PUSHBUTTON,"PushButton_OK",DEFAULT,"OK",1,10,32,DEFAULT,DEFAULT,DEFAULT`
MyDialog002=`011,017,036,012,EDITBOX,"EditBox_1",ebVariable1,"A",DEFAULT,1,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog003=`071,011,036,012,EDITBOX,"EditBox_2",ebVariable2,"BB",DEFAULT,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog004=`025,045,036,012,EDITBOX,"EditBox_3",ebVariable3,"CCC",DEFAULT,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog005=`083,041,036,012,EDITBOX,"EditBox_4",ebVariable4,"DDDD",DEFAULT,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
MyDialog006=`003,105,138,012,STATICTEXT,"StaticText_1",DEFAULT,"current time                      focus loss info",DEFAULT,60,DEFAULT,DEFAULT,DEFAULT,DEFAULT`

ButtonPushed=Dialog("MyDialog")

Exit

Article ID:   W17722
Filename:   Focus Change.txt
File Created: 2011:02:08:09:47:50
Last Updated: 2011:02:08:09:47:50