Reputation: 13750
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.
Upvotes: 0
Views: 217
Reputation: 4033
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");
}
Upvotes: 1