现在的位置: 首页 > 综合 > 正文

C#跨窗体操作(引用传递)

2013年07月27日 ⁄ 综合 ⁄ 共 1605字 ⁄ 字号 评论关闭

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

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

 

 

效果描述:

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

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

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

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

 

 

From1

[csharp] view plaincopy

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


 

Form2

[csharp] view plaincopy

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


 

Form3

[csharp] view plaincopy

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

抱歉!评论已关闭.