C#NET2008 KeyPreview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lenniekuah
    New Member
    • Oct 2006
    • 126

    C#NET2008 KeyPreview

    Hi Good Guys,
    I am back with interesting problem.
    I need your help. Please help me.

    As instructed to allow users to exit from FORM when press ESC key.

    The coding generated these 2 Error Messages:
    Error 1 The best overloaded method match for 'System.Windows .Forms.KeyEvent Args.KeyEventAr gs(System.Windo ws.Forms.Keys)' has some invalid arguments

    Error 2 Argument '1': cannot convert from 'method group' to 'System.Windows .Forms.Keys'

    Here are the coding that causes it:

    Code:
    private void FrmSales_Load(object sender, EventArgs e)
    {
    
      this.KeyPreview = true;
      this.KeyDown += new System.Windows.Forms.KeyEventArgs(this.FrmMyOwnCoding_KeyDown); 
    
    }
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    KeyDown is an event of delegate type KeyEventHandler and not KeyEventArgs!

    Code:
    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FrmMyOwnCoding_KeyDown); 
    
    // Instead of
    
    this.KeyDown += new System.Windows.Forms.KeyEventArgs(this.FrmMyOwnCoding_KeyDown); //WRONG!

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      To save yourself the trouble of having to remember the delegate types for all the events, you can just type the this.KeyDown += and push TAB twice. It will generate the delegate and the method for you.

      Alternatively, you can set this from the designer. Select the form's title bar, and in the Properties window, select the Events button. Then find your event, and double click it. It will create a method, and attach a handler in the .Designer.cs file.

      Comment

      • lenniekuah
        New Member
        • Oct 2006
        • 126

        #4
        Hullo Bro Christian,
        Praise the Lord for your generousity in sharing information with me to help me. I have learned something new logically. Thank you very much.

        May the Lord Jesus Bless you and Family.


        Cheers,
        Lennie

        Comment

        • lenniekuah
          New Member
          • Oct 2006
          • 126

          #5
          Hi Curtis,
          Thank you very much for sharing information to help me. I have also tried out your suggestion and it working very well as well. Awesome....Yaa. ...hoooo

          Comment

          Working...