我是否可以触摸我的Windows窗体应用程序的designer.cs文件?当我将标签引用到我的设置文件时,我打算以这样的方式对其进行编码,如果我的设置文件显示
int startup = 0;
我的组合框选择的代码处理程序将是
if (settingObject.bootOnStartup == 0)
{
comboStartup.SelectedIndex = 0;
}
else
{
comboStartup.SelectedIndex = 1;
}
它确实适用于它的功能,但它会使设计窗口崩溃。
答案 0 :(得分:2)
永远不要修改.designer.cs文件。无论你做什么,它都会在你下次在设计师中编辑你的表格时被覆盖,所以你必须再次这样做。我没有看到任何理由不将此代码放在Form构造函数或Load
事件......
答案 1 :(得分:1)
你的最后一句是答案。如果有任何需要特殊处理的事情,请在部分类的用户部分进行。即使在你的情况下(我在推测),这也需要在设计器代码运行之前手动创建一个ComboBox。
我触摸设计器代码的唯一一次是当我简单而且很快想要改变一些我肯定会在VS的代码生成中存活的东西时,例如绑定中的属性名称。
答案 2 :(得分:1)
Designer.cs文件告诉您不要修改:
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent( ) {
/* ...control customization from designer... */
}
如果您有修改,请在InitializeComponent
之后的表单构造函数中创建它们:
public MainForm( ) {
InitializeComponent( );
if (settingObject.bootOnStartup == 0) {
comboStartup.SelectedIndex = 0;
} else {
comboStartup.SelectedIndex = 1;
}
}
...或者,在this.Load
事件中:
public MainForm( ) {
InitializeComponent( );
this.Load += (s, e) => {
if (settingObject.bootOnStartup == 0) {
comboStartup.SelectedIndex = 0;
} else {
comboStartup.SelectedIndex = 1;
}
};
}