84

I have developed a User Control in Visual Studio (WinForms C#) and have a question.

I need the user of my User Control to be able to change certain string values and I would like them to be able to add the user control to their Form and click on it to bring up the Properties Pane where my User Control's custom properties will be displayed.

How can I have my own custom properties for my user control? For example:

My user control contains a TextBox, and I would like the user to be able to change the value of that TextBox via a property named "Text" or "Value" in the properties at Design-Time.

JYelton
  • 35,664
  • 27
  • 132
  • 191
jay_t55
  • 11,362
  • 28
  • 103
  • 174
  • See Article here -- [Creating Custom Controls in C#](http://www.c-sharpcorner.com/UploadFile/f5a10c/creating-custom-controls-in-C-Sharp/) – Pritam Zope Feb 01 '16 at 08:11
  • How will your control CONTAINS TextBox control ? Using class inheritance or only a container ? Can you give some codes as Custom user control class definition ? – schlebe Nov 23 '21 at 09:34

3 Answers3

122

You do this via attributes on the properties, like this:

[Description("Test text displayed in the textbox"),Category("Data")] 
public string Text {
  get => myInnerTextBox.Text;
  set => myInnerTextBox.Text = value;
}

The category is the heading under which the property will appear in the Visual Studio Properties box. Here's a more complete MSDN reference, including a list of categories.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 3
    My VS2010 crashes each time I want to generate the project with this code, either if I include System.ComponentModel or not :& – Grigory Kornilov Nov 07 '13 at 16:51
  • This works for most custom properties, but specifically for overriding the `Text` property (or any other properties that require an override, I assume) Hans' answer below is what will work – derekantrican May 31 '19 at 15:07
  • how can I set default value to this property? – Osama Adel Apr 30 '21 at 12:42
54

It is very simple, just add a property:

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

Using the Text property is a bit trickier, the UserControl class intentionally hides it. You'll need to override the attributes to put it back in working order:

[Browsable(true), EditorBrowsable(EditorBrowsableState.Always), Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text {
  get { return textBox1.Text; }
  set { textBox1.Text = value; }
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    Thank you so much! Completely forgot about this one and it was driving me nuts trying to recall. – Damian Mar 07 '17 at 21:26
  • 1
    This should be the actual accepted answer. `Text` is handled in a special way – derekantrican May 30 '19 at 21:39
  • I don't really understand why it gets so many votes either. The compiler generates a warning for it, presumably most visitors figure out to stick `new` before it. Which is normally a very bad idea but does work by accident for a UC. – Hans Passant May 30 '19 at 21:47
  • @HansPassant The only problem I'm facing is that if I use the code the same as you have it (for the `Text` property) and change it in my parent control's properties pane, it doesn't add the code to the `Form.Designer.cs` and therefore has no effect. The designer view updates, but when I start the application, the change isn't there – derekantrican May 31 '19 at 15:00
  • Oh, actually after more Googling I just found the solution: https://www.codeproject.com/Tips/403782/Making-an-overridden-Text-property-visible-in-the . Specifically for overriding the `Text` property, you should also have the attribute `DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)` for the changes to get saved from the designer – derekantrican May 31 '19 at 15:05
6

Just add public properties to the user control.

You can add [Category("MyCategory")] and [Description("A property that controls the wossname")] attributes to make it nicer, but as long as it's a public property it should show up in the property panel.

Community
  • 1
  • 1
Jason Williams
  • 56,972
  • 11
  • 108
  • 137