There is a rectangle that can be moved. It is necessary that when you click on it, a window opens. Tried to do so, but does not work.

private void rect1_Click(object sender, EventArgs e) { MessageBox.Show("lol"); } 

Closed due to the fact that the essence of the question is not clear to the participants of the Vadim Ovchinnikov , Igor , αλεχολυτ , Denis Bubnov , fori1ton Jan 20, 17 at 18:18 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 3
    what rect1 ? And where do you use this handler? - Grundy
  • one
    @Grundy, yes he does not use it. Just wrote and that's it. And hwa close everything. - Qwertiy
  • @Qwertiy, no need to slander, I haven’t closed this question yet :) - Grundy
  • Said @Grundy and added a second vote: D - Qwertiy
  • @Qwertiy, it was still not me :) - Grundy

2 answers 2

You must first subscribe to an event (if it exists at all). Perhaps so:

 rect1.Click += rect1_Click; 
  • Thank. I'll try. - Ilya Danilov

In order for a dynamically created object to react to a mouse click, you need to specify the method that will be called when an event is triggered. For example:

 obj.Click += obj_Click; 

In your case, this must be done for the object rect1

It is important to note that not every method can be attached to the delegate chain: the method must accept the following object and EventArgs parameters

You can also attach a method as follows using lambda expressions

 obj.Click += (sender, e) => MessageBox.Show("obj click!"); 
  • @IlyaDanilov if you are given an exhaustive answer, mark it as true (checkmark the selected answer). - Vadim Prokopchuk