943,697 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 947
  • C# RSS
Aug 5th, 2009
0

Problem Travelling Through Windows Forms

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  1. Form2 frm2 = new Form2();
  2.  
  3. frm2.Show();
  4. this.Hide();

- Code of the button that takes me back from 2 to 1:
C# Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Voulnet is offline Offline
30 posts
since Jun 2009
Aug 5th, 2009
0

Re: Problem Travelling Through Windows Forms

When creating Form 1, use this method:
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Aug 6th, 2009
0

Re: Problem Travelling Through Windows Forms

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).
Reputation Points: 10
Solved Threads: 0
Light Poster
Voulnet is offline Offline
30 posts
since Jun 2009
Aug 6th, 2009
0

Re: Problem Travelling Through Windows Forms

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;
C# Syntax (Toggle Plain Text)
  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;
C# Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Light Poster
Dimansu is offline Offline
45 posts
since Aug 2009
Aug 6th, 2009
0

Re: Problem Travelling Through Windows Forms

Ah, yes. I never thought about passing Form1 to 2.

I tried it, and it works.

Thanks guys!
Reputation Points: 10
Solved Threads: 0
Light Poster
Voulnet is offline Offline
30 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Problem in inserting the data
Next Thread in C# Forum Timeline: ListView number formatting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC