Announcement

Collapse
No announcement yet.

Focus Question

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

  • Focus Question

    I am using 'Copy' and 'Paste' as menu items along with the CLIPBOARD Statements to copy and/or paste. When I goto the menu my text boxes loose focus to the menu items. How do I determine the focus of where I am copying from and where I am pasting to.

  • #2
    You'd expect to lose focus when a menu is clicked.

    If you have multiple controls on one screen where copy/paste are valid, you can ..

    A) change to a context menu (right click in an edit control)
    B) Keep track (STATIC or GLOBAL variable, DIALOG/CONTROL SET USER value) of the last qualifying control to obtain the focus so you know the source or destination
    C) change the menu to offer either via a top level or second level option to direct a specific control be used for the operation... eg
    Code:
    File     Paste 
                      Name 
                      Addresss 
                      City 
                      State 
                      Zip
    Me, I like the context menu idea. I think that would be more consistent with other Windows' applications.

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

    Comment


    • #3
      David,

      Here's a compilable example of the context menu that MCM suggests.

      http://www.garybeene.com/power/code/..._gbs_00129.htm

      Comment


      • #4
        If you are using edit controls the default control provides a context menu with select/copy/paste/cut options.

        I would think it would work with "CONTROL ADD TEXTBOX" controls 'out of the box.' Just right-click when focused.
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Thanks for your help. I am using a global bariable to catch the last box in focus but since it is often recommened to ot use global variables I thought there might be a spceific comman to obtain the previous focused box. Thanks again for your help.

          David Hoff Jr

          Comment


          • #6
            DDT TEXTBOXes do support all the normal Cut Copy, Paste functions - hot keys (Ctrl-C etc) as well as popup context menu functions (You would need to subclass the Textboxes to intercept context menu messages).

            Here's some code to add regular Menu support. It also uses accelerator keys which can be useful (don't need subclassing - not that there is anything wrong with that ) Note the use of (non standard) Ctrl+P for 'Paste'
            Code:
            #COMPILE EXE
            #DIM All
            #INCLUDE "WIN32API.INC"
             
            %IDCANCEL         =    2
            %IDC_TEXTBOX1     = 1001
            %IDC_TEXTBOX2     = 1002
            %IDM_EDIT_COPY    = 1004
            %IDM_EDIT_PASTE   = 1006
             
            FUNCTION PbMain()
              MainDialog %HWND_DESKTOP
            END FUNCTION
            '------------------/PbMain
             
            FUNCTION AttachMENU(BYVAL hDlg AS DWORD) AS DWORD
             LOCAL hMenu   AS DWORD
             LOCAL hPopUp1 AS DWORD
             
             MENU NEW BAR TO hMenu
              MENU NEW POPUP TO hPopUp1
                MENU ADD POPUP, hMenu, "&File", hPopUp1, %MF_ENABLED
                MENU ADD STRING, hPopUp1, "Exit", %IDCANCEL, %MF_ENABLED
              MENU NEW POPUP TO hPopUp1
                MENU ADD POPUP, hMenu, "&Edit", hPopUp1, %MF_ENABLED
                MENU ADD STRING, hPopUp1, "&Copy" + $TAB + "Ctrl+C", %IDM_EDIT_COPY, %MF_ENABLED
                MENU ADD STRING, hPopUp1, "&Paste" + $TAB + "Ctrl+P", %IDM_EDIT_PASTE, %MF_ENABLED
             
              MENU ATTACH hMenu, hDlg
             FUNCTION = hMenu
            END FUNCTION
            '------------------/AttachMenu
             
            CALLBACK FUNCTION MainProc()
             LOCAL sClip As String, Res AS LONG 
              SELECT CASE AS LONG CBMSG
                CASE %WM_INITDIALOG
                  Control Set Focus CbHndl, %IDC_TEXTBOX2
                  Control Send CbHndl, %IDC_TEXTBOX2, %EM_SETSEL, 0, -1
             
                CASE %WM_COMMAND
                  SELECT CASE AS LONG CBCTL
                    CASE %IDCANCEL
                      Dialog End CbHndl
             
                    CASE %IDM_EDIT_COPY
                      Clipboard Reset
                      Control Send CbHndl, GetDlgCtrlID(GetFocus), %WM_COPY, 0, 0
                      Clipboard Get Text sClip, Res
                      IF Res THEN 
                        Dialog Set Text CbHndl, sClip
                      ELSE
                        Dialog Set Text CbHndl, "No text seleted"
                      END IF
                      ' MessageBox CbHndl, "Focus", "Test", %MB_OK
             
             '          STATIC hWndSaveFocus AS DWORD               '# Un-Comment these Three lines
             '          hWndSaveFocus = GetFocus()                  '# to avoid shifting focus
                      Dialog2 (CbHndl)                            
             '          SetFocus(hWndSaveFocus)                     '# when Dialog2 is closed 
             
                    CASE %IDM_EDIT_PASTE
                      Control Send CbHndl, GetDlgCtrlID(GetFocus), %WM_PASTE, 0, 0
             
                  END SELECT
              END SELECT
            END FUNCTION
            '------------------/MainProc
             
            FUNCTION MainDialog(BYVAL hParent AS DWORD) AS LONG
             LOCAL hDlg AS DWORD
             
              DIALOG NEW hParent, "Clipboard monitored here (Esc to close)", 70, 58, 201, 157, TO hDlg
                CONTROL ADD TEXTBOX, hDlg, %IDC_TEXTBOX1, "TextBox1", 25, 25, 135, 40
                CONTROL ADD TEXTBOX, hDlg, %IDC_TEXTBOX2, "TextBox2", 25, 70, 135, 40
             
              AttachMENU hDlg
              AttachAccel hDlg
             
              DIALOG SHOW MODAL hDlg, CALL MainProc
             
            END FUNCTION
            '------------------/MainDialog
             
            FUNCTION AttachAccel(BYVAL hDlg AS DWORD) AS DWORD
             LOCAL hAccel   AS DWORD
             LOCAL tAccel() AS ACCELAPI
             DIM   tAccel(1 TO 2) AS ACCELAPI
             
              AssignAccel tAccel(1), ASC("C"), %IDM_EDIT_COPY, %FVIRTKEY OR %FCONTROL OR %FNOINVERT
              AssignAccel tAccel(2), ASC("P"), %IDM_EDIT_PASTE, %FVIRTKEY OR %FCONTROL OR %FNOINVERT
              ACCEL ATTACH hDlg, tAccel() TO hAccel
             
             FUNCTION = hAccel
            END FUNCTION
            '------------------/AttachAccel
             
            FUNCTION AssignAccel(tAccel AS ACCELAPI, BYVAL wKey AS WORD, BYVAL wCmd AS WORD, BYVAL byFVirt AS BYTE) AS LONG
              tAccel.fVirt = byFVirt
              tAccel.key   = wKey
              tAccel.cmd   = wCmd
            END FUNCTION
            '------------------/AssignAccel
             
            FUNCTION DIALOG2(BYVAL hParent AS DWORD) AS LONG
             LOCAL hDlg  AS DWORD
             
              DIALOG NEW hParent, "Dialog2",,, 200, 120, TO hDlg
              CONTROL ADD BUTTON, hDlg, %IDCANCEL, "Close", 75, 45, 50, 15
             
              DIALOG SHOW MODAL hDlg, CALL DIALOG2Proc
             
            END FUNCTION
            '------------------/Dialog2
             
            CALLBACK FUNCTION DIALOG2Proc()
              SELECT CASE AS LONG CBMSG
                CASE %WM_INITDIALOG
             
                CASE %WM_COMMAND
                  SELECT CASE AS LONG CBCTL
                    CASE %IDCANCEL
                      IF CBCTLMSG = %BN_CLICKED OR CBCTLMSG = 1 THEN
                        DIALOG END CBHNDL, 0
                      END IF
             
                  END SELECT
              END SELECT
            END FUNCTION
            '------------------/Dialog2Proc
            This sample code does (now) show one way that the Textbox can loose focus when the Menu is selected. (And a fix).
            Last edited by Dave Biggs; 28 Aug 2009, 11:00 AM. Reason: sample code 'improved' - comments below
            Rgds, Dave

            Comment


            • #7
              menu's don't make a control lose it's focus.
              It may be a different technique here being used..
              hellobasic

              Comment


              • #8
                > It may be a different technique here being used..

                As in, "Allegedly failing code not shown?"
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                • #9
                  David,

                  I've modified the sample code above after playing a little more.

                  I couldn't get the keyboard focus to leave either of the textboxes by opening the Menu but I was able to get that effect by opening a child window when the menu Edit/Copy option was selected.
                  In the example above, when text is selected in TextBox2 and the Edit/Copy menu opt5ion is selected (mouse or keyboard) Dialog2 will popup. When Dialog 2 is closed the focus is shifted to TextBox1...!

                  I'm not sure why that happens. Focus is being reset to the first control in the main dialog's tab order. Strangely the same thing doesn't happen if a MessageBox (API style with parent being the MainDialog) is used instead of Dialog2 (that's what I tried first).

                  Anyhow a fix for that behaviour is included in the sample - just uncomment the three lines marked '# in the CASE %IDM_EDIT_COPY section.

                  A similar fix might work in your situation? If not, you should try to post a compilable example which demonstates the problem you are having.
                  Rgds, Dave

                  Comment

                  Working...
                  X