Open Form1 as new instance after successful operations on Form2 - c#

I am having two forms as Form1 and Form2 on Form1 button click I am opening the form2 as follows
private Form2 form2;
this.Hide();
form2.ShowDialog();
this.Show();
I have a back button and submit button on Form2 when I click on Back this is how I am showing Form1
this.Hide();
this.DialogResult = DialogResult.OK;
But when I submit the form I need to load the Form1 with new instance, this is what I tried but Form1 is showing multiple times
if (DialogResult.OK == MessageBox.Show("Installation was done", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information))
{
Form f1 = new Form ();
f1.Show();
this.Close();
}
When I execute my application this is how it looks after entering data to my textbox
After clicking on button1 it will show the data in Form2 if user wants to modify data he can come back if he clicks on submit I would like to have the new instance of Form1

What you need to do is after you open form 2 do something like
Form1.close();
and then when you are done with the operation:
Form1 form1 = new Form1();
form1.ShowDialog();
this.Close();
Ok if you want to save the data from form1 then i would either fave the data as a file that you could retrieve when ever of pass all of the variables you want to save into Form2 and then save them there. also instead of using a new form 1 you could use the existing one and reset any values that you want to reset.
I hope this helps.

Here is the code I tried, on your Form1 button click event write the following
private void button1_Click(object sender, EventArgs e)
{
form2.ShowData(textBox1.Text);
form2.ShowDialog(this);
}
In your Form2 submit click have the following code
private void button2_Click(object sender, EventArgs e)
{
this.Owner.Hide();
this.Owner = new Form1(this);
this.Hide();
this.Owner.Show();
}

Related

Form remains open in the background after closing it

So I want to show a form and close the other form so I did this in form1:
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 x = new Form2();
x.Show();
this.Hide();
//didn't use this.close() cause then the whole program will close instead of
//just the current form
}
Then I wanted the main form to open again after the 2nd form is closed so I did this in form2:
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form1 x = new Form1();
x.Show();
}
Now the problem is when I'm back in the main form after closing the 2nd form. if I close the main form it doesn't fully close and still remains open in the background (I found it in task manager). I think its because the "show" method just opens another form instead of making the form appear so the main form which got hidden is still there running in the background.
what should I do to make the form get closed when I exit it?
I tried putting this.close(); in the form closed and form closing event but both resulted in a crash.
When you write:
Form1 x = new Form1();
you are creating a new Form1 object, so x refers to the new one and not to the original one. Instead, you could use this:
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var form2 = new Form2())
{
this.Hide();
form2.ShowDialog();
}
this.Show();
}
When ShowDialog() is called, the code following it is not executed until after the dialog box is closed, so this.Show() will execute only after Form2 is closed.
Another option is to simply subscribe to the FormClosed() event of Form2 when you create it, then un-hide your instance of Form1 from there:
// ... all in Form1 ...
private void newTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form2 x = new Form2();
x.FormClosed += X_FormClosed;
x.Show();
}
private void X_FormClosed(object sender, FormClosedEventArgs e)
{
this.Show();
}
So then when you close Form2 your instance of Form1 will automatically re-appear.

In C# how to open third form inside first form when i click a button of the second form

In my Windows Forms application, I have 3 different Forms (Form1, Form2, Form3)
Form1 Contains a Button and a Panel (button1, panel1)
Form2 Contains only a button (button)
Form3 Contains nothing
In Form1 when I click button1 , Form2 opens in panel1
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
Form2 f2 = new Form2();
f2.TopLevel = false;
panel1.Controls.Add(f2);
f2.Dock = DockStyle.Fill;
f2.Show();
}
Now, inside Form2, when i click the button, I want the Form3 open inside Form1's panel, I've tried this code...
Form1 f1 = new Form1();
private void button1_Click(object sender, EventArgs e)
{
f1.panel1.Controls.Clear();
Form3 f3 = new Form3();
f3.TopLevel = false;
f1.panel1.Controls.Add(f3);
f3.Dock = DockStyle.Fill;
f3.Show();
}
Note: In Form1 I've changed the modifier to public for the Panel
Step 1: Create functions in Form1 to Show the other Forms:
class Form1
{
public void ShowForm2() {...}
public void ShowForm3() {...}
// TODO: think about: do you need IsForm2Shown?
// TODO: think about what you want if ShowForm2 is called twice
...
}
Step 2: Let Form2 know about Form1. Give Form2 a function to show Form3 inside Form1
class Form2
{
public Form1 Form1 {get; set;}
protected void ShowForm3InsideForm1()
{
this.Form1.ShowForm3();
}
}
Whenever Form one creates Form2 it should fill property Form1. So inside Form1.ShowForm2():
var form2 = new Form2()
{
Form1 = this,
...
}
form2.Show();
Step 3: whenever you find in Form 2 that you want to "show form 3 in form 1", just call the proper function
// Somewhere inside Form2 code you decide that you want to show Form3 inside Form1:
this.ShowForm3InsideForm1();
Simple comme bonjour!

To Close the present Windows Form before navigating to Next Window

I am in Form1 Designer Window and I have a Button. When I click on the Button, I should navigate from Form1 to Form2. However, Form1 designer window should get closed.
In the below code when I click on the Button, Form2 window will popup. The issue is Form1 will also be there on the computer screen along with Form2. I wish to see only the window to which I would like to navigate, and the other should disappear.
How to correct the below code accordingly?
private void button1_Click(object sender, EventArgs e)
{
Form2 Check = new Form2();
Check.Show();
}
This code hide Form1 when you are showing Form2 and then will close Form1 when you close Form2:
private void button1_Click(object sender, EventArgs e)
{
Hide();
Form2 Check = new Form2();
Check.Closed += (s, args) => this.Close();
Check.Show();
}

Closing a background form from a custom message box

Brief description:
Cannot close form1 by clicking a button that is on form2.
I have the following forms:
form1
formMessageBox which has button1
form3
I want to:
Pop up formMessageBox from form1 (this is working ok) then from formMessageBox, click button1 to close form1 and open form3.
I have used a variety of techniques however form1 cannot be targeted.
Help much appreciated.
Edit
When form1 and the messagebox are open at the same time. Form1 does not close or hide when button1 is clicked to try and close form1.
Not really sure what you're looking for, but I think you are needing something like this.
Say you have Form1 and you pop up a formMessageBox using ShowDialog().
public class Form1 : Form
{
if (somethingHappened)
{
var formMessageBox = new FormMessageBox();
//ShowDialog() sets the formMessageBox object as a modal dialog owned by Form1
DialogResult result = formMessageBox.ShowDialog();
//Assuming you have OK buttons or something like that in formMessageBox
if (result == DialogResult.OK)
{
var form3 = new Form3();
/*Can't use this.Close() here as it would close the entire application
Just hide it and don't forget to dispose of it later when you actually
want the application to close*/
this.Hide();
form3.Show();
}
}
}
Then in your formMessageBox code, just set the DialogResult according to what occurs:
public class FromMessageBox : Form
{
private void okButton_Click(object sender, EventArgs e)
{
//Do some stuff
this.DialogResult = DialogResult.OK
this.Close();
}
}

How to make a second form appear on top of the main form at start?

I have 2 forms; My main Form named Form1 and my second Form named Form2
My main form shows up at start, and I'd like to show Form2 too but it shows up under Form1.
And I want it to show up on top of my main form.
I've tried to set Form2's TopMost property to true then false but it didn't work.
I also tried to create a different Thread for Form2 to appear after Form1, in this case Form2 shows up quickly then disappear.
Show Form2 in Shown event handler of Form1:
private void Form1_Shown(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
You can use ShowDialog() instead of Show() if you want Form2 to be modal.
Add this code at Form1 load event:
Form2 form2 = new Form2();
form2.TopMost = true;
form2.Show();
new Form2().ShowDialog()
This will also lock Form1 until Form2 gets closed

Categories

Resources