0

It gives me such an error and I can't seem to work out what the problem is.

private void Form1.KeyDown(object sender, KeyEventArgs e)  // **THE ERROR HERE**   
    {
        if (ListBox1.Items.Contains(e.KeyCode))
        {
            ListBox1.Items.Remove(e.KeyCode);
            ListBox1.Refresh();
            if (timer1.Interval > 400)
            {

                timer1.Interval -= 10;
            }
            if (timer1.Interval > 250)
            {
                timer1.Interval -= 7;

            }
            if (timer1.Interval > 100)
            {
                timer1.Interval -= 2;

            }
            difficultyProgressBar.Value = 800 - timer1.Interval;
            stats.Update(true);


        }
        else
        {

            stats.Update(false);

        }


        correctLabel.Text = "Correct: " + stats.correct;
        missedLabel.Text = "Missed: " + stats.missed;
        totalLabel.Text = "Total: " + stats.total;
        accuracyLabel.Text = "Accuracy: " + stats.accuracy + "%";


    }

It's code from some tutorial, so it should be working. What could possibly be the problem?

eksponente
  • 49
  • 2
  • 9

4 Answers4

6

You first line should look like this:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    ...
}

Without the dot.

The dot makes the compiler think you referring to the KeyDown event of the form, while you just want a method which listens to that event.

SimpleVar
  • 14,044
  • 4
  • 38
  • 60
  • 3
    No, the dot makes the compiler believe it is trying to implement an external interface, hence the error about not being able to mark the method as "private." – aquinas May 15 '12 at 19:43
  • That doesn't contradict what I said. The compiler sees Form1.KeyDown as the KeyDown event of Form1 - implementing such would mean trying to implement external interface, yes, but in general, the dot means "the member of". I was giving an interpretation, just not a full one. +1 for elaborating, though. :) – SimpleVar May 15 '12 at 19:45
3

Interfacename.methodname syntax is reserved for explicit interface implementations. Interfaces only contain public methods and thus "private" is illegal.

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
1

In VB, when you declare an event handler, you tack on Handles <Class>.<Event> and it automagically hooks everything up for you. In C#, you event handlers are just methods that get attached to events. So you should rename the method name to Form1_KeyDown instead. However, you still need to hook it up (either through the Visual Studio Designer or in the code).

public class Form1 : Form
{
    ...
    public Form1()
    {
        InitializeComponent();
        this.KeyDown += new KeyEventHandler(this.Form1_KeyDown);
    }
    ...
    private void Form1_KeyDown(object sender, KeyEventArgs e) { ... }
}
SPFiredrake
  • 3,852
  • 18
  • 26
0

I just got this error and found I was missing a closing bracket in code above the flagged method. The bad bracket pairs messed up the recognition of private bool method...