Changing activating button

A

Advait Mohan Raut

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
 
C

christery

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
 
M

Marc Gravell

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
}
 
M

Marc Gravell

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
 
M

Marc Gravell

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
}
}
 
A

Advait Mohan Raut

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top