Windows Develop Bookmark and Share   
 index > Windows Forms General > Handling KeyDown at the right level
 

Handling KeyDown at the right level

Hi,

I've made a custom control (by extending user control) where a user can drag and drop graphical elements. I want them to be able to delete selected elements by pressing the delete key. The problem I'm having is getting the KeyDown event to fire. I think my control has focused, because when the user selects a graphical element, I call the Focus() method of my control at the end of the method. Yet, when a user presses a key, the registered KeyDown handler I have never gets invoked. I also tried handling this event at the Form level, but this gave me no luck. I'm also not sure if this second approach is really a good idea, because I have text boxes in the form, and I don't want to mess with the KeyDown behavior when the user is editing a text box. Any advice on how to make the key presses fire when the user is selecting elements on my custom control?

Thanks!
Adam Cataldo  Monday, April 21, 2008 6:33 PM
I found a workaround to my problem. I set the KeyPreview property of my control's containing form to be true, so that all KeyDown events first get handled by the Form. When the Enter event of my custom control gets fired, I make a note that the control is active by setting a boolean flag. (When Leave gets fired, I reset this to false.) If this flag is true, I process the KeyDown in my Form, and pass the important information to my custom control, so my custom control knows to delete the appropriate nodes.
Adam Cataldo  Monday, April 21, 2008 9:13 PM

Adam Cataldo wrote:
Hi,

I've made a custom control (by extending user control) where a user can drag and drop graphical elements. I want them to be able to delete selected elements by pressing the delete key. The problem I'm having is getting the KeyDown event to fire. I think my control has focused, because when the user selects a graphical element, I call the Focus() method of my control at the end of the method. Yet, when a user presses a key, the registered KeyDown handler I have never gets invoked. I also tried handling this event at the Form level, but this gave me no luck. I'm also not sure if this second approach is really a good idea, because I have text boxes in the form, and I don't want to mess with the KeyDown behavior when the user is editing a text box. Any advice on how to make the key presses fire when the user is selecting elements on my custom control?

Thanks!

What type of control did you use or extend?

Does your control have a Selected property?Or SelectedItem? Are they set?

Rudedog

Rudedog2  Monday, April 21, 2008 6:55 PM
I extended System.Windows.Forms.UserControl. My control does NOT have a Selected or SelectedItem property.
Adam Cataldo  Monday, April 21, 2008 7:59 PM

A custom control. Okay

What if you added a Forms.Panel to your control. It has the events you want, instead of using the form's keydown event use the panel's keydown event.

Rudedog

Rudedog2  Monday, April 21, 2008 8:08 PM
First, thanks for all the help. Before I go off and do this, I want to try and understand what's going on here. What events does the panel have that my custom control does not have? My control does have a KeyDown event; it just isn't getting fired anywhere. Adding the panel is a possibility, but my current control has a custom OnPaint method, and integrating the panel with it might be a fair amount of work, so before I go down this path, I just want to make sure the panel will make this problem go away.

Thanks again,
Adam
Adam Cataldo  Monday, April 21, 2008 8:19 PM
I found a workaround to my problem. I set the KeyPreview property of my control's containing form to be true, so that all KeyDown events first get handled by the Form. When the Enter event of my custom control gets fired, I make a note that the control is active by setting a boolean flag. (When Leave gets fired, I reset this to false.) If this flag is true, I process the KeyDown in my Form, and pass the important information to my custom control, so my custom control knows to delete the appropriate nodes.
Adam Cataldo  Monday, April 21, 2008 9:13 PM

You can use google to search for other answers

Custom Search

More Threads

• Rich text boxes and blocking edits (Visual Studio 2005)
• calculating dateTime (now and endDate)
• How do you use the PropertyDescriptorCollection?
• Array to listView
• no FormClose or FormClosing event
• Can't get MenuStrip's to merge correctly
• dateTimePicker question
• .net 2.0 scheduled task
• Having Problem to display form with UTF-8 charactors
• Hot key problem