Un-focusing dataGridView on Load to allow a form Keydown event to occur,

Relative0

Member
Joined
Sep 15, 2007
Messages
8
Programming Experience
1-3
The problem is that when the application is running I want to be able to push a button, (Say "K") and have an action performed. However when the application starts to run some sort of focus is given to the DataGridView and I am not able to push a button and have the needed action performed. Instead, when a letter is pushed the cell in the DataGridView gets the letter. I assume that this is because the DataGridView has focus. But I don't know how to get the focus to leave the DataGridView so that I can have the "K" counted as a key pressed (while over the form) and have the onKeyDown event fire. But to go a little bit further, Other than the DataGridVeiw being selected, I would like to, if any other control is selected on the form, still be able to press the "K" key and have the keypress code activated. So where do I put this code? And How do I make it so that I can do the keydown event no matter what object is selected, (other than the DatagridView)? I have attached some sample code.

Thanks,

Brian
 

Attachments

  • DataGridViewandKeydownEvent.zip
    14.4 KB · Views: 34
Last edited by a moderator:
Set form property KeyPreview=True. Also compare e.KeyCode = Keys.K

I removed the binaries you uploaded and edited the project, so you can just download it and see.
 
Re-selecting the form, (giving it focus), after the Gridview

John,

Hey, I really appreciate that, it works well and with just a flip of a switch. There is one problem still, and that is even if the Gridview is selected and I am trying to write something in a cell, if I push buttons then the keypress is firing. Is there a way to do a "double handles?" So that you have something like handles Keypress and Gridview.Focused = False? It seems even if I try to put something in such as Gridview.Focused = False I get an error:

"Error 1 Expression is a value and therefore cannot be the target of an assignment."

Any ideas?

Thanks,

Brian
 
Can I just point out that a GridView is an ASP.NET control? A DataGridView is a DataGridView and nothing else. Not a DataGrid and not a GridView. Using the wrong names for things is a great way to cause confusion.
 
There is one problem still, and that is even if the Gridview is selected and I am trying to write something in a cell, if I push buttons then the keypress is firing.
It is more complicated than that because the DGV hosts several other controls. The easier generic solution was to exclude the cases when the ActiveControl was derived from TextBoxBase (for example TextBox, RichTextBox and DataGridViewTextBoxEditingControl), but it is still not enough because if DataGridView is active control it will normally enter edit mode when you start writing. There may be other cases, but try this:
VB.NET:
If (Not TypeOf Me.ActiveControl Is TextBoxBase) _
AndAlso (Not TypeOf Me.ActiveControl Is DataGridView) Then
    Me.Text = e.KeyCode.ToString
End If
Depending on what you are actually trying to accomplish there could also be better ways to do it than the road we're currently pursuing. (for example a MenuStrip with the options required and keyboard shortcuts set)
 
Allowing Users to dynamically create Hotkeys.

jmcilhinney, you have a good point, thanks.

Also, John, thanks for the heads up, your idea worked, and worked well. I have been now trying to figure out how to be able to allow the users to dynamically assign a shortcut key to different objects. Do you have any idea about how to do this? I have been reading different things online and it looks like quite a long and drawn out process. I wonder though if there is a more simple way to do it. What I have been thinking is this. Perhaps I can modify the program so that if the "ctrl" button is pushed, when I push another key, the "e.KeyCode.ToString" becomes active and then assigns that value to either a variable or some element in an array. So in the end I can do something such as

dim variable as string
variable = e.KeyCode.ToString\

If e.KeyCode = Keys.variable Then
'Code Goes Here!

End If

I don't know how to do this so that I can stick "variable" at the end of "Keys"

I have been looking at pages such as and similar to:

http://www.developerfusion.co.uk/show/271/

Although I don't understand what is going on.

Furthermore I am wondering how I might save that to My.settings. As I would like to save the values to the database or XML file or whatever VB.NET uses. I however am not sure what to put that under. As for example I get, under the Properties panel "ApplicationSettings" for an object, I press "DataBindings", and can choose something like "Font" or "Location". I however want some kind of value that will store the Keys.variable value. Any thoughts on this? I would like the same key to be re-loaded when the program starts up and so am trying to figure out how to do this also.

Does my proposed method make sense? Should this work? Is there some kind of simple class that will do what I am trying to do?

Thanks much for any leads.

Brian
 
The Keys enumeration allows a bitwise combination of key and modifiers, you also see the type for ToolStripMenuItem.ShortcutKeys property is type Keys. e.KeyData of KeyDown/KeyUp already contains this combination, if you cast it to Integer (CInt function) you can easily store the value and retrieve it again in My.Settings, you can also present it by using ToString (it will display for example "L, Shift, Control") or custom converter method if you have a configuration UI. ToolStripManager.IsValidShortcut method can be used for validation.

Btw, the link you posted is for registering a global system hotkey, not what I've been talking about, but I could have misunderstood. If the option is for private application use and you can't use menus you should write your own shortcut manager using the form KeyPreview feature, avoid system hotkeys if you can.
 
Back
Top