Problem Travelling Through Windows Forms

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 18
Reputation: Voulnet is an unknown quantity at this point 
Solved Threads: 0
Voulnet's Avatar
Voulnet Voulnet is offline Offline
Newbie Poster

Problem Travelling Through Windows Forms

 
0
  #1
Aug 5th, 2009
Hello there fellow members.

I have two windows forms, and they call each other.
- Form 1 has a button that calls Form 2.
- Form 2 does not have an exit button, but rather a button that goes back to Form 1.
- Form 1 is the main form and the program is terminated from it.

Now, I have a background algorithm in Form 1 which must not work when going to Form 2, but when I go back from 2 to 1, that algorithm should work again.

So, I did the following:

- Code of the button that takes me from Form 1 to 2:
  1. Form2 frm2 = new Form2();
  2.  
  3. frm2.Show();
  4. this.Hide();

- Code of the button that takes me back from 2 to 1:
  1. Form1 frm1 = new Form1();
  2. this.Close();
  3.  
  4. frm1.Show();

However, this leads to a problem of Form replication. If I go through the forms back and forth a little bit, then back to the main form and close the application; some artifacts remain in the background. Instances of the application itself remain working (can be shut off from the Task Manager), so how do I make sure that once Form 1 is closed, the application and all extra instances created during this Form traversal are terminated?

Thanks in advance!
Ask a stupid question; get a stupid answer.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 83
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 18
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: Problem Travelling Through Windows Forms

 
0
  #2
Aug 5th, 2009
When creating Form 1, use this method:
  1. Application.Run(new Form1());
This should set Form 1 as the main window of the program, and should terminate the program when it is closed.
Note: This calls the ShowDialog() method of the form after the constructor.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 18
Reputation: Voulnet is an unknown quantity at this point 
Solved Threads: 0
Voulnet's Avatar
Voulnet Voulnet is offline Offline
Newbie Poster

Re: Problem Travelling Through Windows Forms

 
0
  #3
Aug 6th, 2009
I am using this line in my Program.cs file, but artifacts of Form1 still remain in the background after I go back from Form 2 to Form1.

Just to clarify, Form 1 is indeed the main window and the program terminates when it is closed; IF I close it before going back and forth between Form1 and Form2 (Going back between them creates hidden Form1 instances).
Ask a stupid question; get a stupid answer.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 15
Reputation: Dimansu is an unknown quantity at this point 
Solved Threads: 1
Dimansu Dimansu is offline Offline
Newbie Poster

Re: Problem Travelling Through Windows Forms

 
0
  #4
Aug 6th, 2009
Hi
When you want to open the form 2 pass an instance of form1 in the form 2 constructor.ie in the button that shows form2 code should be;
  1. Form2 frm2 = new Form2(this);
  2.  
  3. frm2.Show();
  4. this.Hide();
In the form 2 constructor get that instance of form1 an asign it to a variable of type Form1 ie in Form2;
  1. Form1 = MainForm
  2. public Form2(Form1 InputForm)
  3. {
  4. MainForm = InputForm;
  5. }
  6. Then in the button that takes you to Form1 insert foll code
  7. MainForm.Show();
Last edited by John A; Aug 6th, 2009 at 2:39 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 18
Reputation: Voulnet is an unknown quantity at this point 
Solved Threads: 0
Voulnet's Avatar
Voulnet Voulnet is offline Offline
Newbie Poster

Re: Problem Travelling Through Windows Forms

 
0
  #5
Aug 6th, 2009
Ah, yes. I never thought about passing Form1 to 2.

I tried it, and it works.

Thanks guys!
Ask a stupid question; get a stupid answer.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC