I have one Windows Form and one User Control. The UserControl has three Buttons.
I want to know which Buttons of my UserControl has been clicked.
I need to get the name of Button clicked, to start a specific procedure.
In Form1:
if(User-Control1.Button1.clicked)
Messagebox.show(Button.name);
if(Button2.clicked)
Messagebox.show(Button.name);
Consider this simple UserControl:
You have 3 Buttons and 3 TextBox Controls used to input some values:
The 3 Buttons are named btnAddTask1
, btnAddTask2
and btnAddTask3
.
The 3 TextBoxes are named txtTaskName
, txtInput2a
and txtInput2b
.
In the designer, select all 3 Buttons and, in the PropertyGrid, assign a Click
event handler, the same for all Buttons. Give it a name that makes sense (here, the handler is named btnActionTasks
)
Add a public Custom event:
public event EventHandler<ActionTaskEventArgs> ActionTaskClicked;
Where ActionTaskEventArgs
is a custom class derived from EventArgs
, which will return some values when the event is raised, clicking one of the Buttons (see in code).
When one of the Buttons is clicked, you raise the event, passing in the custom EventArgs
some values related to the current Input of the TextBoxes (or whatever else you need).
The sender
object is set to this
, the UserControl's instance, since you may have more than one UserControl of this type in your UI.
One of the custom EventArgs
properties is set to the name of the Button Control that raised the event; other values are also passed to the subscribers of the event (here, taken from the TextBoxes).
In your Form, after you have dropped an UserControl from the ToolBox, subscribe to its public ActionTaskClicked
event as usual, using the PropertyGrid, or manually, as you prefer.
When the event is raised, because an User clicked a Button, you are notified and you can read the value passed in the event arguments as usual:
public partial class UCButtonActions : UserControl
{
public event EventHandler<ActionTaskEventArgs> ActionTaskClicked;
public UCButtonActions() => InitializeComponent();
private void btnActionTasks_Click(object sender, EventArgs e)
{
string buttonName = (sender as Control).Name;
// Or use the Text, the Tag or whatever other value
// string buttonTag = (sender as Control).Tag;
if (int.TryParse(txtInput2b.Text, out int value)){
var args = new ActionTaskEventArgs(buttonName, value, txtInput2a.Text);
ActionTaskClicked?.Invoke(this, args);
}
}
public class ActionTaskEventArgs : EventArgs
{
public ActionTaskEventArgs(string taskName, int value, string svalue)
{
TaskName = taskName;
TaskNumericValue = value;
TaskStringValue = svalue;
}
public string TaskName { get; }
public int TaskNumericValue { get; }
public string TaskStringValue { get; }
}
}
In the Form class, the event handler returns some values in the UserControl's ActionTaskEventArgs
:
private void ucButtonActions1_ActionTaskClicked(object sender, UCButtonActions.ActionTaskEventArgs e)
{
string message = $"You clicked: {e.TaskName}\r\n"+
$"The String Value is: {e.TaskStringValue}\r\n"+
$"The Numeric Value is: {e.TaskNumericValue}\r\n";
MessageBox.Show(message);
}
This is how it works: