Hi,
I would like to find a way in C# so that, when the user presses the
"Esc" key, the form closes; the problem is that I have a lot of
controls in the form.
At the beginning I have tried with the following form event handler:
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (e.KeyChar == (char)Keys.Escape)
Close();
}
But I have realized that it functions only if there are no controls in
the form.
Suppose that you have three Buttons in the form (btn1, btn2 and btn3).
The fastest way (with the minimum number of code lines) I have found to
close the form pressing the Esc key is the following one:
....
btn1.KeyPress += new KeyPressEventHandler(control_KeyPress);
....
btn2.KeyPress += new KeyPressEventHandler(control_KeyPress);
....
btn3.KeyPress += new KeyPressEventHandler(control_KeyPress);
....
....
....
void control_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Escape)
Close();
}
Have I to add a
Control_i.KeyPress += new KeyPressEventHandler(control_KeyPress);
statement for EVERY control in the form??? (suppose you have 100
controls... It's not so nice!!!)
Do you know if there is a faster way (with a lower number of code
lines) to do that?
Thank you very much 4 10954
I would like to find a way in C# so that, when the user presses the
"Esc" key, the form closes; the problem is that I have a lot of
controls in the form.
Call System.Windows.Forms.Application.Exit() in response to the Escape key
and your program terminates. Or do I misinterpret your question and do you
only want to close one form without closing the program?
On 9 Nov 2006 03:09:01 -0800, polocar wrote:
Hi,
I would like to find a way in C# so that, when the user presses the
"Esc" key, the form closes; the problem is that I have a lot of
controls in the form.
At the beginning I have tried with the following form event handler:
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (e.KeyChar == (char)Keys.Escape)
Close();
}
But I have realized that it functions only if there are no controls in
the form.
Suppose that you have three Buttons in the form (btn1, btn2 and btn3).
The fastest way (with the minimum number of code lines) I have found to
close the form pressing the Esc key is the following one:
...
btn1.KeyPress += new KeyPressEventHandler(control_KeyPress);
...
btn2.KeyPress += new KeyPressEventHandler(control_KeyPress);
...
btn3.KeyPress += new KeyPressEventHandler(control_KeyPress);
...
...
...
void control_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Escape)
Close();
}
Have I to add a
Control_i.KeyPress += new KeyPressEventHandler(control_KeyPress);
statement for EVERY control in the form??? (suppose you have 100
controls... It's not so nice!!!)
Do you know if there is a faster way (with a lower number of code
lines) to do that?
Easiest way to do this is to make one button the close button. Get rid of
all of your keypress event handlers. In your close button's click event
handler, close the form. Then go to the properties for the form and set
your close button as the cancel button for the form.
--
Tom Porterfield
Hi,
I would like to find a way in C# so that, when the user presses the
"Esc" key, the form closes; the problem is that I have a lot of
controls in the form.
At the beginning I have tried with the following form event handler:
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (e.KeyChar == (char)Keys.Escape)
Close();
}
But I have realized that it functions only if there are no controls in
the form.
Suppose that you have three Buttons in the form (btn1, btn2 and btn3).
The fastest way (with the minimum number of code lines) I have found to
close the form pressing the Esc key is the following one:
...
btn1.KeyPress += new KeyPressEventHandler(control_KeyPress);
...
btn2.KeyPress += new KeyPressEventHandler(control_KeyPress);
...
btn3.KeyPress += new KeyPressEventHandler(control_KeyPress);
...
...
...
void control_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Escape)
Close();
}
Have I to add a
Control_i.KeyPress += new KeyPressEventHandler(control_KeyPress);
statement for EVERY control in the form??? (suppose you have 100
controls... It's not so nice!!!)
Do you know if there is a faster way (with a lower number of code
lines) to do that?
Thank you very much
You can (recursively) loop though all controls on the form. When you
find one that can take a KeyPress event (particular baseclass??),
attach the handler.
Hans Kesting
Thank you guys.
Someone suggested me that another possible solution is to set the form
KeyPreview property to true:
this.KeyPreview = true;
so that the form can handle the keyboard events before its controls.
Then you can use the OnKeyPress form event handler... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andras Gilicz |
last post by:
Hi VB fans
I'm working on a relatively large project in VB6 with about a dozen
forms, including graphs, labels, text boxes, etc. The software itself
is actually a flow simulator with more or...
|
by: Richard Cornford |
last post by:
I am interested in hearing opinions on the semantic meaning of FORM
(elements) in HTML.
I have to start of apologising because this question arose in a context
that is not applicable to the...
|
by: Lyn |
last post by:
Hi,
I am working on a genealogy form. The only table (so far) lists everybody
in the family, one record per person. Each record has an autonum ID.
The parent form (frmMainForm) displays the...
|
by: guoqi zheng |
last post by:
In a regular html form, when user press "enter" key, the form will be
submitted.
However, in ASP.NET web form, a form will only be submitted (post back) when
a special button is clicked. Many...
|
by: Nathan |
last post by:
I know this has been asked previously, but I've run into a situation where I
need to know the difference between close and dispose, and I can't get the
information I need from msdn help or previous...
|
by: jmp |
last post by:
(I hope this isn't considered too far off-topic.)
I work as a developer on a browser product found on handheld devices,
and I'm trying to specify behavior for the browser to make it "as...
|
by: Greg Strong |
last post by:
Hello All,
The short questions are
1 Do you know how to make DSN connection close in Access to Oracle 10g
Express Edition?
&/or
2 Do you know how to make a DSN-less pass-through query...
|
by: bsturg21 |
last post by:
Hello,
I have a windows form that has a series of linklabels on it, and I
need to have each linklabel, when clicked, open a separate windows
form that has a single paramter passed into it. The...
|
by: Bob Alston |
last post by:
I have a system where many subforms are used. Often the size of the
subform had to be larger than could be displayed without scrolling.
I set the height of the subform to the typical height...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |