Jake F.
Jake F.

Reputation: 141

if (e.KeyCode == Keys.Enter) is non-functional

I have this

    private void toolStripTextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Return)
        {
            var items = new[] { 500+ objects here };
            if (toolStripTextBox1.Text.StartsWith("www."))
            {
                webBrowser1.Navigate(toolStripTextBox1.Text);
            }
            if (toolStripTextBox1.Text.StartsWith("http://"))
            {
                webBrowser1.Navigate(toolStripTextBox1.Text);
            }
            if (toolStripTextBox1.Text.StartsWith("https://"))
            {
                webBrowser1.Navigate(toolStripTextBox1.Text);
            }
            if (items.Any(item => toolStripTextBox1.Text.Contains(item)))
            {
                webBrowser1.Navigate(toolStripTextBox1.Text);
            }
            else
            {
                webBrowser1.Navigate("https://www.google.ca/?gws_rd=ssl#safe=active&q=" + toolStripTextBox1);
            }
        }
    }

and it litteraly doesn't work. eg. I run it, no errors, all it does is play the windows error sound and won't be functional....

I know the code after the if statement is functional cause i have the exact same code on a button and it works just fine.

Upvotes: 0

Views: 4304

Answers (3)

dub stylee
dub stylee

Reputation: 3342

Try this for your KeyPress event handler:

private void toolStripTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Return)
    {
        //Insert code here

It sounds like the problem may be that the method is not bound to the KeyPress event.

In the designer, click on the textbox, and then over to the side where it lists all of the properties, there should be a lightning bolt looking icon, click on that, and then scroll down to KeyPress and make sure it says toolStripTextBox1_KeyPress

EDIT

Alternatively, you can add the event handler programmatically. In your Form_Load event, add the code

toolStripTextBox1.KeyPress += toolStripTextBox1_KeyPress;

and in your Form_Closed event handler, add

toolStripTextBox1.KeyPress -= toolStripTextBox1_KeyPress;

Upvotes: 0

Ashraf Sada
Ashraf Sada

Reputation: 4905

Use your code in key up event, not key down, this will make event fully executed and eligible to read the key pressed.

I have adjusted your code and it works, you don't have to use KeyChar just use KeyCode instead and it should work.

private void toolStripTextBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter || e.KeyValue == (char)Keys.Return)
            //if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Return)
            {
                var items = new[] { 500 + "objects here" };
                if (toolStripTextBox1.Text.StartsWith("www."))
                {
                    webBrowser1.Navigate(toolStripTextBox1.Text);
                }
                if (toolStripTextBox1.Text.StartsWith("http://"))
                {
                    webBrowser1.Navigate(toolStripTextBox1.Text);
                }
                if (toolStripTextBox1.Text.StartsWith("https://"))
                {
                    webBrowser1.Navigate(toolStripTextBox1.Text);
                }
                if (items.Any(item => toolStripTextBox1.Text.Contains(item)))
                {
                    webBrowser1.Navigate(toolStripTextBox1.Text);
                }
                else
                {
                    webBrowser1.Navigate("https://www.google.ca/?gws_rd=ssl#safe=active&q=" + toolStripTextBox1);
                }
            }

Upvotes: 1

Slubberdegullion
Slubberdegullion

Reputation: 159

Make sure that the control has focus, or redirect the event to the control

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown%28v=vs.110%29.aspx

Upvotes: 0

Related Questions