有效声明/创建变量/控件

发布于 2024-09-17 18:10:27 字数 851 浏览 2 评论 0 原文

我正在清理一个即将完成的应用程序,我注意到一些让我好奇为什么要这样做的事情。虽然您可以在项目中编辑表单的 .Designer.cs,但其中有很多自动生成的内容,例如变量和控件的创建。他们有 Windows 窗体设计器生成的代码,我几乎从未接触过这些代码。但当我以我喜欢的格式创建变量时:

string strValue1,
       strValue2;

相比之下:

string strValue1;
string strValue2;

我注意到 Windows 在文件底部声明控件,然后在 InitializeComponent() 函数中创建/实例化它们。现在,我知道我可以采用“新”实例并将它们放在声明所在的位置,并且看起来运行良好。我的问题是,其中一种比另一种有什么好处?或者Windows 就可以为我们自动生成它们吗?如果以一种方式执行此操作是否有可能比另一种方式获得更好的性能,我想知道。谢谢大家的帮助。

示例 1:

private void InitializeComponent()
{
  ...
  this.control1 = new System.Windows.Forms.Control();
  ...
}

...

System.Windows.Forms.Control control1;

示例 2:

private void InitializeComponent()
{
  ...
}

...

System.Windows.Forms.Control control1 = new System.Windows.Forms.Control();

I'm working on cleaning up an app I'm almost finished with and I noticed something that made me curious as to why it's being done that way. While you can edit the .Designer.cs in your project for a form, there is a lot of autogenerated stuff in there, such as the creation of variables and controls. They have the Windows Form Designer generated code which hardly ever gets touched by me. But as I was making variables in the format I like them:

string strValue1,
       strValue2;

As compared to:

string strValue1;
string strValue2;

I noticed that Windows declares the controls on the bottom of the file then creates/instantiates them in the InitializeComponent() function. Now, I knowI could take the "new" instances and put them where the declarations are and it seems to run fine. My question is what's the benefit of one over the other? Or is this the way it is so Windows can autogenerate them for us? If there's a possibility of better performance for doing it one way over another, I'd like to know. Thanks guys for the help.

Example 1:

private void InitializeComponent()
{
  ...
  this.control1 = new System.Windows.Forms.Control();
  ...
}

...

System.Windows.Forms.Control control1;

Example 2:

private void InitializeComponent()
{
  ...
}

...

System.Windows.Forms.Control control1 = new System.Windows.Forms.Control();

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

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

发布评论

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

评论(2

梦里泪两行 2024-09-24 18:10:27

不要编辑该代码。。它是自动生成的,设计者实际上读回代码以在设计器中重新创建表单。当你进行这样的更改时,你很可能会轰炸设计师,并且你的表单变得无法设计。即使您确实设法避免崩溃,当您更改设计器中的表单时,您的更改也会消失。

标记为“Windows 窗体设计器生成的代码”的区域中的任何内容都是不干涉的。

像这样的改变没有任何好处。它生成完全相同的代码。

Do not edit that code. It is auto-generated and the designer actually reads the code back to recreate the form in the designer. When you make changes like this, it is very likely you'll bomb the designer and your form becomes un-designable. Even if you do manage to avoid crashing it, your changes will simply disappear when you alter the form in the designer.

Anything in the region that's marked "Windows Forms Designer generated code" is hands-off.

There is no benefit whatsoever to changes like these. It generates the exact same code.

后知后觉 2024-09-24 18:10:27

当在 InitializeComponent 中完成时,您可以对内容进行更多控制

如果您打开 .cs 文件(而不是设计器)并以

public Form1()
{
    InitializeComponent();
}

这种方式查看构造函数,您可以在实例化控件之前执行代码..

如果您只是在声明控件时创建控件,那么您将无法执行此操作...

You can get some more control over stuff when its done in the InitializeComponent

If you open up your .cs file (not the designer) and look at the constructor

public Form1()
{
    InitializeComponent();
}

this way you can have code execute before the controls are instantiated..

if you would just create the controls when they are declared then you would not be able to do this...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文