Click here to Skip to main content
11,408,835 members (61,238 online)
Rate this: bad
good
Please Sign up or sign in to vote.
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 3:25am
Edited 21-Dec-12 3:48am
v4
Comments
Krunal Rohit at 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 at 21-Dec-12 8:44am
   
I already tried that same error. I updated the question.
Krunal Rohit at 21-Dec-12 8:46am
   
See my solution below...
Zoltán Zörgő at 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ő at 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 at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 294
2 Maciej Los 155
3 PIEBALDconsult 140
4 Peter Leow 140
0 Sergey Alexandrovich Kryukov 8,626
1 OriginalGriff 6,855
2 Maciej Los 3,322
3 Abhinav S 3,238
4 Peter Leow 3,034


Advertise | Privacy | Mobile
Web01 | 2.8.150414.5 | Last Updated 21 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100