c#rebuilding正在运行我的程序的一部分(加载用户控件),单击play可以导致重建并运行两次(c# rebuilding is running part of my program(loadi

编程入门 行业动态 更新时间:2024-10-28 08:27:54
c#rebuilding正在运行我的程序的一部分(加载用户控件),单击play可以导致重建并运行两次(c# rebuilding is running part of my program(loading a user control), and clicking play can cause a rebuild and run it twice)

我的程序有问题,我已经设法缩小到一些示例代码。

添加了注释 - http://ge.tt/7paP1Gj2 <---我的代码70KB zip文件的链接。

c#rebuilding正在运行我的程序的一部分(加载用户控件),单击play可以导致重建并运行两次。 双击表单也可以有时将其设置为关闭。 我会详细说明我的意思。

我的程序是按以下方式创建的。

我右键单击该项目,然后单击添加...用户控件

所以我有表单和用户控件。

我在表单加载和用户控件中的以下代码中有以下代码

private void Form1_Load(object sender, EventArgs e) { // any time i change this e.g. change the string e.g. change "loaded" to "loaddddd" and click Form1 in solution explorer. then the "uc loaded" messagebox comes up MessageBox.Show("form loadeddd"); }

以及用户控件中的以下内容

private void UserControl1_Load(object sender, EventArgs e) { MessageBox.Show("uc loaded"); // any time i change this string then rebuild, then the "uc loaded" message box comes up. string a="abc"; }

所以现在我点击播放。 “uc loaded”出现两次,“form loaded”出现一次。

如果我再次点击播放,(不更改任何代码),则“uc loaded”出现一次,“form loaded”出现一次。

如果我在解决方案资源管理器中双击Form1,则不会发生任何奇怪的行为。 表格刚刚出现。 没有消息框被设置。

现在假设我将“已加载表单”的消息框字符串(例如“已加载表单”)更改为“已加载表单”然后我在解决方案资源管理器中双击Form1,并出现一个显示“uc loaded”的消息框。 这对我来说似乎很奇怪。 如果我再次单击Form1,则不会出现“uc loaded”。 如果我再次更改字符串并且dbl单击Form1,则会出现“uc loaded”消息框。 如果我再次点击Form1然后它不会出现。

现在让我说我点击播放。 然后我得到“uc loaded”出现两次。 好像它可能首先进行了重建(令人讨厌地似乎运行了用户控件加载方法),(就好像它运行了那样)然后运行程序。 如果我再次单击“播放”,则“uc loaded”仅出现一次。 如果我更改了“form loaded”字符串,那么我单击Play,然后“uc loaded”出现两次。 再次点击播放,它会出现一次。

如果我更改“表单加载”字符串或“uc loaded”字符串或其他字符串,那么当我单击播放时,“uc loaded”会出现两次。

添加注意我的代码已经启动了更多内容并且我复制了文件夹并将其剪切并复制文件夹并将其剪切,然后使其尽可能小,同时仍然产生行为。 但是如果行为不是那么容易重现的话......我已经包含了一个链接到我的减少示例代码,当我尝试它时显​​示错误并且非常整齐。

I have an issue with my program which i've managed to narrow down into some example code.

Added Note- http://ge.tt/7paP1Gj2 <--- a link to my code 70KB zip file.

c# rebuilding is running part of my program(loading a user control), and clicking play can cause a rebuild and run it twice. Double clicking the Form can also set it off sometimes. I'll elaborate on what I mean by this.

My program was created in the following manner.

I right click the project then click add...User Control

So I have the form and the user control.

I have the following code in the form load and the following code in the user control

private void Form1_Load(object sender, EventArgs e) { // any time i change this e.g. change the string e.g. change "loaded" to "loaddddd" and click Form1 in solution explorer. then the "uc loaded" messagebox comes up MessageBox.Show("form loadeddd"); }

And the following in the user control

private void UserControl1_Load(object sender, EventArgs e) { MessageBox.Show("uc loaded"); // any time i change this string then rebuild, then the "uc loaded" message box comes up. string a="abc"; }

So right now I click play. "uc loaded" appears twice, and "form loaded" appears once.

If I click play again, (not changing any code), then "uc loaded" appears once, "form loaded" appears once.

If I double click Form1 in solution explorer then no strange behaviour occurs. The form just appears. No message box gets set off.

Now let's say I change the "form loaded" messagebox string e.g. "form loaded" to "form loadedd" Then I double click Form1 in solution explorer, and a messagebox appears that says "uc loaded". That seems strange to me. If I click Form1 again, then "uc loaded" doesn't appear. If I change the string again and dbl click Form1, then the "uc loaded" message box appears. If I dbl click Form1 again then it doesn't appear.

Now let's say I click Play. Then I get "uc loaded" appear twice. As if maybe it did a rebuild first(which annoyingly seems to run the user control load method), (as if it ran that) and then ran the program. If I click Play again, then "uc loaded" appears only once. If I change that "form loaded" string, then I click Play, then "uc loaded" appears twice. Click play again and it appears once.

If I change the "form loaded" string or the "uc loaded" string or the other string, then "uc loaded" appears twice, when I click play.

Added Note My code had started with more in it and i'd duplicated the folder and cut it down and duplicated the folder and cut it down, before making it as small as it is while still producing the behaviour. But in case the behaviour isn't that easily reproducable.. i've included a link to my cut down example code which when I try it aways shows the error and very neatly.

最满意答案

当您将UserControl直接放在表单上时会导致这种情况。

这个问题有两个解决方案:

在运行时添加控件: 您可以使用面板作为用户控件的“占位符”,您可以在运行时添加它,如下所示:

NameOfUserControl userControl = new NameOfUserControl(); nameOfPanel.Controls.Add(userControl);

检查设计模式: 在用户控件的加载功能中,您可以检查它是处于设计模式还是正在运行。

if(Application.ExecutablePath.IndexOf("devenv.exe", StringComparison.OrdinalIgnoreCase) == -1)  {  //Not in designer mode use > to check for designer mode MessageBox.Show("uc loaded");  }

This is caused when you put your UserControl on your Form directly.

There are 2 solutions to this issue:

Add the control on run: You can use a panel as a "placeholder" for a user control, you can add it at runtime like so:

NameOfUserControl userControl = new NameOfUserControl(); nameOfPanel.Controls.Add(userControl);

Check for design mode: In your user control's load function you can check if its in design mode or running.

if(Application.ExecutablePath.IndexOf("devenv.exe", StringComparison.OrdinalIgnoreCase) == -1)  {  //Not in designer mode use > to check for designer mode MessageBox.Show("uc loaded");  }

更多推荐

本文发布于:2023-07-29 20:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319464.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   控件   单击   正在运行   加载

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!