|
-
Jan 15th, 2012, 10:53 AM
#1
Thread Starter
Addicted Member
Usercontrol Focus and Key events
A form's key events will fire when you press a key. Place a control on that form and it won't happen any more because that control has focus. There are exceptions to this however as you will see if you place just a h or v scrollbar on the form. The key event will fire because the scrollbars can't receive focus as is demonstrated by the following code:
Code:
Private Sub ScrollBar1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles VScrollBar1.GotFocus
Debug.Print("Scroll GF")
End Sub
Private Sub ScrollBar1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles VScrollBar1.LostFocus
Debug.Print("Scroll LF")
End Sub
So I've made my own special scrollbar usercontrol and i can't figure out how to have it leave the key events alone. I'm pretty sure that the problem is that my usercontrol can receive focus at the moment. I found that the way of doing this is "SetStyle(Windows.Forms.ControlStyles.Selectable, False)". I put that in the new event of my usercontrol and now it looks like this:
Code:
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
SetStyle(Windows.Forms.ControlStyles.Selectable, False)
End Sub
For some reason the get focus and lost focus events will still fire and (i'm not sure if this really is the problem) the key events will fire as well which is the thing i'm really after.
-
Jan 15th, 2012, 08:03 PM
#2
Re: Usercontrol Focus and Key events
A form will not raise its own keyboard events when a child control has focus unless you set the form's KeyPreview property to True.
-
Jan 16th, 2012, 05:02 AM
#3
Thread Starter
Addicted Member
Re: Usercontrol Focus and Key events
Right, so how can i make it so that my usercontrol won't receive focus?
I'm really asking this because this usercontrol is inside one of my other usercontrols... which makes it the only kind of control used. If i were to make it so that my scroll usercontrol can't receive focus then i think that my parent usercontrol would get key events just like it would when there are no controls on it.
-
Jan 16th, 2012, 05:12 AM
#4
Re: Usercontrol Focus and Key events
What child controls are on this UserControl? Are they actual VScrollBar or HScrollBar controls or something else? A UserControl can't receive focus itself anyway so setting its Selectable style to False is of no benefit. It would be the child controls that would need not to be selectable. For instance, if you have a Button control on the UserControl then it's the Button that needs to be not selectable. To do that you must inherit the Button class in your own class and set its Selectable style to False.
-
Jan 16th, 2012, 05:16 AM
#5
Thread Starter
Addicted Member
Re: Usercontrol Focus and Key events
I've got a usercontrol purely made by me, no inheritance. I've got two of these usercontrols on another usercontrol which i wan't to have focus all the time, or if that isn't possible to at least get key events(like you said by using KeyPreview).
Edit: I tried setting KeyPreview to true and it's what i need except that there is no keypreview property on a usercontrol. Ideally though, i'd like to make my custom scrollbar usercontrol so that it can't receive focus like the default one.
Last edited by cheesebrother; Jan 16th, 2012 at 05:34 AM.
Hooked for good.
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
|