问题描述
我想创建一个继承c#中textBox控件的类。新类包含一个具有3个可能值的新属性(大小)(最大化,最小化,居中)
我该怎么做?
非常感谢!
Nas
Hi,
I want to create a class which inherits textBox control in c# . the new class contains a new property (size)with 3 possible values (maximize ,minimize,center )
How I can do that?
Thanks a lot!
N.a.s
推荐答案
public class MyTextBox : System.Windows.Forms.TextBox {
public enum SizeBehaviorOption { Maximize, Minimize, Center, }
public SizeBehaviorOption SizeBehavior {
get { return this.sizeBehavior; }
set {
if (this.sizeBehavior == value) return;
this.sizeBehavior = value;
// now, do something to adjust your control size/location/something, as the behavior has been changed
}
}
SizeBehaviorOption sizeBehavior;
} //class MyTextBox
注意属性的 setter 的作用。这是属性背后的主要思想:使用setter(有时是getter,很少)为属性值的读/写操作添加一些副作用。
但是,我怀疑所有这些活动都是多余的。您通常可以使用对接容器,对接和填充属性(在某些情况下为锚点)实现所需的行为。更多细节取决于您要使用的UI库。下次提问时,请务必标记这个重要的细节。
Note the role of the setter of the property. This is the main idea behind property: a setter (sometime getter, rarely) is used to add some side effect to read/write operation for the property values.
However, I suspect all this activity is redundant. You can usually implement desired behavior using docking containers, docking and padding properties, in some cases anchors. The further detail depend on UI library you want to use. Next time you ask a question, always tag this important detail.
myTextbox myt = new myTextbox();
myt.Size = Sizes.Maximize;
Controls.Add(myt);
...
public enum Sizes
{
Maximize,
Minimize,
Centre,
}
public class myTextbox : TextBox
{
public new Sizes Size { get; set; }
}
这篇关于继承文本框控件-c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!