3

I am trying to add some fields to a custom UserControl that I am making. I have some fields that I like them to be visible in the Properties window of Visual Studio. I tried to use the flags below but I dont see the field in the designer, even after a compile.

How should I do this correctly?

public partial class TosChartControl: UserControl
{

    #region PUBLIC FIELDS

    [Browsable(true)] //Added this but still does not show up
    [Category("Data")]
    [DefaultValue(0)]
    [Description("ID of the Sensor Node")]
    public int NodeId { get; set; }

    #endregion

    public TosChartControl()
    {
        InitializeComponent();
    }
}

I did clean and rebuild the soloution and projects but I cant still see this field in Properties window. Even restarting the Visualstudio didnt help.

Dumbo
  • 13,555
  • 54
  • 184
  • 288
  • 1
    You can join the attributes together through commas [attr1,attr2], (henginy's answer is correct) – Sayse Jul 03 '13 at 12:32
  • So is `NodeId` is coming in property window? I guess it should be there if its attributed with get/set – Sandy Jul 03 '13 at 12:39
  • Yes and should go in Data category – Dumbo Jul 03 '13 at 12:40
  • The simplest explanation is that you are actually dropping an old version of the control on the form, one that doesn't have the NodeId property yet. Be careful to avoid adding the control to the toolbox yourself, that creates a copy which can easily get stale. – Hans Passant Jul 03 '13 at 16:02
  • This is strange because by default all the public properties will be shown in Properties window of VS, there is some case in which you can set the value for some property but it should always show up if it is public. – King King Jul 03 '13 at 16:03

2 Answers2

4

UPDATE: Your public properties are visible in the designer only when it's in another control in the designer. It turns out that you don't need to add this attribute, properties are visible by default in the designer. As far as I understand, when it's in another component's design view, an instance of the user control is created and properties can be shown. Sorry for misleading you in the beginning, I thought it was necessary to add it.


Try this attribute:

[Browsable(true)]

http://msdn.microsoft.com/en-us/library/system.componentmodel.browsableattribute.aspx

henginy
  • 2,041
  • 1
  • 16
  • 27
  • Thanks. Where should I add this attribute? for the class itself or each field? I have some usercontrols (made by others) that they work without this attribute too. – Dumbo Jul 03 '13 at 12:37
  • You would add the attribute to members (not the user control) To NodeId for example – henginy Jul 03 '13 at 12:44
  • yes I did, clean , rebuilt even restart VS but cant see it there – Dumbo Jul 03 '13 at 12:54
  • this is what I use `[Browsable(true), EditorBrowsable(EditorBrowsableState.Always), DefaultValue("None"), Category("Data"), ]` – Sayse Jul 03 '13 at 12:59
  • I added `EditorBrowsable` also but I am still not getting it – Dumbo Jul 03 '13 at 13:14
  • Yeah you where right, those fields will not show up until I drag the control in a winfrom. – Dumbo Jul 04 '13 at 16:42
2

To elaborate on henginy's updated answer:

Be sure that you are looking at an instance of the control you want to modify properties for, and not the definition of the control itself.

To clarify, when you add a property to your TosChartControl class, you won't see the property in the TosChartControl.cs [Design] tab, you will see it where you implement a TosChartControl, such as your Form1.cs [Design] tab, e.g. the containing control to which you have added your custom control.

...Assuming that your Properties window is visible, and that you have the control selected.

What to take away from this lesson:

  • Understanding what the properties window is actually showing you — It's contextual.

  • The difference between the model and the implementation of the model — e.g. Designing the custom control and designing the form that uses the custom control.

Conner Harkness
  • 249
  • 3
  • 7