Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I made a simple web browser and I replace my toolStripTextBox with a toolStripComboBox but was having trouble trying to get the enter key to navigate the toolStripComboBox. Here is the code I used:
<pre lang="c#">
private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(toolStripComboBox1.Text);
            }
        }

How do I get the enter key working so that when it is hit the toolStripComboBox navigates??? Thanks
-Commander Uzzy ^.^
Posted
Comments
Sergey Alexandrovich Kryukov 24-Jan-12 23:03pm    
OK, I can see System.Windows.Forms now, but you really need to add to your tags: "WinForms". Please use "Improve question".
--SA
Sergey Alexandrovich Kryukov 24-Jan-12 23:06pm    
I sounds like the condition in ChecKeys is never true, but how do we know how it is called, from where?
--SA

1 solution

Please see my question — you did not supply full information.

Still, there is a problem with the check of the condition. "Enter" is not a character; and the whole idea of using event data is wrong. You should not use KeyPressed but you need to use KeyDown to detect this key (or even KeyUp if you wish), or override the virtual method OnKeyDown or OnKeyUp:

C#
someControl.KeyDown += (sender, eventArgs) => {
    if (eventArgs.KeyCode == Keys.Enter) { /* ... */ } // the type is KeyEventArgs, no need to cast
    // you can also check up key status:
    // eventArgs.Control, eventArgs.Alt, eventArgs.Shift... 
}; 


Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keyup.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeydown.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeyup.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900