text box press return key do a button click

  • Thread starter Thread starter frogman7
  • Start date Start date
F

frogman7

the form I have has 5 buttons and 3 textboxes and 1 list box
see crude pic below

---------------------------------------------------------------
| textbox_______________ btnBrowse |
| |
| textbox_______________ btnBrowse |
| |
| |
| AddTextBox___ btnAdd |
| ------------------------------- |
| | | |
| | | |
| | | |
| | | |
| | | |
| ------------------------------- |
| |
| |
| |
| btnSumit btnClose |
| |
---------------------------------------------------------------



I have a text box and an add button next to it.

the user can type text in the text box and click the add button
i want the user to be able to type the text and press enter and the
text is added to the list box.

How do i capture the return as the btnAdd button click?
 
use KeyPress event :

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
addItemToList();
}

private void button3_Click(object sender, EventArgs e)
{
addItemToList();
}

private void addItemToList()
{
if (textBox1.Text.Length==0)
return;

listBox1.Items.Add(textBox1.Text);
textBox1.Text = "";
}

Regards
Nicolas Guinet
 
Thank you for your reply i know a little c but know much i am coding in
VB.NET

Here is the code i am trying to use but when i press the return key it
doen't capture it.

If e.KeyChar.Equals(Keys.Return) Then
Call btnAddExtention_Click(sender, e)
End If

Thank you for your help
 
Hi

You can trap the return key in KeyUp event handler lke this :

private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs
e)
{

if (e.KeyCode == Keys.Enter)
button1_Click(sender, e);
}
 
Back
Top