-1

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);

Form Image

Jimi
  • 29,621
  • 8
  • 43
  • 61
  • This question needs more details and clarity – Serge Apr 25 '21 at 16:40
  • The `sender` object will tell you the name. See first duplicates. Use events to propagate that information back to the container object (the form). See last duplicate. – Peter Duniho Apr 25 '21 at 18:46
  • @Peter Duniho Most of those *duplicates* are not related to the question The only one, possibly, related is the last one, where the accepted answer (the answer that people most often take into consideration) is one of the worst thing I've seen doing in WinForms. The only one correct is the one you posted; you could have linked just that directly. – Jimi Apr 25 '21 at 20:32
  • @Jimi: I disagree. This question is way too broad to start with; there's really not just a single problem the OP is asking about. But to the extent that they've asked, everything they need to know has already been answered on this site, and those answers are found in the duplicates. – Peter Duniho Apr 25 '21 at 20:35
  • @Peter Duniho Sure, the body of the question is, well, lacking, but the title is clear: *How to know in a Form the name of a Button clicked in a UserControl*. The first three duplicates are not related. -- I'm not saying that this is not a duplicate (because it is). I'm saying that the only correct duplicate to this question is your answer in the last linked dupe (I've already mentioned why). -- Maybe link it directly (to explicitly exclude the accepted answer), but the others don't matter. – Jimi Apr 25 '21 at 20:38

3 Answers3

0

you can create this code

private void button_Click(object sender, EventArgs e)
    {
        var buttonName = ((Button)sender).Name;
         Messagebox.show(buttonName);
    }

and connect Click event of every button to this handler.

Serge
  • 40,935
  • 4
  • 18
  • 45
0

If you just need the button name, then a Custom event in UserControl can send the button name as an event argument. The event can be invoked in the click event of the buttons. Pass var name = ((Button)sender).Name in the event.

PW11
  • 11
  • 3
0

Consider this simple UserControl:

UserControl_CustomEvents 1

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:

UserControl CustomEvents 2

Jimi
  • 29,621
  • 8
  • 43
  • 61