Results 1 to 9 of 9

Thread: [Resolved] How to prevent Command btn from taking focus?

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Resolved [Resolved] How to prevent Command btn from taking focus?

    I don't want some command buttons on my form to take the focus when they are clicked. Is there some easy way out, like setting some properties etc. I want only the depress and release effects, no dotted rectangle.
    Last edited by Pradeep1210; May 10th, 2005 at 01:33 AM. Reason: Matter resolved

  2. #2
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    Re: How to prevent Command btn from taking focus?

    you could just use a picture of a button - the picture box control wont draw any focus rects or depress.

    The harder way is to subclass the button control.
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  3. #3

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to prevent Command btn from taking focus?

    Thanks Therob
    I m using a picture box right now but it is very resource hungry. (Imagine more than 20 picture boxes instead of command buttons).
    Why I asked this quesiton was that whenever command btn has focus (dotted rectangle), the caption wraps if it doesn't fit the button width. Just want to prevent that thing as I can't increase button size. When it doesn't have focus, the caption appears OK.

  4. #4
    Hyperactive Member hassa046's Avatar
    Join Date
    May 2001
    Location
    Venlo, The Netherlands
    Posts
    336

    Re: How to prevent Command btn from taking focus?

    set the focus to a other control or form after OnClick event
    Better to regret things you did, than those you didn't
    International Intelligence

  5. #5
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    Re: How to prevent Command btn from taking focus?

    or even onmousedown
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  6. #6

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to prevent Command btn from taking focus?

    I can't.
    Their GotFocus event will get fired which would create unexpected problems!

  7. #7
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: How to prevent Command btn from taking focus?

    Create a texbox called txtTakeFocus.
    Form_Load should move it's left to -900 (there but not 'see a ble')

    In your button click event use
    VB Code:
    1. txtTakeFocus.SetFocus
    Rob C

  8. #8
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: How to prevent Command btn from taking focus?

    I needed to do the same thing and here is what I found:

    In a Module:
    VB Code:
    1. Option Explicit
    2.  
    3. 'declare external procedures
    4.     Public Declare Function SetWindowLong Lib "user32" _
    5.                                         Alias "SetWindowLongA" _
    6.                                         (ByVal hwnd As Long, _
    7.                                         ByVal nIndex As Long, _
    8.                                         ByVal dwNewLong As Long) As Long
    9.    
    10.     Public Declare Function CallWindowProc Lib "user32" _
    11.                                         Alias "CallWindowProcA" _
    12.                                         (ByVal lpPrevWndFunc As Long, _
    13.                                         ByVal hwnd As Long, _
    14.                                         ByVal uMsg As Long, _
    15.                                         ByVal wParam As Long, _
    16.                                         ByVal lParam As Long) As Long
    17.                                            
    18. 'declare constants
    19.     Public Const GWL_WNDPROC            As Long = (-4)
    20.  
    21. 'declare local variables
    22.     Private WndProcOrig                 As Long
    23.  
    24. Public Function BtnWndProc(ByVal hwndBtn As Long, _
    25.                             ByVal wMsg As Long, _
    26.                             ByVal wParam As Long, _
    27.                             ByVal lParam As Long) As Long
    28.    
    29.     If wMsg = 7 Then
    30.         BtnWndProc = 0
    31.         Exit Function
    32.     Else
    33.         BtnWndProc = CallWindowProc(WndProcOrig, hwndBtn, wMsg, wParam, lParam)
    34.     End If
    35. End Function
    36.  
    37. Public Sub SubClassBtn(ByVal hwndBtn As Long)
    38.     WndProcOrig = SetWindowLong(ByVal hwndBtn, GWL_WNDPROC, AddressOf BtnWndProc)
    39. End Sub
    40.  
    41. Public Sub UnSubclassBtn(ByVal hwndBtn As Long)
    42.     Call SetWindowLong(hwndBtn, GWL_WNDPROC, WndProcOrig)
    43.     WndProcOrig = 0
    44. End Sub

    On your Form in the Form Load Event:
    VB Code:
    1. Private Sub Form_Load()
    2.        Me.Command1.TabStop = False
    3.        SubClassBtn Me.Command1.hwnd
    4. End Sub

    The command button will not receive the focus.
    Last edited by Mark Gambo; May 10th, 2005 at 06:14 AM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  9. #9

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to prevent Command btn from taking focus?

    Thank u all for the replies.

    RobCrombie, Ur method works OK. Only the problem is when the mouse is down, the button still looks odd.
    I think I should stick to what I m doing at present (pictureboxes) or subclass the buttons (the harder way). Mark Gambo's code is really helpful and working.

    Matter closed

    Pradeep

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width