c#: Hitting 'enter' in a text box instead of clicking a button?

Started by
3 comments, last by HopeDagger 19 years, 6 months ago
I always wondered how to do this for each language. Right now whenever I hit enter in a text box, I get a beep sound. The idea is to allow the user to easly press 'enter' instead of clicking on the submit button every time. What I need is something similar to this pseudocode: if User presses enter in a text box do some action. Thanks.
Advertisement
I only know VB.NET [wink]
    Private Sub txtSearchString_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearchString.KeyPress        If (Asc(e.KeyChar)) = 13 Then cmdSearch_Click(sender, e)    End Sub


That passes the event along to cmdSearch_Click. ASCII 13 is the enter key.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

*translates* ;)

	private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)	{		if(e.KeyCode == System.Windows.Forms.Keys.Enter)		{			myButton_Click(sender, e);		}	}
Nope. System.Windows.Forms.TextBox.AcceptsReturn.
Very nice! I wasn't aware that you could set a default button for the form.

This topic is closed to new replies.

Advertisement