Open Form2 from Form1, close Form1 from Form2

Now I have two forms called form1 and form2, in form1 there is a button when I click on it, then open form2

Question: in form2 I want to create a button when I click on it, close form 2 and close form1. How to do it?

+3
source share
8 answers

Form1:

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2(this);
        frm.Show();
    }

Form2:

public partial class Form2 : Form
{
    Form opener;

    public Form2(Form parentForm)
    {
        InitializeComponent();
        opener = parentForm;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        opener.Close();
        this.Close();
    }
}
+7
source

It works:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Hide()
    Form2.Show()
+1
source

, ShowDialog 2. , 2, a DialogResult, , - , 1.

0

form2.buttonclick put

this.close();

form1 2.

2.

put

this.close();
0

1 2 2, , , 1 2 1

,

public class Form2 : Form
{
    Form2(Form1 parentForm):base()
    {
        this.parentForm = parentForm;
    }

    Form1 parentForm;
    .....
}

2

form2, form1, , form1 form2

0

, .

    System.Threading.Thread newThread;
    Form1 frmNewForm = new Form1;

   newThread = new System.Threading.Thread(new System.Threading.ThreadStart(frmNewFormThread));
this.Close();
        newThread.SetApartmentState(System.Threading.ApartmentState.STA);
        newThread.Start();

And add the following method. Your newThread.Start will call this method.

    public void frmNewFormThread)()
    {

        Application.Run(frmNewForm);

    }
0
source
//program to form1 to form2
private void button1_Click(object sender, EventArgs e)
{
    //MessageBox.Show("Welcome Admin");
    Form2 frm = new Form2();
    frm.Show();
    this.Hide();          
}
0
source
private void button1_Click(object sender, EventArgs e)
{
      Form2 m = new Form2();
      m.Show();
      this.Visible = false;
}
0
source

Source: https://habr.com/ru/post/1748170/


All Articles