0

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.

barlop
  • 12,887
  • 8
  • 80
  • 109
  • 1. Not reproduce able 2. MessageBox shows when rebuilding? You are not even running the program... – EpicKip Mar 14 '17 at 15:29
  • At design time user controls can be instantiated & code start to run unexpectedly - I don't know whether it is related to this but see this link : http://stackoverflow.com/questions/282014/net-windows-forms-design-time-rules – PaulF Mar 14 '17 at 15:45
  • @EpicKip what do you mean it's not reproducable. Have you even tried to reproduce it? I spoke of rebuilding (which shouldn't really run it), but I also spoke of clicking Play(which obviously does run it), so I don't know what you're talking about when you say i am not even running the program. – barlop Mar 14 '17 at 15:49
  • @PaulF well even if there is the issue of that happening at design time.. There's also the issue of different behaviour after clicking Play, sometimes a messagebox comes up once, sometimes twice. – barlop Mar 14 '17 at 15:52
  • If you are rebuilding, you are at that time no running. If you do not get a messagebox at rebuilding, reformat your question. And yes I have tried to reproduce it (change characters, rebuild, run) and NO its is not reproduce able! – EpicKip Mar 14 '17 at 15:52
  • @EpicKip obviously you can't reproduce it but I can, maybe I can upload the code somewhere and video it happening. – barlop Mar 14 '17 at 15:52
  • I don't know your code, best would be to make a new project where you reproduce the issue. I'm heading home in 10 minutes (home about 2 hours later) and would be willing to take a look – EpicKip Mar 14 '17 at 15:53
  • And it is not obvious I can't reproduce. If I make the same code I'll have the same problem :) you just haven't found the origin of the problem yet – EpicKip Mar 14 '17 at 15:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138033/discussion-between-barlop-and-epickip). – barlop Mar 14 '17 at 15:55
  • The designer needs to create a single instance of a user control to display it - this may account for the first of the 2 messageboxes. If the designer does not need to completely refresh itself after running, then the next time you will get only1 messagebox, if for some reason it does need to completely refresh then it may create another instance, so 2 again. I am not saying it definitely is the issue, but I have had several problems with the designer running code which I have solved using solutions from that link - it maybe worth just adding the couple of lines of code from the 1st answer. – PaulF Mar 14 '17 at 16:01

1 Answers1

1

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"); 
}
EpicKip
  • 4,015
  • 1
  • 20
  • 37
  • answer accepted because the workaround works.. or testing if the path is devenv and if it is then return(though perhaps one must be careful if experimenting with variations of that 'cos one time I did an Application.Exit on the condition that in design mode, and the program didn't open, but a goto or no doubt a 'return', orthe if as provided, is fine). It works for the Play case and the form getting instantiated when double clicking form. It must be that in the play case the two user control message boxes , one was devenv.exe design mode and the other was the actual executable one. – barlop Mar 15 '17 at 12:26
  • perhaps another workaround is to have no code in the load method, and to have for example a method one could call "init", and the form calls the "init" method of the control. Any code that was in the load, is put in the init. – barlop Mar 19 '17 at 13:44