|
-
Dec 28th, 2011, 12:20 PM
#1
Thread Starter
Addicted Member
Locking usercontrol focus
I'm trying to create a grid usercontrol. This means that when i press "UP" it goes to the cell above (...so on for all directions) tab goes to the right and enter goes down. Now all these keys (excluding enter) change control focus so i've got a problem using them. How can i lock focus on the control so that these keys won't change it and so that i can use these keys for other things?
-
Dec 28th, 2011, 06:18 PM
#2
Re: Locking usercontrol focus
Are you really creating a user control, i.e. a class that inherits UserControl? If so then you should probably forget that idea altogether. A UserControl is intended as a host for multiple other controls. A grid should not contain multiple controls. The standard DataGridView is a prime example. It is a control itself but it contains no child controls by default. Only when you actually start editing a cell does a child control get created and embedded in that cell. As soon as you finish editing that cell, the child control is removed. If you have even a reasonable amount of data and a child control for each cell, performance will suffer. Only if you are always going to be working with a small amount of data should you consider one control per cell.
-
Dec 28th, 2011, 10:05 PM
#3
Re: Locking usercontrol focus
jmcilhinney pretty much nailed it. A grid made up of controls would need a lot of window handles which is a relatively limited user space resource. Also it means multiple paint events, one for each cell when you could simply do all your drawing in the overriden OnPaint of the control itself. In short, a control carries with it much baggage which would dramatically increase overhead for the simplest operations on the grid control as a whole.
-
Dec 29th, 2011, 03:26 AM
#4
Thread Starter
Addicted Member
Re: Locking usercontrol focus
I don't know why you're picking on the fact that i'm making a grid usercontrol but i'll tell you how i've done it. I draw 100% of it on the usercontrol's background. When you edit a cell a textbox (there is only one textbox in the entire control no matter how many cols or rows) appears and then when you're done editing it disappears.
In the past, when i didn't know much, i had a textbox for each cell but that (as jmcilhinney pointed out) made it terribly slow. Right now i've got my grid working very fast and well, i'm just asking a question completely unrelated to a grid control works. So please, forget that this is even connected with a grid or that i'm even trying to make a grid. My question is this :
How can i lock focus on the usercontrol so that these keys won't change it and so that i can use these keys for other things?
This could be needed for many controls, i just mentioned my grid control as an example.
Last edited by cheesebrother; Dec 29th, 2011 at 03:30 AM.
Hooked for good.
-
Dec 29th, 2011, 06:45 AM
#5
Re: Locking usercontrol focus
 Originally Posted by cheesebrother
..
Found this... How to Trap Arrow/Direction Keys in a User Control
-
Dec 29th, 2011, 03:24 PM
#6
Thread Starter
Addicted Member
Re: Locking usercontrol focus
 Originally Posted by Edgemeal
Works great. I modified it a bit to test it and this works. It will bring up a msgbox when the left key is pressed:
Code:
Protected Overrides Function IsInputKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
Dim stdkeys As System.Windows.Forms.Keys = keyData And Not Keys.Modifiers
Dim mods As System.Windows.Forms.Keys = keyData And
Keys.Modifiers
Dim shift As Boolean = ((mods And Keys.Shift) = Keys.Shift), ctrl As Boolean = ((mods And Keys.Control) = Keys.Control)
Dim shiftorctrl As Boolean = shift Or ctrl
Dim nomods As Boolean = (mods = Keys.None)
Dim navigationkeys As Boolean = (stdkeys = Keys.Left Or stdkeys = Keys.Up Or stdkeys = Keys.Right Or stdkeys = Keys.Down Or stdkeys = Keys.Home Or stdkeys = Keys.End Or stdkeys = Keys.PageUp Or stdkeys = Keys.PageDown)
If (navigationkeys And nomods) Or (navigationkeys And shiftorctrl) Then
Return True
Else
Return MyBase.IsInputKey(keyData)
End If
End Function
Private Sub UserControl1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Left Then MsgBox("The left key was pressed")
End Sub
I had tried using the preview keydown and not allowing the usercontrol to loose focus but this is a much, much cleaner way of doing it. Thanks so much for providing the link Edgemeal, that's a big help for me.
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
|