none
Gui Form1.cs[Design] vs2010 RRS feed

  • Question

  • Every time I try to open the [Design] of the Gui it rips out all of my labels and their text.  I tried to change the labels to a resource manager by going into the generated code.  This gave me an error only in the design.  I then proceeded to change the code again and now whenever the form.cs[design] is opened it deletes all of the labels code in the generated code section.  My Gui has compiled and run completely fine with no errors and will display any changes I make to the resource manager.  How do I stop it from deleting changes made in the form1.designer.cs file?
    Thursday, August 25, 2011 8:18 PM

Answers

  • You can't change generated code like that because designer has no way of recreating the code like you want it.

    Another thing is, when you design Form1, designer creates an instance of the base class, which is System.Windows.Forms.Form, and then parses the code from initialize component. Designer will not execute constructor in which you called SetResource(), so rm is null in design time, although it's set in run time.

    Best regards,
    Vladimir 

     

    • Marked as answer by Helen Zhou Friday, September 2, 2011 2:15 AM
    Saturday, August 27, 2011 12:33 AM
  • Hi nipper,

    Code in InitializeComponent(Form1.Designer.cs ) is auto generated that it is not recommended to edit anything in InitializeComponent though. You can read more about how InitializeComponent works in this article: C# InitializeComponent Explanation


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Helen Zhou Friday, September 2, 2011 2:30 AM
    Friday, September 2, 2011 2:30 AM

All replies

  • Do you receive any error when you open form1.designer.cs

    Example

    • Object instance not set an object
    • resource not found

    or any other error which on click takes you to form1.designer.cs (code) file.


    Please mark those posts as answer which answers your question. Faraz
    Friday, August 26, 2011 5:03 AM
  • Hi nipper24a,

    Would you please upload your sample or tell us the steps that can reproduce the problem. It is not necessary that you send out the complete source of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business logic from it.

    Or you can create a new clean project, add labels, set it up the way you did in the original project, and reproduce the issue. It's very likely that you will find the problem while trying to reproduce the issue on a clean project.

    Looking forward to your update.

    Best wishes,


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Friday, August 26, 2011 9:10 AM
  • Here is a small piece of code from earlier in the project.  It is from the auto generated section of the windows form in form1.Designer.cs

     

        this.analyzeCheck.AutoSize = true;

                this.analyzeCheck.Checked = true;

                this.analyzeCheck.CheckState = System.Windows.Forms.CheckState.Checked;

                this.analyzeCheck.Location = new System.Drawing.Point(16, 370);

                this.analyzeCheck.Name = "analyzeCheck";

                this.analyzeCheck.Size = new System.Drawing.Size(63, 17);

                this.analyzeCheck.TabIndex = 3;

                this.analyzeCheck.Text = rm.GetString("0000");   //problem here

                this.analyzeCheck.UseVisualStyleBackColor = true;

     

    The error I receive from this is the variable 'rm' is either undeclared or was never assigned ( I can show call stack if needed)

    Later on I added

     

     public static ResourceManager RM

    {

    get

    {

    return rm ;

    }

    }

     

    and changed 

     this.analyzeCheck.Text = rm.GetString("0000");

    to

    this.analyzeCheck.Text = Form1.RM.GetString("0000");

     

    This does not give me any errors in the [Design] part when looking at the Gui it just rips out the above code completely.  Its like I hit ignore without hitting ignore.

     

    And both of these ways will compile and run perfect.  I only get an error when I open up the form1.cs[Design]

     

     

    **More Code**

    This is all in Form1.cs

            private string strResourcesPath = Application.StartupPath + "/Resources";

            private static ResourceManager rm;

     

            private void SetResource()

            {

            rm = ResourceManager.CreateFileBasedResourceManager("resource", strResourcesPath, null);

            }

     

    And then I call SetResource before InitializeComponent

     

    public Form1()

            {

                SetResource();

                InitializeComponent();

            }

     

    Thanks

    Friday, August 26, 2011 1:09 PM
  • You can't change generated code like that because designer has no way of recreating the code like you want it.

    Another thing is, when you design Form1, designer creates an instance of the base class, which is System.Windows.Forms.Form, and then parses the code from initialize component. Designer will not execute constructor in which you called SetResource(), so rm is null in design time, although it's set in run time.

    Best regards,
    Vladimir 

     

    • Marked as answer by Helen Zhou Friday, September 2, 2011 2:15 AM
    Saturday, August 27, 2011 12:33 AM
  • Hi nipper,

    Code in InitializeComponent(Form1.Designer.cs ) is auto generated that it is not recommended to edit anything in InitializeComponent though. You can read more about how InitializeComponent works in this article: C# InitializeComponent Explanation


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Helen Zhou Friday, September 2, 2011 2:30 AM
    Friday, September 2, 2011 2:30 AM