The sender
is of type Object
, and has the functionality of when the event is executed, receive the information of such control, for example, if you click on a Button
it brings the information of that Button.
Example:
OnaformithastwoButton
,onehasinitsText
withMessage1andMessage2,andButMensagem1
hasbeenplacedinits%withthe1numberandtheTag
wasplaced2number.Howtorecoverthisinformation:
Code:
Rememberthatthe
ButMensagem2
eventwillbethesameforboth
Click
private void ButMensagem_Click(object sender, EventArgs e)
{
//Button butMensagem = (Button)sender;
//ou
Button butMensagem = sender as Button;
switch (((string)butMensagem.Tag))
{
case "1": // mensagem 1
{
MessageBox.Show("Botão Clicado: Mensagem 1", "Clicado", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
case "2": // mensagem 2
{
MessageBox.Show("Botão Clicado: Mensagem 2", "Clicado", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
}
}
That is, using a Cast you retrieve which Button
was clicked and with that retrieves settings, events, and properties. In case the Button
property was retrieved and with this the routine makes the decision to show Message 1 or Message 2 , depending on the Button that was triggered. >