Search code examples
c#.netuser-controlswindows-forms-designervisual-studio-designer

Visual Studio User Contol Design


If you have a user control with a field like public int number = 10; can you make that value come up in the properties box when you use the designer in VS 2010 and C#?


Solution

  • You have to turn your variable into a property. Try adding Get Set and sometimes you have to add the proper attributes (referencing the System.ComponentModel class)

    private int _Number = 10;
    
    [DefaultValue(10)]
    [Description("This is a number.")]
    public int Number
    {
      get { return _Number; }
      set { _Number = value; }
    }
    

    Note: The DefaultValue attribute is for setting the designer to bold or not. It doesn't actually set the default value.