Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

c# - The *Design.cs file will reset automatically and delete the manually writed codes?

I need change the some Initialized control values, as below:

        this.lblName.Top = 10;
        this.lblFamily.Top = this.lblName.Top + this.lblName.Height + 15;
        ...

When I write these codes in InitializeComponent() method (Which automatically created in [Windows Form Name]Designer.cs file which this file is a Windows Form partial class container), sometimes this codes will deleted automatically and sometimes bring error warning in *.cs[Design] view and will delete above manually added codes, after ignoring that prompt.

I want add new controls and assign it's initialized property values manually by write appropriated codes in *Designer.cs file

How can I assign initialized values of controls properties?

&

Why the *Designer.cs file will reset automatically or return error page in *.cs[Design] tab?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are 2 Files for you Form:

MyFormClass.cs here you can edit as you like, add Properties, change them, etc.

MyFormClass.designer.cs // auto generated, dont put stuff here

Put your custom code in the constructor after the InitializeComponent() call

public MyFormClass()
{
    InitializeComponent();
    // do it here
    this.lblName.Top = 10;
    this.lblFamily.Top = this.lblName.Top + this.lblName.Height + 15;
}

It is the 2nd half of the partial class definition, so you have acces to all the Properties.

File1:

namespace Test{
    public partial class MyFormClass{
    // add some code
    }
}

File2:

namespace Test{
public partial class MyFormClass{
    // add some code here
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...