Follow Me On...

Entries in C# (1)

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.