简体   繁体   中英

C# on Program start up, the 2nd form is already running?

Hi i got a program that have two forms..

it goes like this

Form1(my main form)

From2

Form2 will only show if it is called by form1, basically when i start the program form1 is the only one there. but whats weird is.. the timer inside Form2 is already running.

Anyone got an idea why this is happening?

UPDATE:

here the code i used

public partial class MainForm : Form, IMessageFilter 
{
    public Form2 f2 = new Form2();

}

public void ShowForm2()
{
f2.Show();
}

So based on your clarification, you're instantiating Form2 when Form1 is created... and so I'm guessing that the Timer is enabled by default and is instantiated when Form2 is instantiated, so it starts right away.

You need to change it so that the timer isn't enabled until you show Form2 (maybe in the Load event) or some other point later.

More code might be helpful though - I still feel like I'm stabbing the dark here.

When you said:

public Form2 f2 = new Form2 ();  // its inside public partial class MainForm

Is it like this:

public class MainForm {
     public Form2 f2 = new Form2();

     public void ShowForm2() {
         f2.Show();
     }
}

You should not do that, else you should create an instance of Form2 when needed, like this:

public class MainForm {
     public void ShowForm2() {
         Form2 f2 = new Form2();
         f2.Show();
     }
}

Now you are sure that the instance will exist only when the message is received and will avoid to running the timer in Form2 if it is hidden.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM