Click here to Skip to main content
11,813,543 members (45,807 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: (untagged)
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 27-Dec-12 19:45pm
Sergey Alexandrovich Kryukov at 28-Dec-12 1:49am
What did you try?
Sergey Alexandrovich Kryukov at 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.
Sergey Alexandrovich Kryukov at 28-Dec-12 2:04am
Ah, I got it. Please see my updated answer. Problem completely solved.
Sergey Alexandrovich Kryukov at 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,
rajugknr at 28-Dec-12 2:12am
Sergey Alexandrovich Kryukov at 28-Dec-12 2:14am
You are welcome. Are you thinking to accept the answer formally (green button)?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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


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) {
        if (mandatory) {
        } else {
    // ...
} // class MyTextBox

Got the idea?


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

  Print Answers RSS
0 Abhinav S 275
1 Palash Mondal_ 190
2 KrunalRohit 150
4 Manas_Kumar 115
0 OriginalGriff 3,877
1 KrunalRohit 2,526
2 Maciej Los 2,245
3 CPallini 2,240
4 ppolymorphe 1,750

Advertise | Privacy | Mobile
Web04 | 2.8.151002.1 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100