|
-
July 30th, 2008, 11:46 AM
#1
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
Last edited by Korupt; July 30th, 2008 at 11:54 AM.
-
July 30th, 2008, 12:02 PM
#2
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)
{
-
July 30th, 2008, 12:06 PM
#3
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.
-
July 30th, 2008, 12:12 PM
#4
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?
-
July 30th, 2008, 12:50 PM
#5
Re: Press a keyboard key
One of the correct ways to do it is
Code:
if ( e.KeyCode == Keys.Enter ) {...
Thats all about
 Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
My latest articles :
Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
-
July 30th, 2008, 05:24 PM
#6
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|