建议触摸 Designer.cs 文件中的代码吗?

发布于 2024-12-18 01:44:57 字数 393 浏览 0 评论 0原文

我是否建议我触摸 Windows 窗体应用程序的 Designer.cs 文件?当我将标签引用到我的设置文件时,我计划以这样的方式对其进行编码:如果我的设置文件显示了

int startup = 0;

我的组合框选择的代码处理程序,它将

if (settingObject.bootOnStartup == 0)
        {
            comboStartup.SelectedIndex = 0;
        }
        else
        {
            comboStartup.SelectedIndex = 1;
        }

对其功能起作用,但它会导致设计窗口崩溃。

Is it advisable for me to touch the designer.cs file for my windows form application? As I am referencing the labels to my settings file, I plan to code it in such a way that if my settings file shows

int startup = 0;

the code handler for my combo box selection would be

if (settingObject.bootOnStartup == 0)
        {
            comboStartup.SelectedIndex = 0;
        }
        else
        {
            comboStartup.SelectedIndex = 1;
        }

it does works for its function but it sort of crashes the design window.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趁年轻赶紧闹 2024-12-25 01:44:57

永远不要修改 .designer.cs 文件。无论您在其中做什么,下次您在设计器中编辑表单时,它都会被覆盖,因此您必须再次执行此操作。我认为没有任何理由不将此代码放入 Form 构造函数或 Load 事件中...

Don't modify the .designer.cs files, ever. Whatever you do in it, it will be overwritten the next time you edit your Form in the designer, so you will have to do it again. I don't see any reason not to put this code in the Form constructor or the Load event...

暖阳 2024-12-25 01:44:57

你的最后一句话就是答案。如果有什么需要特殊处理,请在分部类的用户部分进行。即使在您的情况下(我推测),这也需要在设计器代码运行之前手动创建一个组合框。

我唯一一次接触设计器代码是当我简单而快速地想要更改一些我确信能够在 VS 的代码生成中幸存下来的内容时,例如绑定中的属性名称。

Your last sentence is the answer. If anything needs special treatment, do it in the user part of the partial class. Even if in your case (I'm speculating) this would require creating a ComboBox manually before the designer code runs.

The only time I touch the designer code is when I simply and quickly want to change something that I'm sure will survive the code generation of VS, e.g. a property name in a binding.

剑心龙吟 2024-12-25 01:44:57

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 之后在 Form 构造函数中进行修改:

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;
         }
    };
}

The Designer.cs file tells you not to modify:

/// <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... */

}

If you have modifications then make those in your Form constructor after InitializeComponent:

public MainForm( ) {
    InitializeComponent( );

    if (settingObject.bootOnStartup == 0) {
        comboStartup.SelectedIndex = 0;
    } else {
        comboStartup.SelectedIndex = 1;
    }
}

... or, on a this.Load event:

public MainForm( ) {
    InitializeComponent( );
    this.Load += (s, e) => {
        if (settingObject.bootOnStartup == 0) {
             comboStartup.SelectedIndex = 0;
         } else {
             comboStartup.SelectedIndex = 1;
         }
    };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文