CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2008
    Posts
    52

    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.

  2. #2
    Join Date
    Aug 2005
    Location
    Seattle, Wa
    Posts
    179

    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)
    {

  3. #3
    Join Date
    Jul 2008
    Posts
    29

    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.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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?

  5. #5
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    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

  6. #6
    Join Date
    Jun 2008
    Posts
    52

    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
  •  





Click Here to Expand Forum to Full Width

Featured