Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Control - Custom Properties

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.

like image 455
jay_t55 Avatar asked Jan 17 '10 17:01

jay_t55


People also ask

What is Property in user control?

User Control properties are used to set the values of a User Control from the parent page.

What is custom control user control?

CustomControl is a loosely coupled control w.r.t code and UI while UserControl is a tightly coupled control w.r.t code and UI. When using CustomControl UI can be changed in different projects but for a UserControl UI is fixed and can't have different looks in different project.

What is custom control in user interface design?

What Does Custom Control Mean? Custom control is a control that is not included in the . NET framework library and is instead created by a third-party software vendor or a user. Custom control is a concept used while building both Windows Forms client and ASP.NET Web applications.

How can you add a custom property to a form in C#?

On the menu bar, choose View > Properties Window. The Property Editor dialog box appears. In the text box in the Name column, specify the name of the property. For the Type field of the custom property, choose the appropriate data type.


3 Answers

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.

like image 193
Nick Craver Avatar answered Oct 18 '22 19:10

Nick Craver


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; }
}
like image 41
Hans Passant Avatar answered Oct 18 '22 19:10

Hans Passant


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.

like image 39
Jason Williams Avatar answered Oct 18 '22 17:10

Jason Williams