Announcement

Collapse
No announcement yet.

How to get KillFocus when Enter is pressed

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get KillFocus when Enter is pressed

    I need to edit what the operator enters into a text box. I only need to do this when they leave the text box. I have been using KillFocus to trap the movement out of the text box, as when the TAB key is pressed. However, when the operator presses the ENTER key or clicks on a button in the form, I don't get the KillFocus message for the text box control.

    I expect there a something very simple I am missing here but I am not sure the best way to approach it. Any ideas?

    Thnx,

    Jim...

  • #2
    Are you trapping the keystroke in a subclass procedure, or in the dialog callback procedure?
    Adam Drake
    PowerBASIC

    Comment


    • #3
      <ENTER> does not lose the focus, it posts (or maybe sends) WM_COMMAND/IDOK to current default button; or the edit control uses or eats it depending on styles. (code not shown).

      A button should be getting the focus causing the edit control to lose same. I assume you are testing for WM_COMMAND/EN_KILLFOCUS, not WM_KILLFOCUS? (code not shown)
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Dialog callback. I was hoping to be able to contain the calls to the editing logic within each particular text box control's logic in the callback. I am not familiar with sub-classing.

        Comment


        • #5
          Yes Michael, "en". Should I be testing for "wm"?

          Comment


          • #6
            Oops, I need to rephrase that...I do get en_killfocus when a button is clicked, but I don't get en_killfocus when the operator presses the Enter key while in the text box and that is what I need to trap so that I can run the edit logic.

            Comment


            • #7
              Here is a working sample that I believe came from this forum a few years ago. I don't know who posted it. It looks like it was generated using Chris Boss's freeware ddt dialog designer, which by the way, is a real cool tool. Thanks Chris!

              Code:
              ' *************************************************************
              '       Demo code Data Entry
              ' *************************************************************
              
              #Compile Exe "data_entry_keytrap.exe"
              #Register None
              #Dim All         
              
               %NOANIMATE    = 1
               %NOBUTTON     = 1
               %NOCOMBO      = 1
               %NODRAGLIST   = 1
               %NOHEADER     = 1
               %NOIMAGELIST  = 1
               %NOLIST       = 1
               %NOTRACKBAR   = 1
              
              %USEMACROS = 1
              #Include "win32api.inc"   
              #Include "commctrl.inc"  
              
              ' *************************************************************
              ' Constants and Declares
              ' *************************************************************
              
              Declare Sub Do_InitFonts()
              Declare Sub Do_DeleteFonts()
              Declare Sub Do_FixSize(ByVal hDlg&, ByVal Style&, ByVal HasMenu&)
              Declare Function Do_IsTooltip(ByVal lParam As Long) As Long
              Declare Function Do_TooltipID(ByVal lParam As Long) As Long
              Declare Sub Do_SetTooltipText(ByVal lParam As Long, TipText$)
              Declare Function Do_IsTab(ByVal lParam As Long) As Long
              Declare Function Do_TabID(ByVal lParam As Long) As Long
              Declare Function Do_TabNum(ByVal lParam As Long) As Long
              Declare Sub Do_AddTabs(ByVal hDlg As Long, ByVal IDNum&, ByVal TabText$)
              Declare Sub Do_DefColors()
              Declare Sub Do_DeleteBrushes()
              Declare Function Do_QBColor(N&) As Long
              Declare Sub Do_ShowControl(ByVal hWnd&, ByVal ID&, ByVal SFlag&)
              
              ' *************************************************************
              '              Application Constants and Declares
              ' *************************************************************
              
              %FRM1_LABEL8             = 97
              %FRM1_LABEL7             = 96
              %FRM1_LABEL6             = 95
              %FRM1_LABEL5             = 94
              %FRM1_LABEL4             = 93
              %FRM1_L3                 = 92
              %FRM1_L2                 = 91
              %FRM1_L1                 = 90
              %FRM1_TXT1               = 100
              %FRM1_TXT2               = 101
              %FRM1_TXT3               = 102
              %FRM1_TXT4               = 103
              %FRM1_TXT5               = 104
              %FRM1_TXT6               = 105
              %FRM1_TXT7               = 106
              %FRM1_TXT8               = 107
              %FRM1_TXT9               = 108
              %FRM1_TXT10              = 109
              
              ' --------------------------------------------------
              Declare Sub ShowDialog_Frm1(ByVal hParent&)
              Declare CallBack Function Frm1_DLGPROC
              ' --------------------------------------------------
              
              'Declare CallBack Function CBF_FRM1_TXT1()
              'Declare CallBack Function CBF_FRM1_TXT2()
              'Declare CallBack Function CBF_FRM1_TXT3()
              'Declare CallBack Function CBF_FRM1_TXT4()
              'Declare CallBack Function CBF_FRM1_TXT5()
              'Declare CallBack Function CBF_FRM1_TXT6()
              'Declare CallBack Function CBF_FRM1_TXT7()
              'Declare CallBack Function CBF_FRM1_TXT8()
              'Declare CallBack Function CBF_FRM1_TXT9()
              'Declare CallBack Function CBF_FRM1_TXT10()
              Declare CallBack Function CBF_FRM1_BUTTON1()
              Declare CallBack Function CBF_FRM1_BUTTON2()
              
              ' *************************************************************
              '            Application Global Variables and Types
              ' *************************************************************
              
              Global App_Brush&()
              Global App_Color&()
              Global App_Font&()
              
              
              Global hFrm1&    ' Dialog handle
              
              ' *************************************************************
              '                    Application Entrance
              ' *************************************************************
              
              Function PbMain
                 Local Count&
                 Local CC1 As INIT_COMMON_CONTROLSEX
              
                 CC1.dwSize=SizeOf(CC1)
                 CC1.dwICC=%ICC_WIN95_CLASSES
                 InitCommonControlsEx CC1
                 Do_DefColors
                 Do_InitFonts
              
                 ShowDialog_Frm1 0
                 Do
                    Dialog DoEvents To Count&
                 Loop Until Count&=0
              
              
                 Do_DeleteBrushes
                 Do_DeleteFonts
              End Function
              
              
              ' *************************************************************
              '                    Application Dialogs
              ' *************************************************************
              
              Sub ShowDialog_Frm1(ByVal hParent&)
                 Local Style&, ExStyle&
                 Local N&, CT&        '  Variables used for Reading Data in Arrays for Listbox and Combobox
                 '   hParent& = 0 if no parent Dialog
                 Style& = %WS_POPUP Or %DS_MODALFRAME Or %WS_CAPTION Or %WS_MINIMIZEBOX Or %WS_SYSMENU Or %DS_CENTER
                 ExStyle& = 0
                 
                 Dialog New hParent&, "(Key Event Test) Press enter or shift-enter, esc to exit", 0, 0,  250,  256, Style&, ExStyle& To hFrm1&
                 Do_FixSize hFrm1&, Style&, 0
                 
                 Control Add Label, hFrm1&,  %FRM1_LABEL8,  "Label  8", 5, 153, 43, 12, %WS_CHILD Or %WS_VISIBLE Or %SS_RIGHT
                 Control Add Label, hFrm1&,  %FRM1_LABEL7,  "Label  7", 5, 133, 43, 12, %WS_CHILD Or %WS_VISIBLE Or %SS_RIGHT
                 Control Add Label, hFrm1&,  %FRM1_LABEL6,  "Label  6", 3, 113, 45, 12, %WS_CHILD Or %WS_VISIBLE Or %SS_RIGHT
                 Control Add Label, hFrm1&,  %FRM1_LABEL5,  "Label  5", 8, 94, 37, 12,  %WS_CHILD Or %WS_VISIBLE Or %SS_RIGHT
                 Control Add Label, hFrm1&,  %FRM1_LABEL4,  "Label  4", 5, 74, 43, 12,  %WS_CHILD Or %WS_VISIBLE Or %SS_RIGHT
                 Control Add Label, hFrm1&,  %FRM1_L3,  "Company:", 11, 54, 37, 12, %WS_CHILD Or %WS_VISIBLE Or %SS_RIGHT
                 Control Add Label, hFrm1&,  %FRM1_L2,  "Last:", 5, 34, 43, 12, %WS_CHILD Or %WS_VISIBLE Or %SS_RIGHT
                 Control Add Label, hFrm1&,  %FRM1_L1,  "First:", 5, 15, 43, 12,%WS_CHILD Or %WS_VISIBLE Or %SS_RIGHT
                 
                 Control Add TextBox, hFrm1&,  %FRM1_TXT1,  "", 51, 15, 77, 12, %WS_CHILD Or %WS_VISIBLE Or %ES_AUTOHSCROLL Or %WS_TABSTOP, _
                    %WS_EX_CLIENTEDGE 
                    Control Set Focus hFrm1&,  %FRM1_TXT1
                    
                 Control Add TextBox, hFrm1&,  %FRM1_TXT2,  "", 51, 34, 77, 12, %WS_CHILD Or %WS_VISIBLE Or %ES_AUTOHSCROLL Or %WS_TABSTOP, _
                    %WS_EX_CLIENTEDGE 
                 Control Add TextBox, hFrm1&,  %FRM1_TXT3,  "", 51, 54, 123, 12, %WS_CHILD Or %WS_VISIBLE Or %ES_AUTOHSCROLL Or %WS_TABSTOP, _
                    %WS_EX_CLIENTEDGE 
                 Control Add TextBox, hFrm1&,  %FRM1_TXT4,  "", 51, 74, 75, 12,  %WS_CHILD Or %WS_VISIBLE Or %ES_AUTOHSCROLL Or %WS_TABSTOP, _
                    %WS_EX_CLIENTEDGE 
                 Control Add TextBox, hFrm1&,  %FRM1_TXT5,  "", 51, 94, 75, 12,  %WS_CHILD Or %WS_VISIBLE Or %ES_AUTOHSCROLL Or %WS_TABSTOP, _
                    %WS_EX_CLIENTEDGE 
                 Control Add TextBox, hFrm1&,  %FRM1_TXT6,  "", 51, 113, 75, 12,  %WS_CHILD Or %WS_VISIBLE Or %ES_AUTOHSCROLL Or %WS_TABSTOP, _
                    %WS_EX_CLIENTEDGE 
                 Control Add TextBox, hFrm1&,  %FRM1_TXT7,  "", 51, 133, 75, 12, _
                    %WS_CHILD Or %WS_VISIBLE Or %ES_AUTOHSCROLL Or %WS_TABSTOP, _
                    %WS_EX_CLIENTEDGE 
                 Control Add TextBox, hFrm1&,  %FRM1_TXT8,  "", 51, 153, 75, 12, _
                    %WS_CHILD Or %WS_VISIBLE Or %ES_AUTOHSCROLL Or %WS_TABSTOP, _
                    %WS_EX_CLIENTEDGE 
                 Control Add TextBox, hFrm1&,  %FRM1_TXT9,  "", 51, 172, 75, 12, _
                    %WS_CHILD Or %WS_VISIBLE Or %ES_AUTOHSCROLL Or %WS_TABSTOP, _
                    %WS_EX_CLIENTEDGE 
                 Control Add TextBox, hFrm1&,  %FRM1_TXT10,  "", 51, 192, 75, 12, _
                    %WS_CHILD Or %WS_VISIBLE Or %ES_AUTOHSCROLL Or %WS_TABSTOP, _
                    %WS_EX_CLIENTEDGE 
                 Control Add "Button", hFrm1&,  %IDOK,  "&Add Contact", 51, 224, 53, 15, %WS_CHILD Or %WS_VISIBLE Or _
                                                                                               %BS_PUSHBUTTON Or %WS_TABSTOP Call CBF_FRM1_BUTTON1
                 Control Add "Button", hFrm1&,  %IDCANCEL,  "E&xit", 123, 224, 53, 15, %WS_CHILD Or %WS_VISIBLE Or _
                                                                                               %BS_PUSHBUTTON Or %WS_TABSTOP Call CBF_FRM1_BUTTON2 
                 Dialog Show Modeless hFrm1& , Call Frm1_DLGPROC
              End Sub
              
              ' *************************************************************
              ' Dialog Callback Procedure for Frm1 uses Global Handle - hFrm1&
              ' *************************************************************
              
              CallBack Function Frm1_DLGPROC
                Local c As Long, lBuf, sBuf, sText As String
              
                 Select Case CbMsg
                    ' Common Windows Messages you may want to process
                    ' -----------------------------------------------
                    Case %WM_TIMER
                    Case %WM_HSCROLL
                    Case %WM_VSCROLL
                    Case %WM_SIZE
                    Case %WM_CLOSE
                    Case %WM_DESTROY
                    Case %WM_SYSCOMMAND
                    Case %WM_PAINT
                    ' -----------------------------------------------
                    Case %WM_CTLCOLORDLG
                       If CbLParam=CbHndl Then
                          ' Dialogs colors
                          SetTextColor CbWParam, App_Color&(0)
                          SetBkColor   CbWParam, App_Color&( 25)
                          Function=App_Brush&(25)
                       End If
                    Case %WM_CTLCOLORMSGBOX , %WM_CTLCOLORBTN, %WM_CTLCOLOREDIT,_
                         %WM_CTLCOLORSTATIC, %WM_CTLCOLORSCROLLBAR, %WM_CTLCOLORLISTBOX
                       ' Control colors
                       Select Case GetDlgCtrlID(CbLParam)
                          Case Else
                             Function=0
                       End Select
                    Case %WM_NOTIFY
                       If Do_IsTooltip(CbLParam) Then
                          Select Case Do_TooltipID(CbLParam)
                             Case  %IDOK
                                Do_SetTooltipText CbLParam, "Control -  %IDOK"
                             Case  %IDCANCEL
                                Do_SetTooltipText CbLParam, "Control -  %IDCANCEL"
                             Case Else
                          End Select
                       End If
                       If Do_IsTab(CbLParam) Then
                          Select Case Do_TabID(CbLParam)
                             Case Else
                          End Select
                       End If
                    Case %WM_COMMAND
                       Select Case CbCtl
                          Case %IDOK   ' <- Ok or Enter key was pressed
                              If CbCtlMsg = %BN_CLICKED Then
                                  Select Case GetDlgCtrlID(GetFocus) 'Which control has focus?
                                     Case %FRM1_TXT1 To %FRM1_TXT10  'If a textbox, move focus
                                         If (GetKeyState(%VK_SHIFT) And &H8000) = 0 Then
                                             SetFocus GetNextDlgTabItem(CbHndl, GetFocus, 0)
                                         Else 'Shift + Enter = move to previous control
                                             SetFocus GetNextDlgTabItem(CbHndl, GetFocus, 1)
                                         End If
                                     Case %IDOK  ' If Ok button has focus
                                         ' In a real code, this is a good place to store the result,
                                         ' but in this demo we just test it in a MSGBOX.
                                         For c = %FRM1_TXT1 To %FRM1_TXT10
                                             Control Get Text CbHndl, c-10 To lBuf
                                             Control Get Text CbHndl, c To sBuf
                                             sBuf = lBuf & "= " &  sBuf
                                             sText = sText + sBuf + $CrLf
                                         Next
                                         MsgBox sText, %MB_TASKMODAL, "TextBox contents"
                                     Case %IDCANCEL  ' If Cancel button has focus
                                         Dialog End CbHndl, 0   '<- End prog
                                     End Select
                            End If
                          '--------------------------------------------------------------
                          Case %IDCANCEL  ' <- Cancel or Esc key was pressed
                              If CbCtlMsg = %BN_CLICKED Then
                                  Dialog End CbHndl, 0   '<- End prog
                              End If
                          '--------------------------------------------------------------
                          End Select
                    Case Else
                 End Select
              End Function
              
              ' *************************************************************
              '
              ' *************************************************************
              
              Sub Do_InitFonts()
                 ReDim App_Font(0 To 5)
                 App_Font(0)=GetStockObject(%SYSTEM_FONT)
                 App_Font(1)=GetStockObject(%SYSTEM_FIXED_FONT)
                 App_Font(2)=GetStockObject(%ANSI_VAR_FONT)
                 App_Font(3)=GetStockObject(%ANSI_FIXED_FONT)
                 App_Font(4)=GetStockObject(%DEFAULT_GUI_FONT)    ' MS Sans Serif
                 App_Font(5)=GetStockObject(%OEM_FIXED_FONT)      ' Terminal Font
                 End Sub
              
              ' -------------------------------------------------------------
              
              Sub Do_DeleteFonts()
                 Local N&
                 ' Fonts 0 to 5 do not need to be deleted
                 For N&=6 To UBound(App_Font)
                    If App_Font(N&)<>0 Then DeleteObject App_Font(N&)
                 Next N&
                 End Sub
              
              ' -------------------------------------------------------------
              
              Sub Do_FixSize(ByVal hDlg&, ByVal Style&, ByVal HasMenu&)
                 Local X&, Y&, W&, H&, XX&, YY&
                 Dialog Get Size hDlg& To X&, Y&
                 If (Style& And %WS_CAPTION) = %WS_CAPTION Then
                    If HasMenu& Then
                       H&=H&+GetSystemMetrics(%SM_CYCAPTION)
                    End If
                 End If
                 If (Style& And %WS_HSCROLL) = %WS_HSCROLL Then
                    H&=H&+GetSystemMetrics(%SM_CYHSCROLL)
                 End If
                 If (Style& And %WS_VSCROLL) = %WS_VSCROLL Then
                    W&=W&+GetSystemMetrics(%SM_CYVSCROLL)
                 End If
                 Dialog Pixels hDlg&, W&, H& To Units XX&, YY&
                 X&=X&+XX&
                 Y&=Y&+YY&
                 Dialog Set Size hDlg&, X&, Y&
              End Sub
              
              ' -------------------------------------------------------------
              
              Function Do_IsTooltip(ByVal lParam As Long) As Long
                 Local pNM As NMHDR Ptr
                 pNM=lParam
                 If @pNM.code=%TTN_NEEDTEXT Then Function=1 Else Function=0
              End Function
              
              ' -------------------------------------------------------------
              
              Function Do_TooltipID(ByVal lParam As Long) As Long
                 Local pNM As NMHDR Ptr, pTT As TOOLTIPTEXT Ptr
                 Local IDNum&, UF&
                 IDNum&=0
                 pNM=lParam
                 If @pNM.code=%TTN_NEEDTEXT Then
                    ' Check for Tooltip message
                    pTT=lParam
                    UF&[email protected] And %TTF_IDISHWND
                    If UF&=%TTF_IDISHWND Then
                       IDNum&=GetDlgCtrlID(@pTT.hdr.idfrom)
                    Else
                       IDNum&[email protected]
                    End If
                 End If
                 Function=IDNum&
              End Function
              
              ' -------------------------------------------------------------
              
              Sub Do_SetTooltipText(ByVal lParam As Long, TipText$)
                 Local pNM As NMHDR Ptr, pTT As TOOLTIPTEXT Ptr
                 pNM=lParam
                 If @pNM.code=%TTN_NEEDTEXT Then
                    ' Check for Tooltip message
                    pTT=lParam
                    If TipText$<>"" Then
                       @pTT.szText=Left$(TipText$, 79)+Chr$(0)
                    End If
                 End If
              End Sub
              
              ' -------------------------------------------------------------
              
              Function Do_IsTab(ByVal lParam As Long) As Long
                 Local pNM As NMHDR Ptr
                 pNM=lParam
                 If @pNM.code=%TCN_SELCHANGE Then Function=1 Else Function=0
              End Function
              
              ' -------------------------------------------------------------
              
              Function Do_TabID(ByVal lParam As Long) As Long
                 Local pNM As NMHDR Ptr
                 Local IDNum&
                 pNM=lParam
                 If @pNM.code=%TCN_SELCHANGE Then
                    IDNum&[email protected]
                 End If
                 Function=IDNum&
              End Function
              
              ' -------------------------------------------------------------
              
              Function Do_TabNum(ByVal lParam As Long) As Long
                 Local RV&, pNM As NMHDR Ptr
                 Local hCtrl As Long
                 pNM=lParam
                 If @pNM.code=%TCN_SELCHANGE Then
                 [email protected]
                    RV&=SendMessage(hCtrl, %TCM_GETCURSEL, 0, 0)+1
                 End If
                 Function=RV&
              End Function
              
              ' -------------------------------------------------------------
              
              Sub Do_AddTabs(ByVal hDlg As Long, ByVal IDNum&, ByVal TabText$)
                 Local pItem As TC_ITEM, hCtrl&
                 Local zText As Asciiz*80
                 Local D$, P&, TB&
                 If IDNum&<>0 Then
                    hCtrl&=GetDlgItem(hDlg,IDNum&)
                    If hCtrl&<>0 Then
                       TB&=0
                       Do
                          If TabText$="" Then Exit Do
                          P&=InStr(TabText$,"|")
                          If P&>0 Then
                             D$=Left$(TabText$,P&-1)
                             TabText$=Mid$(TabText$,P&+1)
                          Else
                             D$=TabText$
                             If TabText$="" Then Exit Do
                             TabText$=""
                          End If
                          pItem.Mask=%TCIF_TEXT
                          zText=D$+Chr$(0)
                          pItem.pszText=VarPtr(zText)
                          SendMessage hCtrl&, %TCM_INSERTITEM, TB&, VarPtr(pItem)
                          TB&=TB&+1
                       Loop
                    End If
                 End If
              End Sub
              
              ' -------------------------------------------------------------
              
              Sub Do_DefColors()
                 Local T&
                 ReDim App_Brush&(0 To 31)
                 ReDim App_Color&(0 To 31)
                 For T&=0 To 31
                    App_Brush&(T&)=CreateSolidBrush(Do_QBColor(T&))
                    App_Color&(T&)=Do_QBColor(T&)
                 Next T&
              End Sub
              
              ' -------------------------------------------------------------
              
              Sub Do_DeleteBrushes()
                 Local T&
                 For T&=0 To 31
                    DeleteObject App_Brush&(T&)
                 Next T&
              End Sub
              
              ' -------------------------------------------------------------
              
              Function Do_QBColor(N&) As Long
                 Local RV&
                 Select Case N&
                    Case 0
                    RV&=RGB(0,0,0)       ' Black
                 Case 1
                    RV&=RGB(0,0,128)     ' Blue
                 Case 2
                    RV&=RGB(0,128,0)     ' Green
                 Case 3
                    RV&=RGB(0,128,128)   ' Cyan
                 Case 4
                    RV&=RGB(196,0,0)     ' Red
                 Case 5
                    RV&=RGB(128,0,128)   ' Magenta (Purple)
                 Case 6
                    RV&=RGB(128,64,0)    ' Brown
                 Case 7
                    RV&=RGB(196,196,196) ' White
                 Case 8
                    RV&=RGB(128,128,128) ' Gray
                 Case 9
                    RV&=RGB(0,0, 255)    ' Lt. Blue
                 Case 10
                    RV&=RGB(0,255,0)     ' Lt. Green
                 Case 11
                    RV&=RGB(0,255,255)   ' Lt. Cyan
                 Case 12
                    RV&=RGB(255,0,0)     ' Lt. Red
                 Case 13
                    RV&=RGB(255,0,255)   ' Lt. magenta (Purple)
                 Case 14
                    RV&=RGB(255,255,0)   ' Yellow
                 Case 15
                    RV&=RGB(255,255,255) ' Bright White
                 Case 16   ' - Extended QB colors Pastel version -
                    RV&=RGB(164,164,164)
                 Case 17
                    RV&=RGB(128,160,255)
                 Case 18
                    RV&=RGB(160,255,160)
                 Case 19
                    RV&=RGB(160,255,255)
                 Case 20
                    RV&=RGB(255,160,160)
                 Case 21
                    RV&=RGB(255,160,255)
                 Case 22
                    RV&=RGB(255,255,160)
                 Case 23
                    RV&=RGB(212,212,212)
                 Case 24
                    RV&=RGB(180,180,180)
                 Case 25
                    RV&=RGB(188,220,255)
                 Case 26
                    RV&= RGB(220,255,220)
                 Case 27
                    RV&=RGB(220,255,255)
                 Case 28
                    RV&=RGB(255,220,220)
                 Case 29
                    RV&=RGB(255,220,255)
                 Case 30
                    RV&=RGB(255,255,220)
                 Case 31
                    RV&=RGB(228,228,228)
                 Case Else
                    RV&=RGB(0,0,0)
                 End Select
                 Function=RV&
              End Function
               ' -------------------------------------------------------------
              Sub Do_ShowControl(ByVal hWnd&, ByVal ID&, ByVal SFlag&)
                 Local hCtrl&
                 If IsWindow(hWnd&) Then
                    If ID&<>0 Then
                       hCtrl&=GetDlgItem(hWnd&,ID&)
                       If SFlag&=0 Then
                          ShowWindow hCtrl&, %SW_HIDE
                       Else
                          ShowWindow hCtrl&, %SW_SHOW
                       End If
                    End If
                 End If
              End Sub
               ' -------------------------------------------------------------
              
              ' *************************************************************
              '  Application Callback Functions (or Procedures) for Controls
              ' *************************************************************
              
              
              ' ------------------------------------------------
              CallBack Function CBF_FRM1_BUTTON1
                 If CbCtlMsg=%BN_CLICKED Then
              
                 End If
                 If CbCtlMsg=%BN_SETFOCUS Then
              
                 End If
                 If CbCtlMsg=%BN_KILLFOCUS Then
              
                 End If
              End Function
              ' ------------------------------------------------
              
              ' ------------------------------------------------
              CallBack Function CBF_FRM1_BUTTON2
                 If CbCtlMsg=%BN_CLICKED Then
              
                 End If
                 If CbCtlMsg=%BN_SETFOCUS Then
              
                 End If
                 If CbCtlMsg=%BN_KILLFOCUS Then
              
                 End If
              End Function
              ' ------------------------------------------------
              The most exasperating part of the "rat race" is how often the rats are in the lead!

              Comment


              • #8
                nother example posted long ago by B Hagsten modified to show message ids in dialog title
                Code:
                '
                ' Small sample of data input form. Textbox in focus is framed.
                ' Enter-key moves focus to next textbox, and closes dialog if
                ' Exit button has focus. Just to show some useful API tricks..
                ' Public Domain by Borje Hagsten, May 2003.
                '
                ' Declares
                '--------------------------------------------------------------------
                #COMPILE EXE
                #INCLUDE "WIN32API.INC"
                '--------------------------------------------------------------------
                %IDC_LABEL1 = 140
                %IDC_TEXT1 = 141
                %IDC_LABEL2 = 142
                %IDC_TEXT2 = 143
                %IDC_LABEL3 = 144
                %IDC_TEXT3 = 145
                '--------------------------------------------------------------------
                DECLARE CALLBACK FUNCTION DlgProc() AS LONG
                DECLARE SUB FrameControlInFocus (BYVAL hDlg AS DWORD, BYVAL hWnd AS DWORD, _
                BYVAL clr AS DWORD)
                
                '
                ' Program entrance
                '--------------------------------------------------------------------
                FUNCTION WINMAIN (BYVAL hInst AS DWORD, BYVAL hPrevInstance AS DWORD, _
                BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG
                
                LOCAL hDlg AS DWORD
                
                DIALOG NEW 0, "Data Input",,, 220, 120, _
                %WS_CAPTION OR %WS_SYSMENU, 0 TO hDlg
                
                CONTROL ADD LABEL, hDlg, %IDC_LABEL1, "&Name", 6, 7, 50, 9
                CONTROL ADD TEXTBOX, hDlg, %IDC_TEXT1, "", 5, 17, 100, 13
                
                CONTROL ADD LABEL, hDlg, %IDC_LABEL2, "&City", 6, 34, 50, 9
                CONTROL ADD TEXTBOX, hDlg, %IDC_TEXT2, "", 5, 44, 100, 13
                
                CONTROL ADD LABEL, hDlg, %IDC_LABEL3, "&Phone", 6, 61, 50, 9
                CONTROL ADD TEXTBOX, hDlg, %IDC_TEXT3, "", 5, 71, 100, 13
                
                CONTROL ADD BUTTON, hDlg, %IDCANCEL, "E&xit", 164, 101, 50, 14
                
                DIALOG SHOW MODAL hDlg, CALL DlgProc
                
                END FUNCTION
                
                '
                ' Main Dialog procedure
                '--------------------------------------------------------------------
                CALLBACK FUNCTION DlgProc() AS LONG
                
                SELECT CASE CBMSG
                    CASE %WM_INITDIALOG
                        STATIC sthText AS DWORD, stCol AS LONG 'to store handle and color for %WM_PAINT
                
                    CASE %WM_PAINT 'must be able to repaint if dialog has
                        BEEP
                        IF sthText THEN 'been covered/uncovered by something else.
                            FrameControlInFocus CBHNDL, sthText, stCol
                        END IF
                
                    CASE %WM_COMMAND
                        SELECT CASE CBCTL
                            CASE %IDC_TEXT1 TO %IDC_TEXT3 'textboxes only
                                SELECT CASE CBCTLMSG
                                    CASE %EN_SETFOCUS 'a textbox has got focus, draw frame
                                        DIALOG SET TEXT CBHNDL, "EN_SETFOCUS"
                                        stCol = RGB(255,0,0) 'store color and handle for eventual %WM_PAINT
                                        sthText = CBLPARAM
                                        FrameControlInFocus CBHNDL, sthText, stCol
                
                                    CASE %EN_KILLFOCUS 'we are leaving a textbox, so wipe out drawn rect
                                        DIALOG SET TEXT CBHNDL, "EN_KILLFOCUS"
                                        FrameControlInFocus CBHNDL, CBLPARAM, GetSysColor(%COLOR_3DFACE)
                                        sthText = 0 'reset handle, we don't need to repeat this under WM_PAINT
                                END SELECT
                
                            CASE %IDOK 'Enter key triggers IDOK in dialogs - let's use that fact..
                                DIALOG SET TEXT CBHNDL, "IDOK"
                                DIALOG REDRAW CBHNDL
                                SLEEP 1000
                                IF GetDlgCtrlId(GetFocus) = %IDCANCEL THEN 'if exit button
                                    DIALOG END CBHNDL, 0 'whatever..
                                    ELSE 'else textbox - move to next
                                    SetFocus GetNextDlgTabItem(CBHNDL, GetFocus, 0)
                                END IF
                
                            CASE %IDCANCEL
                                DIALOG SET TEXT CBHNDL, "IDCANCEL"
                                IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                                    DIALOG END CBHNDL, 0
                                END IF
                        END SELECT
                
                    END SELECT
                
                END FUNCTION
                
                '
                ' Draw a rectangle around any given control
                '--------------------------------------------------------------------
                SUB FrameControlInFocus (BYVAL hDlg AS DWORD, BYVAL hWnd AS DWORD, BYVAL clr AS DWORD)
                    LOCAL hDC AS DWORD, hBrush AS DWORD, hPen AS DWORD, rc AS RECT
                
                    GetWindowRect hWnd, rc 'get control's pos and size on screen
                    MapWindowPoints 0, hDlg, rc, 2 'map rect to dialog
                    InflateRect rc, 2, 2 'increase slightly to draw around control
                
                    hDC = GetDc(hDlg) 'use dialog's DC since we want to paint on dialog
                    hPen = CreatePen(%PS_SOLID, 1, clr) 'create a pen for given color
                    hPen = SelectObject(hDC, hPen) 'select the new pen into the DC
                    hBrush = SelectObject(hDC, GetStockObject(%NULL_BRUSH)) 'use stock null brush for hollow rect
                
                    Rectangle hDC, rc.nLeft, rc.nTop, rc.nRight, rc.nBottom 'draw the rectangle
                
                    SelectObject hDC, hBrush 'return original pen and brush, then release DC
                    DeleteObject SelectObject(hDC, hPen)
                    ReleaseDc hDlg, hDC
                
                END SUB

                Comment


                • #9
                  but I don't get en_killfocus when the operator presses the Enter key while in the text box and that is what I need to trap so that I can run the edit logic.
                  As I said, in a single-line edit control (code still not shown), <ENTER> results in WM_COMMAND/IDOK. Trap that and do your thing when it happens.

                  If you have no default button, that may not happen. In this case you'll need to subclass because other than WM_COMMAND/IDOK, the <Enter> key is processed by the control itself.

                  You want to advance one tab position from current?
                  Code:
                      SetFocus GetNextDlgTabItem (GetFocus(), %FALSE)
                  .. which, by the way, will result in an EN_KILLFOCUS message, so careful not to do things twice.

                  One last thing: <ENTER> is not a usual (Windows) key to "end editing of current text box." If you want that behavior anyway, you may do better in the long run to just go ahead and dig into subclassing, at least to handle the TAB, SHIFT+TAB and ENTER keys.

                  MCM
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    Thank you everyone!
                    Lots for me to work with here.

                    Have a great weekend everyone!

                    Jim...

                    Comment

                    Working...
                    X