Using the KeyDown event of the text box for capturing Enter isn't always safe. If the form has a DefaultButton set then the button will receive the Enter key press, and the textbox event will never fire. What you can do is override ProcessCmdKey() :
private void frmTestKeyDown_Load(object sender, EventArgs e)
{
this.AcceptButton = button1;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (((keyData & Keys.Enter) == Keys.Enter) && this.textBox1.Focused)
{
listBox1.Items.Add(textBox1.Text);
return true; //stops the key from being further processed
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735