The Form's KeyPreview Property
To understand this property,
lets look on the following example:
Start new project, and add 1 Command Button
(named Command1) to your form.
verify that the Form's KeyPreview property is set to "False".
Add the following code to your program:
Private Sub Command1_KeyPress(KeyAscii As Integer)
Print "Button Pressed"
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Print "Form Pressed"
End Sub
The code above will print "Button Pressed" on
the form when the Command Button's KeyPress event
will be executed, and print "Form Pressed" when the
Form's KeyPress event will be executed.
Run the program and press any key on the keyboard.
"Button Pressed" is appearing on the form, but
"Form Pressed" isn't appearing.
The Form's KeyPress event hasn't been executed.
When the KeyPreview property is "False",
if any control is found on the form (the command button in this case),
It will get all the Key events (KeyPress, KeyDown and KeyUp)
instead of the form.
To allow the Form's Key events be executed,
set the KeyPreview property to "True".
Lets try it. Set the KeyPreview
property to "True", run the program again
and press any key.
The Form's KeyPress event has been executed,
in addition to the Button's KeyPress event that
been executed too.
Notice That the form's KeyPress event executed
before the Button's event.
Popular Posts
-
2.4 Advanced Topics for the Interested If you are one of those that just must know some of the details, then this section is for you...
-
Once we know of the existence of variables and constants, we can begin to operate with them. For that purpose, C++ integrates operators. Unl...
-
Simple Binding Data Binding Data Binding is binding controls to data from the database. With data binding we can bind a control to a partic...
-
2 Managed Code and the CLR Remember that managed code is code written for (and with) the .NET Framework. Managed code is called manage...
-
Polymorphism Before getting into this section, it is recommended that you have a proper understanding of pointers and class inheritance. If ...
-
SQL Server Express is a free, easy to use, redistributable version of SQL Server 2005 designed for building simple data-driven applications....
-
Preprocessor directives Preprocessor directives are lines included in the code of our programs that are not program statements but directive...
-
5.3 Draw.aspx Web Applications I am just about to finish this tutorial. I have given you a couple of code samples to chew on, but t...
-
Adding Controls to the Form There are many controls you can use with your program: Buttons, Text Boxes, Pictures, Scroll Bars and more. You ...
-
4 Reusable Components and the FCL Up to this point I have spoken quite a bit about the goals of the .NET Framework, as well as what it ...
No comments:
Post a Comment