C Constantly updating the Sharp form?

Through Visual Studio, you can easily create a method for an event (button click, etc.). Is there a way to create a method for several events, for example, a method that will run at any time when one of the three buttons is pressed, or one of many text fields has been entered?

+3
source share
4 answers

Once you have created a method, as soon as you can use it for any other event that has a similar signature.

From the constructor, do not double-click this method, but instead drop the list for any existing event handler that can be assigned to your selected event.

@lcfseth, , object sender.

+5

, ( )

firstEvent += MyMethod;
secondEvent += MyMethod
+1

, (.. ) . , , .

Button1.Click += new EventHandler(this.AnyButton_Click);
Button2.Click += new EventHandler(this.AnyButton_Click);
Button3.Click += new EventHandler(this.AnyButton_Click);

"", , , , "" - , , , , , (.. Button ).

void AnyButton_Click(Object sender, EventArgs e)
{
    Button clickedButton = (Button)sender; // unbox the sender
    MessageBox.Show("You just clicked button " clickedButton.Text);
}

(MSDN)

0

You can assign the same Click event handler to multiple controls. the sender indicates the control that raised the event.

0
source

All Articles