Click here to Skip to main content
Sign Up to vote bad
good
See more: C#4.0
Let's say I have a form and a button in it. When I click the button I want to open the new form and start working on it. I Tried this.
 
private void ProceedButton_Click(object sender, EventArgs e)
{
    TestCreator TestCrt = new TestCreator();
    this.Hide();
    TestCrt.ShowDialog();
    this.Close();
 
}
 
But when TestCrt is opened , it is not functioning well.
When I run TestCreator from the main its working properly but when opening by showdialog its not. For example when I click a button in TestCrt the program is shutting.
Is there another better way?
Why am I having the problem?
Posted 21-Dec-12 2:25am
Edited 21-Dec-12 2:48am

Comments
Krunal Rohit - 21-Dec-12 8:28am
How it could be possible..?? Place your TestCrt code here.. And ya one more thing, first you hiding the form and after showing other form, you close previous one, is there something to do such ???
missak boyajian - 21-Dec-12 8:44am
I already tried that same error. I updated the question.
Krunal Rohit - 21-Dec-12 8:46am
See my solution below...
Zoltán Zörgő - 21-Dec-12 8:34am
.Show is not modal! The this.Close(); statement is executed just after the other form is shown. If this is the main form, than you will exit the application also. Try .ShowDialog instead.
Zoltán Zörgő - 21-Dec-12 8:46am
If the problem is the same with ShowDialog, than you probably posted far to few from your code. Are you using threads for example?
missak boyajian - 21-Dec-12 8:55am
When fisrt running the form from the main its working properly but opening from another form its not.
missak boyajian - 21-Dec-12 8:58am
And no i'm not using any threads. I just want to know if there is a problem with the code or running a new form needs another way.

2 solutions

After this.Close();, you exit the application because this is the main form. Try this:
private void ProceedButton_Click(object sender, EventArgs e)
{
    TestCreator TestCrt = new TestCreator();
    this.Hide();
    TestCrt.ShowDialog();
    this.Close();
 
}
If you call ShowDialog, your program wait until TestCrt is closed. If you call Show, this.Close(); is called immediately, also if TestCrt isn't closed.
  Permalink  
private void ProceedButton_Click(object sender, EventArgs e)
{
    TestCreator TestCrt = new TestCreator();
    this.Hide();
    TestCrt.Show();
    this.Close(); // remove this, because it closes the main form, that's your application exits..

}
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Prasad_Kulkarni 407
1 _Amy 336
2 Sergey Alexandrovich Kryukov 327
3 OriginalGriff 235
4 Christian Graus 223
0 Sergey Alexandrovich Kryukov 6,649
1 Prasad_Kulkarni 3,281
2 _Amy 3,065
3 OriginalGriff 2,989
4 CPallini 2,696


Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 21 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid