Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone
i am using a piece of code as under


the object of the form1 class/form doesn't appears please help to display it within the time_elapsed body

What I have tried:

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{

timer = new Timer();
timer.Interval = 1000;
timer.Elapsed += timer_Elapsed;

timer.Start();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
form1 object = new form1();
object.Show()

}
Posted
Updated 15-Sep-16 3:11am
Comments
BillWoodruff 15-Sep-16 9:37am    
Create the instance of the Form outside the call to Timer_Elapsed.
johannesnestler 15-Sep-16 10:13am    
post your real code - then we see further -this doesn't compile. With that kind of accuracy at work you do better something else then programming...
Philippe Mori 15-Sep-16 12:17pm    
Obviously, if the form is not shown (multiple times), then something else must be wrong in your code...

That's a silly thing to do, even if your code compiled - which it won't. It's missing a semicolon, and you can't call a variable object for the same reasons you can't call it float or string.
So the chances are that if you fix them, it'll work - it does for me - and it doesn't seem to work for you because it doesn't compile so the "running version" doesn't have that code in it.
    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Interval = 1000;
    timer.Elapsed += timer_Elapsed;
    timer.Start();
    }
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
    Form1 f1 = new Form1();
    f1.Show();
    }
But that's not nice - it'll open a new form every second ... which will fill the screen pretty quickly.
 
Share this answer
 
v2
try this:
C#
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{

timer = new Timer();
timer.Interval = 1000;
timer.Elapsed += timer_Elapsed;

timer.Start();
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
form1 f1 = new form1();
f1.Show();
timer.Stop();
} 


You shouldn't use "object" as a name for an object, because object is a reserved type and of course you'd better stop the timer after created the new instance of form1.

You further should know that this is not the "professional" way of doing anything.
 
Share this answer
 
Comments
Faiiziii Awan 15-Sep-16 9:30am    
thanks buffedcheesy
but i have already try this nut now work.
as form1 obj = new form(); creates the object suddenly object goes kill.i already tired stop timer first then creates object etc etc but all does nothing
buffedcheesy 15-Sep-16 9:44am    
Which environment are you working with?
Usually the standard name is "Form1" (upper case) not "form1", this is confusing me a bit. The order of commands in the event handler doesn't matter in this case.

I Try to follow you but i don't know how far you are.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900