C# 为 PictureBox 添加一个选项

发布于 2024-09-13 08:55:07 字数 245 浏览 5 评论 0原文

我想知道如何将 .customString 添加到 PictureBox 对象。
比如:

PictureBox box = new PictureBox();
box.CustomString = "string here";

然后我就会访问它。

MessageBox.Show(boxname.CustomString);

谢谢。

i was wondering how could i add .customString to PictureBox object.
Something like:

PictureBox box = new PictureBox();
box.CustomString = "string here";

And then later on i would be access it.

MessageBox.Show(boxname.CustomString);

Thank you.

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

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

发布评论

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

评论(4

木緿 2024-09-20 08:55:07

如果您想向现有控件添加属性,最好的方法是从 PictureBox 派生 MyCustomPictureBox 并将新属性添加到您的派生版本:

public class MyCustomPictureBox : PictureBox
{

    public string CustomString {get; set;}

}

If you want to add a property to an existing control the best way would be to derive MyCustomPictureBox from PictureBox and add the new property to your derived version:

public class MyCustomPictureBox : PictureBox
{

    public string CustomString {get; set;}

}
贪恋 2024-09-20 08:55:07

最简单的方法是使用 Tag 属性:

PictureBox box = new PictureBox();
box.Tag = "string here";

然后:

MessageBox.Show((string)box.Tag);

The easiest way to do it is to use the Tag property:

PictureBox box = new PictureBox();
box.Tag = "string here";

And, later:

MessageBox.Show((string)box.Tag);
阿楠 2024-09-20 08:55:07
public class MyPictureBox : PictureBox
{
   public MyPictureBox(...) :base(....) {}   // duplicated ctors

   public string   CustomString {get; set;}
}

现在,使用它会有点棘手。如果您通过在 Winforms 设计器中拖放来创建原始图片框,那么您必须进入 myform.designer.cs 文件,并将“PictureBox”实例替换为“MyPictureBox”

public class MyPictureBox : PictureBox
{
   public MyPictureBox(...) :base(....) {}   // duplicated ctors

   public string   CustomString {get; set;}
}

Now, using it will be a bit trickier. If you've created the original picturebox by drag'n'dropping it in the Winforms designer, then you'll have to go into the myform.designer.cs file, and replace the instances of "PictureBox" with "MyPictureBox"

归属感 2024-09-20 08:55:07

您可以创建一个名为 MyPictureBox 的新类,它派生自 PictureBox。在新类中,您可以添加自定义属性。像下面这样的东西。

public class MyPictureBox : PictureBox
{
  public MyPictureBox():base()
  {}

  public string CustomString
  {
    get{}
    set{}
  }
}

现在您可以像使用 PictureBox 一样使用新类,唯一的区别是您的类具有自定义属性/逻辑。

享受!

You could create a new class called MyPictureBox which derives from PictureBox. In the new class you can add your custom property. Something like the below.

public class MyPictureBox : PictureBox
{
  public MyPictureBox():base()
  {}

  public string CustomString
  {
    get{}
    set{}
  }
}

Now you can use the new class just like you would the PictureBox only difference is the yours has the custom property / logic.

Enjoy!

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