-
Press a keyboard key
Hey guys I've been trying to get my web browser to load on a web page on enter but nothing seems to work here's what I'm trying to do:
Try 1
Code:
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar = Keys.Enter)
{
webBrowser1.Navigate(comboBox1.Text);
}
}
VS2008 Compiler error: Cannot implicitly convert type 'System.Windows.Forms.Keys' to 'char'. An explicit conversion exists (are you missing a cast?)
Try 2
Code:
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case Keys.Enter:
webBrowser1.Navigate(comboBox1.Text);
break;
}
}
VS2008 Compiler error: Cannot implicitly convert type 'System.Windows.Forms.Keys' to 'char'. An explicit conversion exists (are you missing a cast?)
Try 3
Code:
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case Keys.Enter.ToString();
webBrowser1.Navigate(comboBox1.Text);
break;
}
}
VS2008 Compiler error: error CS0029: Cannot implicitly convert type 'string' to 'char'
Can anyone please please help I've been trying to find a solution for 3 hours now but nothing works :(
-
Re: Press a keyboard key
Your first example will work on the KEY UP event.
(provided you fix your IF statement to include two = signs)
Code:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Left)
{
-
Re: Press a keyboard key
Yes. KeyPress doesn't trigger with modifier keys(such as control, enter), only character keys. Also it doesn't use Keys class to identify keys. I think.
-
Re: Press a keyboard key
Really the best thing you can do is use Msdn.
I don't know the specifics of this problem, but I know from the error that you have two different types. So...
I looked up KeyPressEventArgs.KeyChar in Msdn.
Then I looked at the sample code for C#. Here's the relevant part:
Code:
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
}
Notice the char cast for Keys.Return?
-
Re: Press a keyboard key
One of the correct ways to do it is
Code:
if ( e.KeyCode == Keys.Enter ) {...
Thats all about
-
Re: Press a keyboard key
this method worked:
Code:
private void comboBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
webBrowser1.Navigate(comboBox1.Text);
}
}
thanks a lot guys you're awesome