Click to See Complete Forum and Search --> : Text Box Event Handler


definition
January 30th, 2006, 04:35 AM
Hey can any of you guys tell me how to get an event handler to fire off a text box when i hit enter or return. I checked the microsoft documentation, however its method doesnt actually fire an event when the keys are pressed to check if it is the enter or return keys.

editBox.AcceptsReturn = true;
editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver);

private void EditOver(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
//ascii decimal values
if (e.KeyChar == (char)13)
{
lstABList.Items[itemSelected] = editBox.Text;
editBox.Hide();
this.btnABSelect.Enabled = true;
updateHandsetContacts();
}

if (e.KeyChar == (char)27)
{
Console.Out.Write("");
loadContactsFromModel();
}
}

It does however fire when i press other keys, like letters or numbers. Can anyone help?

jmcilhinney
January 30th, 2006, 06:40 AM
Only certain keys raise a KeyPress. Virtually every key raises a KeyDown and KeyUp event though.

definition
January 30th, 2006, 07:22 AM
thanks, it should be noted the text box also needs to be set to be multiline for enter and return events to occur and also have its acceptsreturn property set to true.

jmcilhinney
January 30th, 2006, 07:30 AM
thanks, it should be noted the text box also needs to be set to be multiline for enter and return events to occur and also have its acceptsreturn property set to true.You should be a little careful with your terminology. There is an Enter event for a TextBox but it has nothing to do with the Enter key on the keyboard, and there is no such thing as a Return event. There is a KeyUp, KeyDown and KeyPress event and these events may be raised when the user presses keys on the keyboard. It may not seem important but using consistent terminology helps to reduce confusion.