我想知道如何添加.customString到PictureBox对象。
喜欢的东西:C#添加一个选项到PictureBox
PictureBox box = new PictureBox();
box.CustomString = "string here";
再后来,我将访问它。
MessageBox.Show(boxname.CustomString);
谢谢。
我想知道如何添加.customString到PictureBox对象。
喜欢的东西:C#添加一个选项到PictureBox
PictureBox box = new PictureBox();
box.CustomString = "string here";
再后来,我将访问它。
MessageBox.Show(boxname.CustomString);
谢谢。
:
PictureBox box = new PictureBox();
box.Tag = "string here";
,后来从PictureBox
派生MyCustomPictureBox
并将新属性添加到您的派生版本中:
public class MyCustomPictureBox : PictureBox
{
public string CustomString {get; set;}
}
做到这一点最简单的方法是使用Tag
属性:如果您想将属性添加到现有的控制最好的办法
MessageBox.Show((string)box.Tag);
public class MyPictureBox : PictureBox
{
public MyPictureBox(...) :base(....) {} // duplicated ctors
public string CustomString {get; set;}
}
现在,使用它会有点棘手。如果通过在Winforms设计器中拖放它来创建原始图片框,则必须进入myform.designer.cs文件,并将“PictureBox”的实例替换为“MyPictureBox”
您可以创建一个名为MyPictureBox的新类,它来自PictureBox。在新课程中,您可以添加您的自定义属性。像下面的东西。
public class MyPictureBox : PictureBox
{
public MyPictureBox():base()
{}
public string CustomString
{
get{}
set{}
}
}
现在你可以像使用PictureBox一样使用新类了,只不过你的自定义属性/逻辑不同。
享受!
谢谢。这有帮助。 – Semas 2010-08-10 18:58:32