Follow Me On...
« Eclipse RegEx Syntax & Capture Groups | Main | Java Packaging / Deployment Tools I Recently Came Across »
Monday
Feb052007

C# User Control Properties

Below are a few examples of how to define public properties that will show up in the Property Editor for a user control when selected in the Designer View. Basically, you define a setter and getter and then add the Category, Description, and DefaultValue tags.

        /// <summary>
        /// Gets or sets progress indicator text label.
        /// </summary>
        [Category("Appearance")]
        [DescriptionAttribute("Defines the label text that appears under the control.")]
        [DefaultValue(typeof(string), "Please Wait...")]
        public string LabelText
        {
            get { return label1.Text; }
            set { label1.Text = value; }
        }

The example below overrides the default Text Property.

        /// <summary>
        /// Gets or sets progress indicator text label.
        /// </summary>
        [Category("Appearance")]
        [DescriptionAttribute("Defines the label text that appears under the control.")]
        [DefaultValue(typeof(string), "Please Wait...")]
        public override string Text
        {
            get { return label1.Text; }
            set { label1.Text = value; }
        }

An example for defining a color property.

        /// <summary>
        /// Gets or sets the BackColor of this control.
        /// </summary>
        [Category("Appearance")]
        [DescriptionAttribute("Sets the background color")]
        [DefaultValue(typeof(System.Drawing.Color), "Transparent")]
        public override System.Drawing.Color BackColor
        {
            get { return this.BackColor; }
            set { this.BackColor = value; }
        }

Other Notes:


  • I experimented with specifiying a custom category such as “MyAppearance” and when i did so the properties didn’t show up.
  • You have to do rebuild your solution in order to make the new user control properties show up.

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.