PDA

View Full Version : [Visual C#] On "Enter" in ToolStripTextBox



Esherido
January 27th, 2007, 04:36 PM
I'm trying to make a event handler for when a user presses the Enter key while he's typing in a ToolStripTextBox. It's in a web-browser like program so I want it to go to the URL user's type in when the user presses the Enter key.


Thanks,
:kommie: Esherido

kirupa
January 27th, 2007, 05:25 PM
When you mention "web-browser like program", are you hoping to send that text to the address bar of an actual web browser (IE, FF, etc.) or a web-browser control within your application? :)

Esherido
January 27th, 2007, 05:31 PM
To a web-browser control within the actual application. ;) Thanks for the help kirupa, I feel honored. :P

kirupa
January 27th, 2007, 08:31 PM
Oh, in that case, select your text box and look in the Properties panel. You should see five icons towards the top of your Properties panel and click on the one that looks like a lightning bolt. That is the area for Event Handling, so in the KeyDown field, provide a name - any name - and press Enter.

You will find yourself in the Code view. From there, all you have to do is get some code to catch the event and bind it to your Enter Key:
private void EnterKeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
browserMain.Url = new Uri(txtInput.Text);
}
}
In my program, my browser control is called browserMain and my text field is called txtInput. I have attached my source file for a quick example I created that loads a HTML page based on the text you enter.

http://img253.imageshack.us/img253/6644/mykirupabrowserry3.th.png (http://img253.imageshack.us/my.php?image=mykirupabrowserry3.png)

Cheers!
Kirupa :P

Esherido
January 28th, 2007, 02:12 PM
Thanks, kirupa! I was using the KeyDown event but it just wasn't working for me. Thanks again!

EDIT: I just tried it out and it works perfectly! Thanks! :P