C#跨窗体操作,引用传递

现在给大家介绍一种最简单的跨窗体操作

WinForm的窗体是一个类,C#的类是引用类型,那么我们应该可以将WinForm窗体类进行传递,那不就可以进行操作了么?

效果描述:

有三个窗体然后顺序分别是

(1)点击第一个窗体中的按钮弹出第二个窗体,隐藏第一个窗体

(2)第二个窗体到一定时间弹出第三个窗体

(3)点击第三个窗体的按钮关闭第三个和第二个窗体,弹出第一个窗体

From1

[csharp]view plaincopy

  1. using System;
  2. using System.Windows.Forms;
  3. namespace WindowsFormsApplication1
  4. {
  5. public partial class Form1 : Form
  6. {
  7. public Form1()
  8. {
  9. InitializeComponent();
  10. }
  11. private void 打开form2隐藏form1_Click(object sender, EventArgs e)
  12. {
  13. Form2 f = new Form2();
  14. f.fatherForm = this;
  15. f.Show();
  16. this.Hide();
  17. }
  18. }
  19. }

Form2

[csharp]view plaincopy

  1. using System;
  2. using System.Windows.Forms;
  3. namespace WindowsFormsApplication1
  4. {
  5. public partial class Form2 : Form
  6. {
  7. public Form2()
  8. {
  9. InitializeComponent();
  10. }
  11. public Form1 fatherForm;
  12. private void 打开from3_Click(object sender, EventArgs e)
  13. {
  14. Form3 f = new Form3();
  15. f.fatherForm = this;
  16. f.Show();
  17. }
  18. }
  19. }

Form3

[csharp]view plaincopy

  1. using System;
  2. using System.Windows.Forms;
  3. namespace WindowsFormsApplication1
  4. {
  5. public partial class Form3 : Form
  6. {
  7. public Form3()
  8. {
  9. InitializeComponent();
  10. }
  11. public Form2 fatherForm;
  12. private void 关闭form3from2显示from1_Click(object sender, EventArgs e)
  13. {
  14. fatherForm.fatherForm.Show();
  15. fatherForm.Close();
  16. this.Close();
  17. }
  18. }
  19. }