Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8797885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:56:52+00:00 2026-06-13T23:56:52+00:00

I’m trying to customize the Windows Forms Designer’s code generation for InitializeComponent . The

  • 0

I’m trying to customize the Windows Forms Designer’s code generation for InitializeComponent. The MSDN article “Customizing Code Generation in the .NET Framework Visual Designers” contains a section “Controlling Code Generation” that explains the basics of how this can be done.

I’ve closely followed an example in the above article:

//using System.ComponentModel.Design.Serialization;

class SomeFormSerializer : CodeDomSerializer
{
    public override object Serialize(IDesignerSerializationManager manager,
                                     object value)
    {
        // first, let the default serializer do its work:
        var baseSerializer = (CodeDomSerializer)manager.GetSerializer(
                             typeof(Form).BaseType, typeof(CodeDomSerializer));
        object codeObject = baseSerializer.Serialize(manager, value);

        // then, modify the generated CodeDOM -- add a comment as the 1st line:
        if (codeObject is CodeStatementCollection)
        {
            var statements = (CodeStatementCollection)codeObject;
            statements.Insert(0, new CodeCommentStatement("CODEDOM WAS HERE"));
        }

        // finally, return the modified CodeDOM:
        return codeObject;
    }
}

Now I hook this up to my form SomeForm:

[DesignerSerializer(typeof(SomeFormSerializer), typeof(CodeDomSerializer))]
class SomeForm : Form { … }

The Forms Designer might then generate the following InitializeComponent code:

private void InitializeComponent()
{
    … /* (general setup code, such as a call to `this.SuspendLayout`) */ 

    // 
    // someButton
    // 
    … /* (someButton's properties are set) */

    // CODEDOM WAS HERE!
    // 
    // SomeForm
    // 
    … /* (form's properties are set) */

    … /* (general setup code, such as a call to `this.ResumeLayout`) */ 
}

Note that the comment // CODEDOM WAS HERE was not added as the very first line in InitializeComponent, but only as the first line of the code block that deals with the properties of the form object itself.

What would I have to do if I wanted to be able to modify the generated CodeDOM of the whole method, and not just of the part that deals with a specific object?

Background: Why do I want to do this? In Windows Forms, if one wants flexible value conversion during data binding, one usually has to resort to subscribing to the Format and Parse events of some particular Binding object. So I’m creating a specialized Binding subclass (let’s call it ConvertingBinding) that simplifies this process a bit.

Now, the issue is that when data bindings are set up in the Windows Forms Designer, the generated code creates instances of Binding; however, I would want the designer to instantiate my specialized subclass instead. My current approach is to let the designer create a CodeDOM tree first, then walk that tree and replace all instantiations of Binding by instantiations of ConvertingBinding.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:56:53+00:00Added an answer on June 13, 2026 at 11:56 pm

    You need to create two Form class. First Form with a DesignerSerializerAttribute. Second Form is descendant from first. After that you can customize InitializeComponent() for second Form and it’s controls or components.
    For this you should use manager.Context to get all StatementContext and CodeStatementCollection objects that contains serialized code of Form‘s controls.

    Here is some simple steps.
    Include libraries:

    using System.CodeDom;
    using System.ComponentModel.Design.Serialization;
    using System.Collections;
    

    Create new form and add DesignerSerializerAttribute:

    [DesignerSerializer(typeof(CustomFormSerializer), typeof(CodeDomSerializer))]
    class CustomForm : Form { … }
    

    Create CustomForm descendant and add some controls or components to it:

    class CustomForm1 : CustomForm { … }
    

    Add method to CustomFormSerializer for processing CodeStatementCollection, for example:

    private void DoSomethingWith(CodeStatementCollection statements)
    {
        statements.Insert(0, new CodeCommentStatement("CODEDOM WAS HERE"));
    }
    

    In Serialize method use cycle through manager.Context:

    public override object Serialize(IDesignerSerializationManager manager,
        object value)
    {
        //Cycle through manager.Context            
        for (int iIndex = 0; manager.Context[iIndex] != null; iIndex++)
        {
            object context = manager.Context[iIndex];
    
            if (context is StatementContext)
            // Get CodeStatementCollection objects from StatementContext
            {
                ObjectStatementCollection objectStatementCollection =
                    ((StatementContext)context).StatementCollection;
    
                // Get each entry in collection.
                foreach (DictionaryEntry dictionaryEntry in objectStatementCollection)
                    // dictionaryEntry.Key is control or component contained in CustomForm descendant class
                    // dictionartEntry.Value is CodeDOM for this control or component
                    if (dictionaryEntry.Value is CodeStatementCollection)
                        DoSomethingWith((CodeStatementCollection)dictionaryEntry.Value);
            }
    
            //Do something with each collection in manager.Context:
            if (context is CodeStatementCollection)
                DoSomethingWith((CodeStatementCollection)context);
        }
    
        // Let the default serializer do its work:
        CodeDomSerializer baseClassSerializer = (CodeDomSerializer)manager.
            GetSerializer(value.GetType().BaseType, typeof(CodeDomSerializer));
        object codeObject = baseClassSerializer.Serialize(manager, value);
    
        // Then, modify the generated CodeDOM:
        if (codeObject is CodeStatementCollection)
            DoSomethingWith((CodeStatementCollection)codeObject);
    
        // Finally, return the modified CodeDOM:
        return codeObject;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.