473,480 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Changing activating button

Hello friends,
In my winwows form, I have two text boxes - textbox1 & textbox2 also
the form has two buttons - button1 & button2.

button1 will work for textbox1 and button2 will work for textbox2 .
In other words, if type something in textbox1 and press Enter, button1
should be pressed and if I type type something in textbox2 and pressed
Enter, then button2 should be pressed ?

How can I do this ?

yours
Advait
Dec 10 '07 #1
5 1292
hmm,didnt do this in c# but in vb6 it should be setting "default"
property on the buttons, true or false, never tried that in runtime...
//CY
Dec 10 '07 #2
Perhaps simply handle KeyDown (on the textboxes) and check for
e.KeyCode == Keys.Return.

You might choose to move your button-code out into a separate method
that the KeyDown can call; alternatively, you can call PerformClick()
on the button; the latter is more indirect, but respects "Enabled"
etc, and is useful if you aren't directly in control of the button-
code (perhaps because of inheritance) so can't call the method
directly.

Generally I'd use the first approach. Examples below.

Marc

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
//DoSomethingInteresting(); // approach 1
//button1.PerformClick(); // approach 2
}
}
private void button1_Click(object sender, EventArgs e)
{
DoSomethingInteresting();
}
private void DoSomethingInteresting()
{
// whatever
}
Dec 10 '07 #3
in vb6 it should be setting "default" property on the buttons

For reference, this is the Form's AcceptButton property in .NET; but
you raise an interesting point... with an AcceptButton set, you'd
actually need to react to the PreviewKeyDown, not the KeyDown -
otherwise the Return doesn't make it as far as the TextBox (the Form
steals it). You might also need to use a flag to disable the regular
accept button behavior... not pretty, but achievable. Advait: let me
know if you are using an AcceptButton and need some help with this.

Marc
Dec 10 '07 #4
Actually, after a quick check it seems the easiest way to do this with
an AcceptButton is in the accept button's handler:

private void button2_Click(object sender, EventArgs e) // the
AcceptButton
{
if (ActiveControl == textBox1)
{
DoSomethingInteresting();
} // else if any other special cases...
else
{
// whatever the you would do normally
}
}
Dec 10 '07 #5
Thank you Marc Gravell for taking interest. I knew the idea you
posted in your first reply. ie. KeyDown event. But Enter kye inside
KeyDown seems like hard coding. I was looking for some other way like
christ mentioned. And you suggested "AcceptButton" property. thank
you and to christ too.
So I did it this way..

private void textBox1_Enter(object sender, EventArgs e)
{
AcceptButton = button1;
}

private void button1_Click(object sender, EventArgs e)
{
// my task
}

Still the porblem is not over !
because when I press Enter the button dosen't show PushDown effect.

yours
Advait
Dec 10 '07 #6

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

Similar topics

0
2430
by: mrjolly | last post by:
I have VB6 code that creates Excel v8 workbooks/worksheet/controls/macros based upon a text file. I have created a button on a worksheet that activates a piece of code. Unfortunately, the...
1
2649
by: Jeppe 1971 | last post by:
Hi Does anyone know of a way to load a HTML-page and examine the document without activating the code? I am trying to create a search-function in javascript. The idea is that the function...
2
1346
by: andrewkooi | last post by:
Greetings, I do not know whether such code exist but no harm asking: Is it possible for me to make visible a close button if the active window is a popup window or a new window (other than the...
2
3241
by: Mike Strauss | last post by:
Mission impossible? I would like to click on a button on an aspx page, have that trigger a section of code in the code behind, then depending of the results of the code behind execution, execute...
7
2924
by: Sakharam Phapale | last post by:
Hi All, How to preserve the old font properties while changing new one? I posted same question 2 months back, but I had very small time then. eg. "Shopping for" is a text in RichTextBox and...
12
3757
by: ppcguy | last post by:
i've got a link that i give focus to via accesskey. <a href="blah" accesskey=p> in IE, that just gives it focus, but does not activate it. how do i activate the link. thx.
17
2364
by: blufox | last post by:
Hi All, Can i change the execution path of methods in my process at runtime? e.g a()->b()->c()->d()->e() Now, i want execution to be altered at runtime as -
8
1940
by: AnjaliPanda | last post by:
I want to make the search button default so that for both mozilla and IE when i wil hit enter key it will perform the search. Now in mozilla enter key works as search key but in IE i have to hit...
2
4793
by: Hrvoje Vrbanc | last post by:
Hello all! As I have only recently started to use native ASP.NET 2.0 data access controls (and found them to be very powerful), I have one question that I was unable to find the answer to: -...
0
7046
marktang
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,...
0
7048
Oralloy
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,...
0
7088
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...
1
6741
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
5342
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,...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4485
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...
0
2997
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...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.