|
-
May 9th, 2005, 06:25 AM
#1
[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
-
May 9th, 2005, 07:02 AM
#2
Fanatic Member
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]
-
May 9th, 2005, 07:15 AM
#3
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.
-
May 9th, 2005, 07:18 AM
#4
Hyperactive Member
Re: How to prevent Command btn from taking focus?
set the focus to a other control or form after OnClick event
-
May 9th, 2005, 07:21 AM
#5
Fanatic Member
Re: How to prevent Command btn from taking focus?
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]
-
May 9th, 2005, 07:32 AM
#6
Re: How to prevent Command btn from taking focus?
I can't.
Their GotFocus event will get fired which would create unexpected problems!
-
May 9th, 2005, 08:00 AM
#7
Fanatic Member
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
-
May 9th, 2005, 09:17 AM
#8
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:
Option Explicit
'declare external procedures
Public Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" _
Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
'declare constants
Public Const GWL_WNDPROC As Long = (-4)
'declare local variables
Private WndProcOrig As Long
Public Function BtnWndProc(ByVal hwndBtn As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
If wMsg = 7 Then
BtnWndProc = 0
Exit Function
Else
BtnWndProc = CallWindowProc(WndProcOrig, hwndBtn, wMsg, wParam, lParam)
End If
End Function
Public Sub SubClassBtn(ByVal hwndBtn As Long)
WndProcOrig = SetWindowLong(ByVal hwndBtn, GWL_WNDPROC, AddressOf BtnWndProc)
End Sub
Public Sub UnSubclassBtn(ByVal hwndBtn As Long)
Call SetWindowLong(hwndBtn, GWL_WNDPROC, WndProcOrig)
WndProcOrig = 0
End Sub
On your Form in the Form Load Event:
VB Code:
Private Sub Form_Load()
Me.Command1.TabStop = False
SubClassBtn Me.Command1.hwnd
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."
-
May 10th, 2005, 01:31 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|