locked
Generate code in designer file. RRS feed

  • Question

  • Hi,

    I have a form, where I have dragged and dropped myControl(control that I have defined).
    When I drag it into the form the designer file of the form is updated with the code related to this control(visual studio in design mode).
    Can anyone tell me, how is this code generated in the designer file?
    Is there any way that I can generate and insert the same code in the designer file, without dragging and dropping, in design mode of Visual Studio?

    Thank you in advance.
    • Moved by Chao Kuo Monday, December 21, 2009 3:27 AM windows form related! (From:Visual Studio Extensibility)
    Thursday, December 17, 2009 2:03 PM

Answers

  • Hi,

    you can completely control the design-time generated code through the code serializers. See this topic for details.

    Here is an example where additional 'myControl.Visible=false;' statement is auto-generated:

    [Serializer(typeof(MySerializer))]
    class MyControl : Control {}
    
    class MySerializer : CodeDomSerializer
    {
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
          CodeDomSerializer baseSerializer;
          CodeStatementCollection statements;
          CodeExpression targetObject;
    
          if(manager == null || value == null)
          {
            return null;
          }
    
          baseSerializer = (CodeDomSerializer)manager.GetSerializer(typeof(MyControl).BaseType, typeof(CodeDomSerializer));
          statements = baseSerializer.Serialize(manager, value) as CodeStatementCollection;
          if(statements == null)
          {
            statements = new CodeStatementCollection();
          }
    
          targetObject = GetExpression(manager, value);
          if(targetObject != null)
          {
            // add 'myControl.Visible = true;' statement.
            statements.Add(
              new CodeAssignStatement(
                new CodeFieldReferenceExpression(targetObject, "Visible"),
                new CodePrimitiveExpression(true)));
          }
          return statements;
        }
    }
    


    Best regards.

    Flexible TreeView - the most flexible treeview-listview-grid hybrid control for .NET!
    • Proposed as answer by Ruslan Mogilevskiy Monday, December 21, 2009 9:05 AM
    • Marked as answer by Aland Li Friday, December 25, 2009 3:02 AM
    Monday, December 21, 2009 9:05 AM
  • Hi KeiPi,

    As far as I know, there is often a serilizer attached to a control. When we design the control in design time, the serilizer instance will deserialize the control instance, generate related code and add them to the designer code file of the Form, such as 'Form1.Designer.cs'.

    We can create our own CodeDomSerializer class and attach it to a control to custom the deserialize behavior. The document below shows the details about CodeDomSerializer class and a sample:
    http://msdn.microsoft.com/en-us/library/system.componentmodel.design.serialization.codedomserializer.aspx

    Let me know if this does not help.
    Aland Li

    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by Aland Li Friday, December 25, 2009 3:02 AM
    Monday, December 21, 2009 9:12 AM

All replies

  • Hi,

    you can completely control the design-time generated code through the code serializers. See this topic for details.

    Here is an example where additional 'myControl.Visible=false;' statement is auto-generated:

    [Serializer(typeof(MySerializer))]
    class MyControl : Control {}
    
    class MySerializer : CodeDomSerializer
    {
        public override object Serialize(IDesignerSerializationManager manager, object value)
        {
          CodeDomSerializer baseSerializer;
          CodeStatementCollection statements;
          CodeExpression targetObject;
    
          if(manager == null || value == null)
          {
            return null;
          }
    
          baseSerializer = (CodeDomSerializer)manager.GetSerializer(typeof(MyControl).BaseType, typeof(CodeDomSerializer));
          statements = baseSerializer.Serialize(manager, value) as CodeStatementCollection;
          if(statements == null)
          {
            statements = new CodeStatementCollection();
          }
    
          targetObject = GetExpression(manager, value);
          if(targetObject != null)
          {
            // add 'myControl.Visible = true;' statement.
            statements.Add(
              new CodeAssignStatement(
                new CodeFieldReferenceExpression(targetObject, "Visible"),
                new CodePrimitiveExpression(true)));
          }
          return statements;
        }
    }
    


    Best regards.

    Flexible TreeView - the most flexible treeview-listview-grid hybrid control for .NET!
    • Proposed as answer by Ruslan Mogilevskiy Monday, December 21, 2009 9:05 AM
    • Marked as answer by Aland Li Friday, December 25, 2009 3:02 AM
    Monday, December 21, 2009 9:05 AM
  • Hi KeiPi,

    As far as I know, there is often a serilizer attached to a control. When we design the control in design time, the serilizer instance will deserialize the control instance, generate related code and add them to the designer code file of the Form, such as 'Form1.Designer.cs'.

    We can create our own CodeDomSerializer class and attach it to a control to custom the deserialize behavior. The document below shows the details about CodeDomSerializer class and a sample:
    http://msdn.microsoft.com/en-us/library/system.componentmodel.design.serialization.codedomserializer.aspx

    Let me know if this does not help.
    Aland Li

    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by Aland Li Friday, December 25, 2009 3:02 AM
    Monday, December 21, 2009 9:12 AM
  • Thank you very much for your answer Ruslan and Aland!



    Tuesday, January 19, 2010 10:08 PM
  • Hi,

    you can completely control the design-time generated codec through the code serializers. See this topic for details.

    Here is an example where additional 'myControl.Visible=false;' statement is auto-generated:

    [Serializer(typeof(MySerializer))]
    class</SPN> MyControl : Control {}
    
    class MySerializer : CodeDomSerializer
    {
      public override object Serialize(IDesignerSerializationManager manager, object value)
      {
       CodeDomSerializer baseSerializer;
       CodeStatementCollection statements;
       CodeExpression targetObject;
    
       if(manager == null || value == null)
       {
        return null;
       }
    
       baseSerializer = (CodeDomSerializer)manager.GetSerializer(typeof(MyControl).BaseType, typeof(CodeDomSerializer));
       statements = baseSerializer.Serialize(manager, value) as CodeStatementCollection;
       if(statements == null)
       {
        statements = new CodeStatementCollection();
       }
    
       targetObject = GetExpression(manager, value);
       if(targetObject != null)
       {
        // add 'myControl.Visible = true;' statement.
        statements.Add(
         new CodeAssignStatement(
          new CodeFieldReferenceExpression(targetObject, "Visible"),
          new CodePrimitiveExpression(true)));
       }
       return statements;
      }
    }
    
    


    Best regards.

    Flexible TreeView - the most flexible treeview-listview-grid hybrid control for .NET!

    Please explain it in details, There is something about the sample I cannot understand, Thank you! Where to find some related articles?
    Sunday, August 15, 2010 2:31 AM
  • Hello Camille,

    it might be better if you start your own thread and ask a little bit more specific question then "There is something about the sample I cannot understand" .... state what are you trying to achieve, what have you tried so far, and you may also post a link to this thread also as the starting point. 

    It would be easier for both answerers and future readers if we focus on one question per thread.

    best regards,
    Vladimir
    Sunday, August 15, 2010 1:46 PM