The sender
is the type Object
, and has the functionality when the event runs, receive information from such control, for example if you click on a Button
he brings the information of this Button.
Example:
In one form has two Button
, a has on your Text
with Message 1 and Message 2 respecitvamente the ButMensagem1
was placed on your property Tag
the number 1 and in the ButMensagem2
the number was placed 2. How to recover this information:
Code:
Obs: Remembering that the event Click
will be the same for both Button
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 recover which Button
was clicked and retrieves the settings, events and properties. In case the property was recovered Tag
and with it the routine makes the decision to show the Message 1 or Message 2, depending on the Button that was triggered.