Announcement

Collapse
No announcement yet.

Combobox, auto-selection of text

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

  • Combobox, auto-selection of text

    Guys --
    I want to prevent standart selection of text as result of Tab.
    ComboBox has %CBS_DROPDOWNLIST Or %WS_TABSTOP styles.
    With textboxes all is clear
    Code:
             Case %WM_COMMAND
                If (CbCtlMsg = %EN_KILLFOCUS) Or (CbCtlMsg = %EN_SETFOCUS) Then
                   SendMessage GetDlgItem(CbHndl, CbCtl), %EM_SETSEL, -1, -1
                   InvalidateRect CbLparam, ByVal 0&, 1
                   Exit Function
                End If
    But what to do with combobox ?

    ------------------
    E-MAIL: [email protected]

  • #2
    I am assuming from the use of the WS_TABSTOP style that you want the
    combo box to get the keyboard focus but without hiliting the current
    selection. Hmm...
    If the style was CBS_DROPDOWN then a technique similar to the one used
    for edit controls will work. Unfortunately, combo boxes created with the
    CBS_DROPDOWNLIST style have no edit control. The area of the combobox that
    displays the current selection is in the combobox window itself.
    You propably need to use an owner drawn combo box.


    ------------------
    Dominic Mitchell
    Dominic Mitchell
    Phoenix Visual Designer
    http://www.phnxthunder.com

    Comment


    • #3
      Tricky one. Subclassing and returning zero in combo's WM_SETFOCUS
      should do it, but I think you also lose possibility to drop the list
      with the mouse then. Ownerdrawn combo is probably the only way to
      do it in a good way, since you still can provide a FocusRect, without
      selection color. Quick test that works:
      Code:
      '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
      ' Template - Ownerdrawn ComboBox..
      '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
      #COMPILE EXE
      #INCLUDE "WIN32API.INC"
       
      %ID_LABEL1 = 100
      %ID_COMBO1 = 121
      %ID_COMBO2 = 122
       
      DECLARE CALLBACK FUNCTION DlgProc
       
      '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
      ' Entrance - Create dialog and controls
      '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
      FUNCTION PBMAIN
        LOCAL hDlg AS LONG, I AS LONG, hCombo AS LONG
        REDIM arr(15) AS STRING
       
        DIALOG NEW 0, "Press Tab or F4..", , , 120, 80, %WS_CAPTION OR %WS_SYSMENU TO hDlg
       
        'Create items for list
        FOR I = 0 TO 15 : arr(I) = "CB1, item no:" & STR$(I) : NEXT
        CONTROL ADD COMBOBOX, hDlg, %ID_COMBO1, arr(), 10, 10, 100, 120, _
                    %CBS_OWNERDRAWFIXED OR %CBS_HASSTRINGS OR %CBS_DROPDOWNLIST OR _
                    %WS_TABSTOP OR %CBS_DISABLENOSCROLL OR %WS_VSCROLL, %WS_EX_CLIENTEDGE
       
        FOR I = 0 TO 15 : arr(I) = "CB2, item no:" & STR$(I) : NEXT
        CONTROL ADD COMBOBOX, hDlg, %ID_COMBO2, arr(), 10, 40, 100, 120, _
                    %CBS_OWNERDRAWFIXED OR %CBS_HASSTRINGS OR %CBS_DROPDOWNLIST OR _
                    %WS_TABSTOP OR %CBS_DISABLENOSCROLL OR %WS_VSCROLL, %WS_EX_CLIENTEDGE
       
        CONTROL ADD BUTTON, hDlg, %IDOK, "&Close", 30, 62, 60, 14
       
        CONTROL SEND hDlg, %ID_COMBO1, %CB_SETCURSEL,  5, 0  'Select an item, like 5..
        CONTROL SEND hDlg, %ID_COMBO2, %CB_SETCURSEL, 10, 0  'Select an item, like 10..
        DIALOG SHOW MODAL hDlg, CALL DlgProc
      END FUNCTION
       
      '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
      ' Main dialog callback
      '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
      CALLBACK FUNCTION DlgProc
        SELECT CASE CBMSG
           CASE %WM_COMMAND
              SELECT CASE CBCTL
                 CASE %IDOK : DIALOG END CBHNDL       'Exit
       
                 CASE %ID_COMBO1
                    IF HIWRD(CBWPARAM) = %CBN_SELENDOK THEN
                       'Whatever..
                    END IF
       
                 CASE %ID_COMBO2
                    IF HIWRD(CBWPARAM) = %CBN_SELENDOK THEN
                    END IF
              END SELECT
       
           CASE %WM_DRAWITEM
              IF CBWPARAM = %ID_COMBO1 OR CBWPARAM = %ID_COMBO2 THEN
                 LOCAL hBrush AS LONG, hBrushOld AS LONG
                 LOCAL lpdis AS DRAWITEMSTRUCT PTR, zTxt AS ASCIIZ * 120 '<- increase if you need more..
       
                 lpdis = CBLPARAM
                 IF @lpdis.itemID = -1 THEN EXIT FUNCTION
       
                 SELECT CASE @lpdis.itemAction
                    CASE %ODA_DRAWENTIRE, %ODA_SELECT
                       'CLEAR BACKGROUND
                       hBrush = CreateSolidBrush(GetSysColor(%COLOR_WINDOW)) 'Create a background brush
                       hBrushOld = SelectObject(@lpdis.hDC, hBrush)      'Select brush into device context
                       CALL FillRect(@lpdis.hDC, @lpdis.rcItem, hBrush)  'Paint background color rectangle
                       CALL SelectObject(@lpdis.hDC, hBrushOld)          'Select old brush back
                       CALL DeleteObject(hBrush)                         'Delete brush
       
                       'DRAW TEXT
                       CALL SetBkColor(@lpdis.hDC, GetSysColor(%COLOR_WINDOW))             'Set text Background
                       CALL SetTextColor(@lpdis.hDC, GetSysColor(%COLOR_WINDOWTEXT))       'Set text color
                       CONTROL SEND CBHNDL, CBWPARAM, %CB_GETLBTEXT, @lpdis.itemID, VARPTR(zTxt)  'Get text
                       DrawText @lpdis.hDC, zTxt, -1, @lpdis.rcItem, %DT_SINGLELINE OR %DT_LEFT OR %DT_VCENTER
       
                       'SELECTED ITEM
                       IF (@lpdis.itemState AND %ODS_SELECTED) THEN      'if selected
                             'CALL InvertRect(@lpdis.hDC, @lpdis.rcItem)     'invert area around text only
                          CALL DrawFocusRect(@lpdis.hDC, @lpdis.rcItem)  'and draw a focus rectangle around all
                       END IF
       
                    CASE %ODA_FOCUS                                      'for selction in dropped list
                       CALL InvertRect(@lpdis.hDC, @lpdis.rcItem)     'invert area around text only
                 END SELECT
                 FUNCTION = %TRUE : EXIT FUNCTION
              END IF
        END SELECT
       
      END FUNCTION

      ------------------

      Comment


      • #4
        Semen,
        You quite often use a global hook

        After all the message pump of the dialogbox is the reason.
        Maybe the best spot to remove the setfocus message??


        ------------------
        hellobasic

        Comment


        • #5
          Sorry


          [This message has been edited by Fred Oxenby (edited April 01, 2001).]
          Fred
          mailto:[email protected][email protected]</A>
          http://www.oxenby.se

          Comment


          • #6
            Guys --
            Thanks for replies.

            Ownerdrawn, of course, works fine. But a code is a little large.

            WM_SETFOCUS... I tried, but results are "too good" (combobox doesn't work correctly).
            Probably, somebody will give a new idea. If not - I'll follow Dominic's / Borje's suggestion.



            ------------------
            E-MAIL: [email protected]

            Comment


            • #7
              Aren't we a funny bunch of people? I actually reacted same way Semen,
              thinking there must be an easier way = a couple of hundred bytes less
              code. Then I realized, in another thread they are discussing VB NjET
              and HelloWorld app's that hardly fits on a CD anymore..

              Adwantage of Ownerdrawn, like in sample above - you can do so many
              more things with it, like using different colors/fonts for different
              items, including bitmaps, draw text centered, right-aligned, indented, etc.

              Only a few hundred bytes extra, and your users will be amazed..


              ------------------

              Comment

              Working...
              X