Results 1 to 11 of 11

Thread: c# key events

  1. #1

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    c# key events

    I am still learning c# and now i face a problem.
    How do make my application do things on keyboard commands?
    here is the code i currently use and it does nothing?

    Code:
            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Control == true && e.KeyCode == Keys.I)
                {
                    label1.Text = "Ctrl + I was pressed";
                    // Don't pass message to other controls
                    e.Handled = true;
                }
            }

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: c# key events

    Have you assigned the event handler to the right event? Is it entering the method at all? Place a breakpoint on the if() line and see if it hits it.

  3. #3

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    Re: c# key events

    it dosent seem to hit...
    no errors no nothing :S

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: c# key events

    How are you assigning the event handler?

  5. #5

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    Re: c# key events

    ?
    can you put it a little clearly? i am not understanding

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: c# key events

    Just because you've called it Form1_KeyDown doesn't mean that it'll automatically be called. You need to actually assign it:

    Code:
    public Form1()
    {
      // InitializeComponent() etc.
    
      this.KeyDown += new KeyEventHandler(Form1_KeyDown);
    }
    
    private void Form1_KeyDown(...) etc.

  7. #7

    Thread Starter
    Addicted Member Beasts's Avatar
    Join Date
    Oct 2006
    Posts
    147

    Re: c# key events

    oh i see it now.
    That bit of code wasn't there before in my method.
    When i add it it works fine. Do I have to add it manually every time for each control?

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: c# key events

    Per event per control, yes.

  9. #9
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: c# key events

    Not all events, lets say you add a button and double click on it. The event handler for the click event will be automatically created. But the rest will need to be added manually.

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: c# key events

    That's just the designer adding the same line of code, that you could add yourself, to the .designer.cs file.

  11. #11
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: c# key events

    or you could just right click to your form and then properties, click the events button or the lightning icon and there listed all the events. Just assign a name of the events then hit enter.

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