继承文本框控件-c# [英] inherit textbox control -c#

查看:99
本文介绍了继承文本框控件-c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想创建一个继承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

推荐答案

这篇详细的文章 Visual C#.NET中的自定义控件 [ ^ ]应该让您走上正确的道路。之后,您可以将其作为家庭作业继承 Textbox 控件并创建新属性/枚举。
This detailed article Custom Controls in Visual C# .NET[^] should put you on the right path. After that, you can take it as an homework task to inherit the Textbox control and create a new property / enumeration.


请参阅我在对这个问题的评论中给出的建议。并遵循它 - 没有别的办法。



你需要的东西会是这样的:

Please see my advice given in my comment to the question. And follow it — there is no other way.

What you need will look something like this:
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库。下次提问时,请务必标记这个重要的细节。



-SA



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.

—SA


你不能 - 或者至少你不应该。

名称Size已经是从Control派生的所有类的属性,如果你将它覆盖到不同的类型,那么它就变得很难了使用。



但是,实际上这很容易:

You can''t - or at least you shouldn''t.
The name Size is already a property of all classes derived from Control and if you override it to a different type, then it becomes difficult to work with.

But, to actually do it is easy:
    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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆