How to open a new form from another form

后端 未结 11 1546
太阳男子
太阳男子 2020-12-03 00:29

I have form which is opened using ShowDialog Method. In this form i have a Button called More. If we click on More it should open another form and it should close the curren

相关标签:
11条回答
  • 2020-12-03 01:07
    private void Button1_Click(object sender, EventArgs e)
    {
        NewForm newForm = new NewForm();    //Create the New Form Object
        this.Hide();    //Hide the Old Form
        newForm.ShowDialog();    //Show the New Form
        this.Close();    //Close the Old Form
    }
    
    0 讨论(0)
  • 2020-12-03 01:08

    If I got you right, are you trying like this?

    alt text

    into this?
    alt text

    in your Form1, add this event in your button:

        // button event in your Form1
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog(); // Shows Form2
        }
    

    then, in your Form2 add also this event in your button:

        // button event in your Form2
        private void button1_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3(); // Instantiate a Form3 object.
            f3.Show(); // Show Form3 and
            this.Close(); // closes the Form2 instance.
        }
    
    0 讨论(0)
  • 2020-12-03 01:12

    Use this.Hide() instead of this.Close()

    0 讨论(0)
  • 2020-12-03 01:14

    For example, you have a Button named as Button1. First click on it it will open the EventHandler of that Button2 to call another Form you should write the following code to your Button.

    your name example=form2.
    
    form2 obj=new form2();
    
    obj.show();
    

    To close form1, write the following code:

    form1.visible=false; or form1.Hide();

    0 讨论(0)
  • 2020-12-03 01:15

    I would use a value that gets set when more button get pushed closed the first dialog and then have the original form test the value and then display the the there dialog.

    For the Ex

    1. Create three windows froms
    2. Form1 Form2 Form3
    3. Add One button to Form1
    4. Add Two buttons to form2

    Form 1 Code

     public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private bool DrawText = false;
    
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
            if (f2.ShowMoreActions)
            {
                Form3 f3 = new Form3();
                f3.ShowDialog();
            }
    
        }
    

    Form2 code

     public partial class Form2 : Form
     {
            public Form2()
            {
                InitializeComponent();
            }
    
            public bool ShowMoreActions = false;
            private void button1_Click(object sender, EventArgs e)
            {
                ShowMoreActions = true;
                this.Close();
            }
    
    
            private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    

    Leave form3 as is

    0 讨论(0)
  • 2020-12-03 01:15

    you may consider this example

    //Form1 Window
    //EventHandler
    Form1 frm2 = new Form1();
    {
        frm2.Show(this); //this will show Form2
        frm1.Hide();  //this Form will hide
    }
    
    0 讨论(0)
提交回复
热议问题