我希望有一个简单的 C# 问题!向按钮添加附加属性

发布于 2024-09-29 14:20:45 字数 546 浏览 1 评论 0原文

你好 在 Windows 窗体(不是 WPF)上,我在 Flowlayout 上动态创建按钮,我想向它们添加一些属性,以便将其他值(int 和 string)与按钮一起存储以供以后使用。

            Button bn = new Button();
            bn.Text = "mybutton";
            bn.Name = "mybutton";
            toolTip1.SetToolTip(bn, "some tip");
            bn.Location = new Point(200, 200);
            bn.Size = new Size(110, 30);
            bn.BackColor = SystemColors.Control;
            bn.Show();
            flowLayoutPanel1.Controls.Add(bn);

我想为每个按钮存储大约 6 个值,因为每个按钮的值都不同。

可以这样做吗?

hi
on a windows form (not WPF) I dynamically create buttons on a flowlayout and I would like to add some properties to them simply to store other values (int and string) with the buttons for latter use.

            Button bn = new Button();
            bn.Text = "mybutton";
            bn.Name = "mybutton";
            toolTip1.SetToolTip(bn, "some tip");
            bn.Location = new Point(200, 200);
            bn.Size = new Size(110, 30);
            bn.BackColor = SystemColors.Control;
            bn.Show();
            flowLayoutPanel1.Controls.Add(bn);

I have about 6 values I would like to store with each button as it is different for each button..

Can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

爱她像谁 2024-10-06 14:20:45

对于非强类型信息,您可以使用 Tag 属性。否则,我认为你必须子类化。

For non-strongly-typed information, you can possibly use the Tag property. Otherwise, I think you'd have to subclass.

半窗疏影 2024-10-06 14:20:45

源自 Button: 就

public class MyButton : Button
{
  public string ExtraProperty {get;set;}
}

我个人而言,我认为这是糟糕的代码。真的很糟糕的代码。

Derive from Button:

public class MyButton : Button
{
  public string ExtraProperty {get;set;}
}

Personally, I think this is bad code. Really bad code.

辞慾 2024-10-06 14:20:45

是的。您可以将这样的数据分配给 Button.Tag 属性(继承自 控制)。该属性的类型为对象,因此您可以为其分配任何您想要的内容。

或者,您可以从 Button 继承。

Yes. You can assign data like this to the Button.Tag property (inherited from Control). This property is typed as an object so you can assign anything you want to it.

Alternative, you could inherit from Button.

过潦 2024-10-06 14:20:45

与所有 WinForms 控件一样,Button 也有一个 Tag 属性,可用于存储任意对象。

public struct MyButtonData {
    public int myInt;
    public string myString;
}

...

bn.Tag = new MyButtonData() {myInt = 3, myString = "Hello World"};

...

var data = (MyButtonData)bn.Tag;

Like all WinForms controls, Button also has a Tag property, which can be used to store arbitrary objects.

public struct MyButtonData {
    public int myInt;
    public string myString;
}

...

bn.Tag = new MyButtonData() {myInt = 3, myString = "Hello World"};

...

var data = (MyButtonData)bn.Tag;
假扮的天使 2024-10-06 14:20:45

您可以:

  • 创建一个从 Button 派生的控件,然后添加其他属性。
  • 创建一个类来封装要分配给每个按钮的数据,实例化该类,然后将控件的“Tag”属性指向实例化的对象。

Tag 属性就是为此目的而设计的。

You can either:

  • Create a control, derived from Button, and add the additional properties.
  • Create a class to encapsulate the data you want to assign to the each button, instantiate the class, and then point the control's "Tag" property at the instantiated object.

The Tag property was designed for this very purpose.

自我难过 2024-10-06 14:20:45

在这种情况下,您想要做的事情是创建一个自定义控件。使用自定义控件,您比使用标准控件拥有更多的自由。您不仅可以继承您正在构建自定义控件的现有控件的所有功能。您还将有机会向自定义控件添加更多功能和属性。

来源: 微软 - Devloper Network。
https://msdn.microsoft.com/en -us/library/ff723977(v=表达式.40).aspx

Something you would like to do in this case would be to create a custom control. With a custom control, you have more freedom than with the standard control. Not only will you inherit all the functionality from the existing control you are building your custom control on. You will also have the opportunity to add more functionality and properties to your custom control.

Source: Microsoft - Devloper Network.
https://msdn.microsoft.com/en-us/library/ff723977(v=expression.40).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文