Click here to Skip to main content
14,920,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically i am trying to make a program that can handle keyboard events, but it keeps spitting out the error.

cannot assign to 'keydown' because it is a 'method group'


The error
Form1.Keypress(object, KeyPressEventsArg)' hides inherited method 'Control.KeyPress'. Use the new Keboard if hiding was intended 


The Two Programs i used were
private void KeyDown(object sender, KeyEventArgs e)
       {
           if(e.KeyCode == Keys.Enter)
           {
               MessageBox.Show("Enter Key Pressed");
           }
       }

       private void KeyUp(object sender, KeyEventArgs e)
       {

       }

       private void KeyPress(object sender, KeyPressEventArgs e)
       {
           if(e.KeyChar == (char)Keys.Enter)
           {
               MessageBox.Show("Enter was pressed");
           }
       }


The other one was
private new void KeyDown(object sender, KeyEventArgs e)
        {
            Input.ChangedState(e.KeyCode, true);
        }

        private new void KeyUp(object sender, KeyEventArgs e)
        {
            Input.ChangedState(e.KeyCode, false);
        }


public static Hashtable KeyBoard = new Hashtable();

public static bool KeyPressed(Keys key)
{
    if (KeyBoard[key] == null)
    {
        return false;
    }
    else
    {
        return (bool)KeyBoard[key];
    }

}
public static void ChangedState(Keys key, bool state)
{
    KeyBoard[key] = state;
}


What I have tried:

I tried using the solutions vs provided but none of them work
Posted
Updated 22-May-20 8:13am

Your methods' names interfere either with existing events, or existing methods in parent classes.
I would suggest to rename your methods OnKeyDown, OnKeyUp; OnKeyPress etc. Or, if your method interferes with an existing method in a parent class, just declare it as override (if said method is overridable, of course).
   
There are plenty of working examples in the documentation:
Control.KeyPress Event (System.Windows.Forms) | Microsoft Docs[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900