测试那些事儿—C#开发两个子窗体相互切换

设计:

1.新建三个窗体分别为Form1,Form2,Form3

2.Form1上有两个按钮,分别用来打开Form2和Form3

3.Form2上有一个按钮用来切换到Form3

3.Form3上有一个按钮用来切换到Form2

//Form1的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace test02
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }         private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
            this.Hide();
        }         private void button2_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.Show();
            this.Hide();
        }
    }
}   //Form2的代码如下: using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace test02
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        Form3 f3 = null;
        private void button1_Click(object sender, EventArgs e)
        {
            if (f3 == null) f3 = new Form3();
            this.Close();
            f3.Show();
        }
    }
}   //Form3的代码如下: using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace test02
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        Form2 f2 = null;
        private void button1_Click(object sender, EventArgs e)
        {
            if (f2 == null) f2 = new Form2();
            this.Close();
            f2.Show();
        }
    }
}
上一篇:窗体的跳转值


下一篇:c#中在工作线程创建窗体并操作