How to add custom properties in default controls

BenTam 1,761 Reputation points
2024-01-14T08:56:22.24+00:00

Dear All,

We can add custom properties to custom controls in Windows Forms Control Library projects as follows, e.g., the following will a a property MyText in c# code.

public string MyText
{
	get { return textbox1.Text; }
	set { textBox1.Text = value; }
}

I wonder if it is possible to add custom properties to default controls.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,294 questions
{count} votes

Accepted answer
  1. José Pablo Ramirez (a. k. a. webJose) 440 Reputation points
    2024-01-14T09:50:22.8066667+00:00

    Yes, one can further add to controls as they are just classes, like any other class. Create a new class and derive from TextBox:

    public class MyTextBox : TextBox
    {
        [Browsable(true)]
        public string AllCapsText
            => Text.ToUpper();
    }
    

    Now textboxes of the MyTextBox class will have the read-only property AllCapsText ready to be used. The Browsable attribute you see there is one of the many things you can do to enhance the developer experience with your new component. In this case, we are making the property available for viewing in the Properties window of Visual Studio.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.