views:

56

answers:

2

I've written a custom control which mimics the appearance of a table header. The columns it draws are given by a collection of objects with a Text string property and a Width int property. Both have Browsable, Category, DefaultValue and Description attributes added. In the designer, the collection is editable and the user can set the Text and Width as they would for a control, and the attributes are honoured as I'd hoped. However, the user can't edit the name of the variable; the generated code looks like this:

HeaderMimicColumn headerMimicColumn1 = new HeaderMimicColumn();
HeaderMimicColumn headerMimicColumn2 = new HeaderMimicColumn();
HeaderMimicColumn headerMimicColumn3 = new HeaderMimicColumn();
...

Is it possible to make it so the user can specify the names of the variables used for the columns, in the designer? If so, how?

EDIT: Just to be clear, HeaderMimicColumn doesn't derive from System.Windows.Forms.Control, it's just a plain class with those two properties and a pair of events which fire when they change.

I should probably also explain that the collection has its DesignerSerializationVisibility set to Contents.

A: 

The user should already be able to do this by doing the following

  1. Hit F4 in the designer (brings up the properties window)
  2. Select headerMimicColumn1 in the combobox dropdown
  3. Change the name field
JaredPar
the header mimic columns don't show up in the combobox - perhaps because they don't derive from System.Windows.Forms.Control ?
Simon
+1  A: 

When non-Component objects are part of a collection that is persisted through the DesignerSerializationVisibility.Contents mechanism, then each object in that collection does not have a separate identity. It is simply part of the parent collection. Hence, you can't set their names, since they literally don't exist as separate objects.

If you want your HeaderMimicColumn to be part of your collection AND separately addressable (like Columns in a ListView), you will at least have to derive them from System.ComponentModel.Component. And then deal with the other problems that that will bring :)

Grammarian
Great, thanks. For reference, to get the behaviour I wanted, I needed to add [DesignTimeVisible(false)] and [ToolboxItem(false)] to the class as well.
Simon