利用委托 实现窗体间通信,非原创
copy自:https://www.cnblogs.com/hugoNB/p/7130562.html,這個作者寫的淺顯易懂,就復制下來自己看
實現過程:
這里主要是用到委托實現,所以主要描述一下委托在這里的應用。
我們要在主窗體(這里的子父窗體都是自己假想)中獲取子窗體中的元素,所以首先在主窗體聲明一個委托,這個委托是用來干嘛的?我們知道,在子窗體勾選幾個選項點擊確定之后,在這個事件中,我們需要將勾選的內容傳送到主窗體,這里的委托的含義就是:我主窗體有給TextBox顯示文本的方法,但是我主窗體干不了這件事兒,因為我沒有文本,文本在你子窗體那里,所以主窗體得委托子窗體干一件事兒,這個事兒就是麻煩你子窗體把勾選的東西的文本給我顯示到主窗體傳的TextBox中。
//1、聲明一個委托 public delegate void showText(List<String> ls);聲明完委托后,在子窗體(Form2/Form3)實例化一個委托,這個委托才是真真正正的委托,是干事的委托。
//2、實例化一個委托public showText f2;
? 那有了委托之后,你子窗體需要干什么事情呢?來,就是干這件事兒:麻煩你幫我把list集合中的字符串顯示到textBox1里面去。該方法是在主窗體中寫的。
//3、委托的方法,這個方法應該和第一步是同步實現的,這里暫且記作第3步。private void T1Show(List<String> ls){stringList1 = ls;stringList1.Sort();this.textBox1.Text = null;foreach (String item in stringList1){this.textBox1.Text += item + ";";} }
? 委托子窗體要干的事情有了,接下來就是把這件事委托給子窗體。
//4、把要干的事情委托給子窗體已經創建好的委托載體f2.objForm.f2 = this.T1Show;
到這里基本上就實現了子父窗體利用委托進行窗體間通信,先把整個項目的代碼展示出來:
主窗體代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections;namespace 云狀 {public partial class Form1 : Form{//保存當前已經添加到數據庫的氣象代碼// public List<String> stringList1 = new List<string>();//private List<String> stringList2 = new List<string>();public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){Form2 objForm = new Form2();objForm.FormBorderStyle = FormBorderStyle.None;//4、把要干的事情委托給子窗體已經創建好的委托載體f2.objForm.f2 = this.T1Show;objForm.ShowDialog();}//3、委托的方法,這個方法應該和第一步是同步實現的,這里暫且記作第3步。private void T1Show(List<String> ls){stringList1 = ls;stringList1.Sort();this.textBox1.Text = null;foreach (String item in stringList1){this.textBox1.Text += item + ";";}}private void T2Show(List<String> ls){stringList2 = ls;stringList2.Sort();this.textBox2.Text = null;foreach (String item in stringList2){this.textBox2.Text += item + ";";}}private void button2_Click(object sender, EventArgs e){Form3 objForm = new Form3();objForm.FormBorderStyle = FormBorderStyle.None;objForm.f3 = this.T2Show;objForm.ShowDialog();}private void button3_Click(object sender, EventArgs e){//入庫}}//1、聲明一個委托public delegate void showText(List<String> ls); }子窗體Form2代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections;namespace 云狀 {public partial class Form2 : Form{//2、實例化一個委托public showText f2;public Form2(){InitializeComponent();if (Form1.stringList1 != null){foreach (Control item in this.panel1.Controls){if (item is CheckBox){string str = ((CheckBox)item).Text.Substring(0, 2);if (Form1.stringList1.Contains(str)){((CheckBox)item).Checked = true;}}} }}private void button1_Click(object sender, EventArgs e){List<String> ls=new List<string>();foreach(Control item in this.panel1.Controls){if(item is CheckBox){if (((CheckBox)item).Checked==true){ls.Add(((CheckBox)item).Text.Substring(0, 2));}}}if(f2!=null){f2(ls);}this.Close();}private void button2_Click(object sender, EventArgs e){this.Close();}} }子窗體Form3代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace 云狀 {public partial class Form3 : Form{public showText f3;public Form3(){InitializeComponent();if (Form1.stringList2 != null){foreach (Control item in this.panel1.Controls){if (item is CheckBox){string str = ((CheckBox)item).Text.Substring(0, 2);if (Form1.stringList2.Contains(str)){((CheckBox)item).Checked = true;}}} }}private void button1_Click(object sender, EventArgs e){List<String> ls=new List<string>();foreach (Control item in this.panel1.Controls){if (item is CheckBox){if (((CheckBox)item).Checked == true){ls.Add(((CheckBox)item).Text.Substring(0, 2));}}}if (f3 != null){f3(ls);}this.Close();}private void button2_Click(object sender, EventArgs e){this.Close();}} }可能會有疑問,不就是傳一個List<String>嗎,有必要這么麻煩嗎?其實這里只是利用委托做事情,委托的其他用處還很廣泛,我也在學習之中,以后有什么值得記錄的東西,再在這里記錄。。。
轉載于:https://www.cnblogs.com/DoudouZhang/p/9804252.html
總結
以上是生活随笔為你收集整理的利用委托 实现窗体间通信,非原创的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 10.16 多校联测
- 下一篇: git add . 出现错误 The