How to provide custom code for InitializeComponent?
Asked Answered
S

2

13

When you modify column headers of a ListView at design time, the designer generates code to serialize column headers at run-time:

private void InitializeComponent()
{
    this.listView1 = new System.Windows.Forms.ListView();
    this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
    this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
    this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        this.columnHeader1,
        this.columnHeader2
    });
}

How does the forms-designer know that it should call the constructor for each column followed by a call to the AddRange method of the Columns property of the ListView? I need this for a ListView like UserControl I am writing.

Sibling answered 14/10, 2009 at 12:56 Comment(0)
S
6

What I wanted to achieve was to customize the InitializeComponent code produced by my custom component. I found this MSDN article which describes how to do that:

Customizing Code Generation in the .NET Framework Visual Designers

It appears that I need to write a CodeDomSerializer for my component, and generate a collection of CodeExpression's describing my custom initialization code.

Sibling answered 16/10, 2009 at 14:14 Comment(0)
W
4

You can use special attributes to tell the Visual Studio designer how to serialize properties in code. See the MSDN reference for DesignerSerializationVisibilityAttribute for an example. This series of articles also gives a good overview of the various attributes available to extend design time support for custom controls. Hope this helps.

Wivinah answered 14/10, 2009 at 21:6 Comment(1)
Thanks for taking the time to answer, what I need is more than what automatic serialization can provide. I found a MSDN article today described in my post below.Sibling

© 2022 - 2024 — McMap. All rights reserved.