如何在表单中知道在 UserControl 中单击的按钮的名称 [英] How to know in a Form the name of a Button clicked in a UserControl

查看:28
本文介绍了如何在表单中知道在 UserControl 中单击的按钮的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 窗体和一个用户控件.UserControl 有三个按钮.
我想知道我的 UserControl 的哪些按钮被点击了.我需要点击按钮的名称,以启动特定程序.

在 Form1 中:

if(User-Control1.Button1.clicked)Messagebox.show(Button.name);如果(Button2.clicked)Messagebox.show(Button.name);

您有 3 个按钮和 3 个文本框控件用于输入一些值:
这 3 个按钮分别命名为 btnAddTask1btnAddTask2btnAddTask3.
3 个文本框被命名为 txtTaskNametxtInput2atxtInput2b.

在设计器中,选择所有 3 个按钮,然后在 PropertyGrid 中分配一个 Click 事件处理程序,所有按钮都相同.给它一个有意义的名字(这里,处理程序被命名为 btnActionTasks)

添加公共自定义事件:

公共事件EventHandlerActionTaskClicked;

其中 ActionTaskEventArgs 是从 EventArgs 派生的自定义类,它会在引发事件时返回一些值,单击其中一个按钮(参见代码).

当单击其中一个按钮时,您会引发事件,传入自定义 EventArgs 一些与当前文本框输入相关的值(或您需要的任何其他值).

sender 对象设置为 this,即 UserControl 实例,因为您可能有多个在您的 UI 中使用此类型的 UserControl.
自定义 EventArgs 属性之一设置为引发事件的按钮控件的名称;其他值也会传递给事件的订阅者(此处取自 TextBox).

在您的表单中,从 ToolBox 中删除 UserControl 后,像往常一样使用 PropertyGrid 订阅其公共 ActionTaskClicked 事件,或者根据您的喜好手动.
当事件被引发时,因为用户点击了一个按钮,你会收到通知,你可以像往常一样读取事件参数中传递的值:

公共部分类 UCButtonActions : UserControl{公共事件 EventHandlerActionTaskClicked;公共 UCButtonActions() =>初始化组件();private void btnActionTasks_Click(object sender, EventArgs e){string buttonName = (sender as Control).Name;//或者使用文本、标签或任何其他值//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);}}公共类 ActionTaskEventArgs : EventArgs{public ActionTaskEventArgs(string taskName, int value, string svalue){任务名称 = 任务名称;TaskNumericValue = 值;TaskStringValue = svalue;}公共字符串任务名称 { 获取;}公共 int TaskNumericValue { 获取;}公共字符串 TaskStringValue { 获取;}}}

在Form类中,事件处理程序返回UserControl的ActionTaskEventArgs中的一些值:

private void ucButtonActions1_ActionTaskClicked(object sender, UCButtonActions.ActionTaskEventArgs e){string message = $"您点击了:{e.TaskName}\r\n"+$"字符串值为:{e.TaskStringValue}\r\n"+$数值为:{e.TaskNumericValue}\r\n";MessageBox.Show(message);}

这是它的工作原理:

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

解决方案

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 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 a 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 a User clicked a Button, you are notified an 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 中单击的按钮的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆