-2

Please help me, I have the code:

this.panel1.MouseMove += new MouseEventHandler(panel1.MouseMove);

But visual studio is giving me the error

error The event 'control.mousemove' can only appear on the left hand side of += or -=

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 1
    `new MouseEventHandler(panel1_MouseMove);` If you don't have a `panel1_MouseMove` method, you need to create one. – LarsTech Mar 16 '21 at 13:39
  • 1
    Does this answer your question [-event- can only appear on the left hand side of += or -=](https://stackoverflow.com/questions/4496799/event-can-only-appear-on-the-left-hand-side-of-or) and or [this](https://stackoverflow.com/questions/756237/c-raising-an-inherited-event) answer? – Trevor Mar 16 '21 at 13:41

2 Answers2

1

This is becaouse you have pannel1.MouseMove on the right hand side as well:

this.panel1.MouseMove += new MouseEventHandler(panel1.MouseMove);
                                                  ^
                                                HERE

The correct way to create a handler is to provide method to the constructor you used, like:

this.panel1.MouseMove += new MouseEventHandler(this.panel1_MouseMove);

where panel1_MouseMove is of the form:

private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
  // code here..
}

See this for reference.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • This gives me the same error as if i do it this.panel1.MouseMove += new MouseEventHandler(this.panel1.MouseMove); But if i do this.panel1.MouseMove += new MouseEventHandler(this.panel1_MouseMove); Error CS1061 'Form1' does not contain a definition for 'panel1_MouseMove' and no accessible extension method 'panel1_MouseMove' accepting a first argument of type 'Form1' could be found (are you missing a using directive or an assembly reference?) – NRG GAMING Mar 16 '21 at 14:14
  • @NRGGAMING Then define method like I posted: `private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e){}` – Michał Turczyn Mar 16 '21 at 14:25
0

If your mousemove handler code is short, it can be neater to define it inline, like:

panel1.MouseMove += (sender, e) => {
  //handler code here, e.g. 
  this.Text = $"Mouse is at coords {e.Location}";
};
Caius Jard
  • 72,509
  • 5
  • 49
  • 80