473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Intercepting the Enter key

I have a default button on a form, but when a certain textbox has focus, I want
to intercept Enter key events and perform a different action. Is there an easy
way to do this, or should I just check the textbox's focus in the default
button's event handler?

Thanks in advance,
Steve Murphy
Nov 16 '05 #1
6 4874
You could use the KeyUp or KeyDown event handler on the textbox and check
the KeyEventArgs.KeyCode property agains the Keys enumerable Keys.Enter. e.g

public class Form1:System.Windows.Forms.Form
{
private TextBox textBox1
public Form1
{
/// add button and textbox to form etc

textBox1.KeyUp += new KeyEventHandler(TextBox1_KeyUp);
}

private void TextBox1_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
RunSomeMethod(); /// the method you wish to run
}
}
}
Nov 16 '05 #2

"Uchiha Jax" <i_************************@NOSPAMhotmail.com> wrote:
You could use the KeyUp or KeyDown event handler on the textbox and check
the KeyEventArgs.KeyCode property agains the Keys enumerable Keys.Enter. e.g


Thanks. This works only in the KeyUp event. Do you have any thoughts on why? Not
that it's important; I'm just curious.

Also, however, this does not filter the event, and the default button event
still runs.

Thanks,
Steve Murphy
Nov 16 '05 #3
Oh yes sorry, far easier is to put in the button eventhandler

if(!this.textBox1.Focused)
{
RunYourRoutine();
}
else
{
//// do something else
}

Although if you have lots of these types of textbox you may want to consider
a different solution, is this ASP.NET?
"Steve Murphy" <sm*****@granite-ridge.com> wrote in message
news:Od****************@TK2MSFTNGP12.phx.gbl...

"Uchiha Jax" <i_************************@NOSPAMhotmail.com> wrote:
You could use the KeyUp or KeyDown event handler on the textbox and check the KeyEventArgs.KeyCode property agains the Keys enumerable Keys.Enter.
e.g
Thanks. This works only in the KeyUp event. Do you have any thoughts on why? Not that it's important; I'm just curious.

Also, however, this does not filter the event, and the default button event still runs.

Thanks,
Steve Murphy

Nov 16 '05 #4
> Oh yes sorry, far easier is to put in the button eventhandler

No problem. I was just wondering if there was a better way.

Thanks again,
Steve Murphy
Nov 16 '05 #5
That's because the AcceptEnter property of the textbox is set to false.
If you set it to true, you'll get event notification for KeyDown also.
You'll be able to filter it too.

Regards
Senthil

Nov 16 '05 #6
"sadhu" <se**********@wdevs.com> wrote:
That's because the AcceptEnter property of the textbox is set to false.
If you set it to true, you'll get event notification for KeyDown also.
You'll be able to filter it too.


Nope. Tried that. It's okay, putting the code in the button method is not a
problem.

Thanks,
Steve Murphy
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Valery Polyakov | last post by:
I am drawing lines with a semi-transparent color (alpha=100). When these lines intercept, the color becomes more intense. Is there a way to avoid this, so that I would get the same intensity of...
7
by: John | last post by:
Hi, Can anyone point me out how to intercept the keystrokes from a c# application that's not a active window? - intercepting the system wide keystrokes before it hits the target active window? ...
0
by: VMI | last post by:
My Windows datagrid has two columns: one with the data that the user will see (col_X) and the other one (a hidden one: col_sort) that'll be used to sort data. When a user clicks on col_X's column...
4
by: Curious Coder | last post by:
I have been tasked with a project that I do not think can be accomplished. Our company has an application that runs as an unmanaged ActiveX control on user desktops. It is designed to work with...
5
by: aaa | last post by:
Is there a simple straight forward tool out there for intercepting my SOAP requests so that I can debug the XML that I am sending?
3
by: pamelafluente | last post by:
I am new to asp.net. I have an asp page with a submit button which sends out some information. Instead of having IIS to respond and deal with this information, I would like to have a .NET...
5
by: imimin | last post by:
Hello! I am new to your forum and I must say it looks GREAT! I have a bit of a problem. I use a vendor for my business that I am pulling data off of their web-site and putting on mine using...
0
by: Gabriel Genellina | last post by:
En Thu, 18 Sep 2008 19:24:26 -0300, Robert Dailey <rcdailey@gmail.com> escribió: Why don't you try it yourself? You may replace builtins.print with your own function too. It's not exactly the...
8
by: K Viltersten | last post by:
I've seen some people calling a stored procedure on MS SQL Server and intercepting an invalid parameter list by a switch statement, demanding that each of the parameters names is exactly as the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.