Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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....
Posted
Comments
Sergey Alexandrovich Kryukov 28-Dec-12 1:49am    
What did you try?
—SA
Sergey Alexandrovich Kryukov 28-Dec-12 1:55am    
What did you try so far?
Not clear what exactly do you want to achieve. How a property is related to attributes? (Probably you don't mean .NET attributes, but something else.) Anyway, I told you how to add a property, you think how to use it. Or ask a follow-up question, but try to be more clear then so far.
—SA
Sergey Alexandrovich Kryukov 28-Dec-12 2:04am    
Ah, I got it. Please see my updated answer. Problem completely solved.
—SA
Sergey Alexandrovich Kryukov 28-Dec-12 2:07am    
You confused me with your term "attributes". Remember, "Attribute" is a very certain and special thing in .NET, a way to pass additional metadata which can be read by Reflection. Learn about them; this is important stuff.
Good luck,
—SA
rajugknr 28-Dec-12 2:12am    
Thanks

1 solution

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:
C#
class MyTextBox : System.Windows.Forms.TextBox {
    string MyNewProperty { get; set; }
    // ...
} // class MyTextBox


[EDIT]

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

C#
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


Got the idea?

—SA
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900