如何将新属性添加到文本框控件 [英] How to add new property to the text box control

查看:76
本文介绍了如何将新属性添加到文本框控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友,



我在Windows窗体中有一个文本框。有没有办法向该文本框添加新属性。强制属性等属性为True和False。如果设置属性必须为true ..文本框将显示一个红色astrerik图像。

我可以这样做....

Hi Friends,

I have a text box in the windows form. Is there any way to add new property to that text box. The property like mandatory attributes are True and False. If set property Mandatory true .. the text box will show one red astrerik image.
Can i do like this....

推荐答案

这个控件只是一个类,而不是密封一个。所以,当然,你可以创建一个派生类并添加你想要的任何属性,以及任何其他成员:

This control is nothing but a class, not a sealed one. So, naturally, you can create a derived class and add any property you want, as well as any other member:
class MyTextBox : System.Windows.Forms.TextBox {
    string MyNewProperty { get; set; }
    // ...
} // class MyTextBox









现在,你的强制你真的需要完全成熟的属性功能,比如 setter







Now, with your Mandatory you really need fully-fledged property features, such as the setter:

class MyTextBox : System.Windows.Forms.TextBox {
    internal bool Mandatory {
        get { return mandatory; }
        set {
            if (value == mandatory) return;
            mandatory = value;
            Invalidate(); // important step; that's why you need property
        }
    }
    bool mandatory;
    protected override void OnPaint(PaintEventArgs eventArgs) {
        base.OnPaint(eventArgs);
        if (mandatory) {
            RenderItOneWay(eventArgs.Graphics);
        } else {
            RenderItAnotherWay(eventArgs.Graphics);
        }
    }
    // ...
} // class MyTextBox





有这个主意吗?



-SA


这篇关于如何将新属性添加到文本框控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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