I've never handled keysroke events before and I would like to implement a Return keystroke event in a Windows form to bring up a list of items in a datagrid as well as habing a search button in the form.
With the help of a tutorial, I have coded the following event: -
private void ReturnResults(object o, KeyPressEventArgs e)
-
{
-
char c = e.KeyChar;
-
if (c == (char) Keys.Return)
-
{
-
SearchAdmin();
-
}
-
else
-
{
-
e.Handled = false;
-
}
-
-
base.ProcessDialogKey(Keys.Return);
-
}
-
and have assigned the event handler in the constructor of my Form, with the following line: -
this.KeyPress += new KeyPressEventHandler(ReturnResults);
-
//this.ProcessDialogKey += new KeyPressEventHandler(ReturnResults);
-
I have also been told that the KeyPress event does not handle control keys, so I am using the ProcessDialogKe y() library to override it in the method, but cannot assign this to the ProcessDialogKe y method group (commented out part).
Unfortunately, nothing happens when I press return with the code as it stands. And, when I first tried implementing it, the form would EXIT when return was pressed, but I don't know how that would have happened?
Am I missing something? Please advise.
Matt
6 4566
In case anyone else has this problem:
You need to put the following in the constructor of the form:
if you want to handle a key being pressed when controls are active:
The control will then work on the form every time!
Useful little bit of information...
i actually have a similar question...thou ght id ask it in this thread rather than starting a new one
i have a infinite for loop that keeps on going. i want to make it so that if 'escape' key is pressed at anytime, the loop stops. whats the best way to do this?
im new to multithreading, delegates, and events but im thinking of having a key press event that is fired whenever the escape key is pressed and it stops the loop. i understand the theory but not sure how the code would look.
thuoghts? sample code? links? thanks.
@developing
Depends how you'd like to do it and how you're code it setup. You could have -
while(Console.ReadKey().Key != Key.Escape)
-
{
-
// Do stuff
-
}
-
or something more like: -
private bool cancel;
-
-
private void Setup()
-
{
-
this.KeyDown += new KeyDownEventHandler(ToggleCancel);
-
-
// or using lambdas
-
this.KeyDown += new KeyDownEventHandler((sender, e) =>
-
{
-
if(e.Key == Keys.Escape)
-
{
-
this.cancel = true;
-
}
-
});
-
-
Process();
-
}
-
-
private void Process()
-
{
-
cancel = false;
-
while(!cancel)
-
{
-
// Do stuff
-
}
-
}
-
-
private void ToggleCancel(Object sender, KeyEventArgs e)
-
{
-
if(e.Key == Keys.Escape)
-
{
-
this.cancel = true;
-
}
-
}
-
thanks for the skeleton, im trying the second part.
lets see if this doesn't help me out. thanks
i got it working. thanks...had to put the stuff inside the while(!cancel) loop and the event on different threads to get it workin
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: coolsti |
last post by:
Can someone help me enhance this code snippet which works only for IE so
that it will also work for Firefox? I have been trying all sorts of things
and have gotten to where I can capture the keydown and keypress events in
Firefox, but then I can't seem to get the key code. I also don't know if
the window.event.srcElement.type works with...
|
by: Jay Xx |
last post by:
I have an IFrame in design mode. I've tried a bunch of things to
capture key presses in that IFrame, but I can't seem to get it. I can
capture key presses outside the IFrame fine. I have this problem in
Firefox, not IE.
I do know it's possible because Blogger.com's rich text editor does it,
but their code is cryptic and separated into like 20...
|
by: jerrygarciuh |
last post by:
Hello,
I have been playing with various Googled solutions for capturing the <Enter>
key to suppress form submission.
My first question is whether anyone has a script that works in all common
browsers? The script bellow is IE only. It fails FF 1.0 and NN 7.
Also, if I can trap the keypress I would like to use it to tab to the next...
|
by: alien2_51 |
last post by:
I have a form with several buttons, I'd like to default to
a specific button on the Enter keypress event, How would I
do this...?
|
by: @sh |
last post by:
Has anyone a function handy that I can apply to various textboxes within a
form, each textbox will permit most characters but I want to ban certain
characters from each textbox.
Therefore I need a function that I can put into the <text area> tag of each
box, something like...
<text area...
| |
by: jbigham |
last post by:
Hello,
I'd like to capture key events using javascript, but don't want to
process such events when the user is typing into an input box or into a
textarea. As an example, gmail has a feature where you can type "r"
anywhere on the page while viewing a message and it will open a reply
box, but if you type an "r" while entering text into a...
|
by: sULs |
last post by:
Hi all
Hoping someone can help !!
I am trying to catch F2 - 6 key presses in a winforms application
Code as follows
private void frmMain_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
|
by: MLM450 |
last post by:
I have a control that handles the KeyDown event but it does not seem to
execute when a combination of keys is pressed - like CTRL+Z. If I
press CTRL, it executes. If I press Z, it executes. But the handler
does not see the combination.
Now this control is contained within another control which is contained
within another. The top most...
|
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= |
last post by:
I have a tabcontrol where I want to capture the CTRL+C key combination. My
tabcontrol has on its tabpages some treeView controls. My intention is to
intercept the event of pressing the CTRL+C when one of the controls embedded
in the tabControl has the focus. I'm doing this using the KeyDown event.
However, when pressing the CTRL+C my computer...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |