Open Form2 from Form1, close Form1 from Form2

后端 未结 8 702
陌清茗
陌清茗 2020-12-03 13:06

Now, I have two forms, called form1 and form2, in the form1 there\'s a button, when I click it, then open the form2

Question: in the form2, I want to create a button

8条回答
  •  清歌不尽
    2020-12-03 13:46

    if you just want to close form1 from form2 without closing form2 as well in the process, as the title suggests, then you could pass a reference to form 1 along to form 2 when you create it and use that to close form 1

    for example you could add a

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

    field and constructor to Form2

    if you want to first close form2 and then form1 as the text of the question suggests, I'd go with Justins answer of returning an appropriate result to form1 on upon closing form2

提交回复
热议问题