Solved

key press to form only; not child controls

Posted on 2004-04-03
8
531 Views
Last Modified: 2007-12-19
I have a form with child controls.  When the form opens, one of the child controls (a radio button) gets focus.  Then when I press the up/down arrow keys, a radio button group selection changes.

What I want is for the form to always get the key press events and I dont ever want the child controls to get a key press event.  I tried over-riding the OnKeyDown method, but since a child control on the form has focus, it gets the key press event and not the parent form.  

How can I cause the parent form to get all key press events and stop them from being propigated to the child controls?  The user still needs to interact with the controls (sliders, radio button, etc), but even if a slider has focus, if the user presses a key, I want the parent form to get this event and cosume it and not let the slider get it.

thanks
Bryan
0
Comment
Question by:bryanwells
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 10747972
Have you tried setting the Form's KeyPreview property to True? With this event Form's KeyDown, KeyUp, and KeyPress events will be raised BEFORE those of the controls.

0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 10747974
Sorry, forget my comment. I thought I was into the VB topic!
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 10747991
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:RichardCleaver
ID: 10748512
Try setting Form.KeyPreview to true.  Then in the keyboard event set e.Handled = true.  This will stop any controls from receiving the input.

For example:

private void FormX_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
      if (e.Alt && e.KeyCode == Keys.F4 || e.KeyCode == Keys.Escape) {
      // Do some processing if the Escape key or Alt+F4 is pressed.  i.e. FormX.Close()
      e.Handled = true;
      }
}
0
 

Author Comment

by:bryanwells
ID: 10749352
This almost works.  But my radio buttons come up selected first, and the arrow keys still toggle the radio buttons without getting a call to my key down event handler in my form.  Once I put focus on another control, it works as expected.  Is there something special about radio buttons and up/down arrow keys?
0
 

Expert Comment

by:RichardCleaver
ID: 10749724
Radio buttons always act as a group in a container.  The container in this case is the form.

Try putting 'this.ActiveControl = MyControl' in your FormX_Load event.  This should put the focus on another control (MyControl) before the form is visible.
0
 

Author Comment

by:bryanwells
ID: 10749752
This is very close to what I want.  I just wish I could stop the arrow keys from toggling the radio buttons.  I have them in a group box instead of in the form directly, but still it is the same behavior as you described.  How can I  disable radio buttons from responding to the up/down arrow keys?
0
 
LVL 4

Accepted Solution

by:
caner_elci earned 125 total points
ID: 10796385
You can add a message filter to your application and listen for WM_KEYDOWN and WM_KEYUP messages there. PreFilterMessage method of IMessageFilter is what you need.

Check out IMessageFilter and Application.AddMessageFilter() for further information.

Also you can try setting TabStop property to false for your radio buttons...

Caner ELCI
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Gridview 1 64
Get distinct values from excel or comm seperated file 4 40
How to Generate a Method from a keyboard shortcut in Visual Studio? 2 34
Problem to copy file 14 43
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question