gpt4 book ai didi

c# - 在点击事件中识别发件人按钮控件

转载 作者:行者123 更新时间:2023-12-03 13:48:53 24 4
gpt4 key购买 nike

我做了一个自定义按钮,其中有一个名为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!
}

更新:

抱歉,我说“未编译”表示 .Data不会出现在智能感知中...

最佳答案

您需要转换为具有“数据”字段的自定义类的类型。

就像是:

YourCustomButton button = sender as YourCustomButton;

关于c# - 在点击事件中识别发件人按钮控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11387070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com