识别点击事件中的发件人按钮控件

Sae*_*ani 4 c# custom-controls winforms

我做了一个自定义按钮,其中有一个名为的字段Data.

我在运行时以编程方式将此按钮添加到我的winform中,添加时我还为它们定义了一个click事件.好吧,实际上我只有一个方法,我订阅了新添加的按钮到这个方法.

但是在点击事件中,我想访问此Data字段并将其显示为消息框,但似乎我的转换不正确:

    CustomButton_Click(object sender, EventArgs e)
    {
        Button button;
        if (sender is Button)
        {
            button = sender as Button;
        } 

        //How to access "Data" field in the sender button? 
        //button.Data  is not compiling!
    }
Run Code Online (Sandbox Code Playgroud)

更新:

对不起,我说"没有编译" .Data没有出现在知识产权中......

Dav*_*all 6

您需要强制转换为具有"数据"字段的自定义类的类型.

就像是:

YourCustomButton button = sender as YourCustomButton;
Run Code Online (Sandbox Code Playgroud)