Click here to Skip to main content
13,146,014 members (55,500 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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
Updated 21-Dec-12 2:48am
v4
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 ???
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.
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: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.
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
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 21 Dec 2012
Copyright © CodeProject, 1999-2017
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