Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Slide, tank and thermometer will affect "keydown" event

Hi,

I just found that the slide, tank and thermometer control will stop the "keydown" event of the Form. Here are what I did:

1. Create a project, add a Form1.
2. In Form1.Designer.cs add a line:   this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
3. Add the event handler in Form1.cs: 
        void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            MessageBox.Show("Hello, world.");
        }

4. Run the program and hit any key, you will get the "Hello..." message.
5. Drop either slide, tank or thermometer on the Form1.
6. Try to run again and hit any key gives no response.

I am not sure if this is a bug. However, I would like to  have the key event hanler for some other functionalities, even it can be done by click on mouse.

Environment:  MS Visual Studio 2005  C#, Measurement Studio 8.1 (Professional Package).

Dufei


0 Kudos
Message 1 of 5
(3,578 Views)
Hi,

the problem can be "solved" by setting the control property "TabStop" to false.  I don't know why it will affect the Form's "keydown" event though.

Dufei
0 Kudos
Message 2 of 5
(3,571 Views)
Hi, Dufei,

It looks like this problem is not restricted to slide, tank, and thermometer. I was able to reproduce the same behavior with a button. So I think it has something to do with the keydown event, rather than measurement studio. You might be able to find an answer on msdn. If I find anything, I will let you know.

Song Du
Regards,

Song Du
Systems Software
National Instruments R&D
0 Kudos
Message 3 of 5
(3,543 Views)

Hi Dufei,

The behavior you are seeing is normal behavior with regards to how the KeyPress event works in .NET. Every .NET control and Windows Form have the KeyPress event you can capture.

Suppose that you have dropped a control onto your form and created and event handler to handle the KeyPress event. You also have an event handler for the Forms KeyPress event. Here are behaviors you will see:

- By default, when you run the application and press a key, the control's KeyPress event will fire but not the Forms event. This is because the control has focus. If you read the KeyPress event description, it states "Occurs when the control has focus and the user presses and releases a key". Thus, if you set the TabStop property for the control to false, meaning that control will never have focus, then the Forms event will fire.

- Now, Windows Forms have a KeyPreview property that determines whether the form will receive key events before the event is passed to the control that has focus. If you set that property to true, both events will fire with the Forms event coming first. If you don't want the control's event to fire after the Forms event, inside the Forms event handler, you can set the
e.Handler property to true which controls whether the control's event is handled.

Hope this clarifies things!

Best Regards,

Message Edited by Jonathan N on 08-20-2007 01:09 PM

Jonathan N.
National Instruments
0 Kudos
Message 4 of 5
(3,541 Views)
Hi, Jonathan. Thank you very much indeed.

Dufei
0 Kudos
Message 5 of 5
(3,535 Views)