現在給大家介紹一種最簡單的跨窗體操作
WinForm的窗體是一個類,C#的類是引用類型,那么我們應該可以將WinForm窗體類進行傳遞,那不就可以進行操作了么?
效果描述: 有三個窗體然后順序分別是 (1)點擊第一個窗體中的按鈕彈出第二個窗體,隱藏第一個窗體 (2)第二個窗體到一定時間彈出第三個窗體 (3)點擊第三個窗體的按鈕關閉第三個和第二個窗體,彈出第一個窗體 |
From1
- using System;
- using System.Windows.Forms;
-
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void 打開form2隱藏form1_Click(object sender, EventArgs e)
- {
- Form2 f = new Form2();
- f.fatherForm = this;
- f.Show();
- this.Hide();
- }
- }
- }
Form2
- using System;
- using System.Windows.Forms;
-
- namespace WindowsFormsApplication1
- {
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- }
-
- public Form1 fatherForm;
-
- private void 打開from3_Click(object sender, EventArgs e)
- {
- Form3 f = new Form3();
- f.fatherForm = this;
- f.Show();
- }
- }
- }
Form3
- using System;
- using System.Windows.Forms;
-
- namespace WindowsFormsApplication1
- {
- public partial class Form3 : Form
- {
- public Form3()
- {
- InitializeComponent();
- }
-
- public Form2 fatherForm;
-
- private void 關閉form3from2顯示from1_Click(object sender, EventArgs e)
- {
- fatherForm.fatherForm.Show();
- fatherForm.Close();
- this.Close();
- }
-
- }
- }
該文章在 2017/5/27 14:58:05 編輯過