开发者

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 开发者_JAVA技巧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();
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜