.NET C# - 用于事件接线的表单与设计器代码文件

发布于 2024-07-09 18:45:54 字数 537 浏览 4 评论 0原文

我只是好奇其他人如何在 C# 中使用这种 WinForm 代码。 假设我有一个表单,我们称之为 Form1。 我有一个名为 dgvMain 的 DataGridView。

您将代码放在哪里:

this.dgvMain.CellEndEdit += new DataGridViewCellEventHandler(dgvMain_CellEndEdit);

您将其放在 Form1 代码中还是 Form1 设计器代码中? 这个“事件连接”代码应该放在 Form1_Load 方法中吗?

我问的原因是...如果您双击 DataGridView,IDE 会将代码:

 this.dgvMain.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMain_CellContentClick);

插入到设计器代码中。 您的“事件接线”代码应该位于两个地方吗?

I was just curious how others work with this kind of WinForm code in C#.
Lets say I have a Form lets call it Form1. And I have a DataGridView called dgvMain.

Where do you put the code:

this.dgvMain.CellEndEdit += new DataGridViewCellEventHandler(dgvMain_CellEndEdit);

Do you put it in the Form1 code or the Form1 designer code?
Should this "event wiring" code go in the Form1_Load method?

The reason I am ask is... if you double click the DataGridView the IDE inserts the code:

 this.dgvMain.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvMain_CellContentClick);

into the designer code. Should your "event wiring" code be in two places?

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

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

发布评论

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

评论(3

半窗疏影 2024-07-16 18:45:54

简短的回答是肯定的。

更长的答案是 .designer.cs 用于存放设计器生成的代码。 如果你把自己的代码放在那里,它有可能被覆盖,搞砸 Visual Studio 中的设计时间,并降低可维护性,因为没有人期望自定义代码放在那里。

Short answer is yes.

Longer answer is that .designer.cs is there for code generated by the designer. if you put your own code in there, it has a chance of getting overwritten, screwing up the design time stuff in visual studio, and lowers maintainability because nobody expects custom code to be in there.

梦幻的味道 2024-07-16 18:45:54

这是一个敏感的话题。 在 1.1 中,表单文件中有相应的区域,但很多时候,代码会被设计者重写。 我是根据网络表单的经验来说的,但我只会认为其他地方也是一样的。

现在,您实际上将事件名称放入表单本身中(它是表单设计器的属性之一),代码生成器将在分部类中推出 += 事件处理程序。 我讨厌这样,但事实就是如此。

This is a touchy subject. In 1.1, there where regions for this in your forms file, but a lot of the time, the code would be over written by the designer. I'm speaking from webforms experiance, but I would only gather that it would be the same elsewhere.

Now, you actually put the eventname in the form itself (it's one of the properties on the forms designer), and the code generator will push out the += event handler thingies in the partial class. I hate it this way, but it is what it is.

岁月打碎记忆 2024-07-16 18:45:54

我使用设计器来处理与组件相关的所有事件。

我对所有对象事件使用代码

I use the Designer for all event related to Component.

I use the code for all object event.

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