當前位置:
首頁 > 知識 > winform利用委託delegate進行窗體間通信

winform利用委託delegate進行窗體間通信

前段時間學習委託,感覺很模糊的樣子,也做過許多實例,但是項目中一直沒有用到,今天在項目中遇到一個很簡單的例子,現在拿出來,做一個簡單的記錄。

要求:將彈出框里勾選的內容返回到主面板上。

工具:委託。

效果圖:(由於是根據項目提取出來的,所以裡面的界面有點文字有點奇怪)

主窗體:

winform利用委託delegate進行窗體間通信

子窗體:(點擊瀏覽之後彈出的對話框)

winform利用委託delegate進行窗體間通信

勾選幾項之後,點擊確定,主窗體顯示:

winform利用委託delegate進行窗體間通信

實現過程:

這裡主要是用到委託實現,所以主要描述一下委託在這裡的應用。

我們要在主窗體(這裡的子父窗體都是自己假想)中獲取子窗體中的元素,所以首先在主窗體聲明一個委託,這個委託是用來幹嘛的?我們知道,在子窗體勾選幾個選項點擊確定之後,在這個事件中,我們需要將勾選的內容傳送到主窗體,這裡的委託的含義就是:我主窗體有給TextBox顯示文本的方法,但是我主窗體幹不了這件事兒,因為我沒有文本,文本在你子窗體那裡,所以主窗體得委託子窗體干一件事兒,這個事兒就是麻煩你子窗體把勾選的東西的文本給我顯示到主窗體傳的TextBox中。

//1、聲明一個委託
public delegate void showText(List ls);

聲明完委託後,在子窗體(Form2/Form3)實例化一個委託,這個委託才是真真正正的委託,是幹事的委託。

//2、實例化一個委託
public showText f2;

那有了委託之後,你子窗體需要幹什麼事情呢?來,就是干這件事兒:麻煩你幫我把list集合中的字元串顯示到textBox1裡面去。該方法是在主窗體中寫的。

//3、委託的方法,這個方法應該和第一步是同步實現的,這裡暫且記作第3步。
private void T1Show(List 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 stringList1 = new List;
//private List stringList2 = new List;
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 ls)
{
stringList1 = ls;
stringList1.Sort;
this.textBox1.Text = null;
foreach (String item in stringList1)
{
this.textBox1.Text += item + ";";
}
}
private void T2Show(List 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 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 ls=new List;
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 ls=new List;
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嗎,有必要這麼麻煩嗎?其實這裡只是利用委託做事情,委託的其他用處還很廣泛,我也在學習之中,以後有什麼值得記錄的東西,再在這裡記錄。。。

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 達人科技 的精彩文章:

14:40-15:00博客站點web伺服器雪崩似的CPU 100%
Elasticsearch學習隨筆(一)——原理理解與5.0核心插件部署過程
net 中web.config一個配置文件解決方法 (其他配置文件引入方式)
MVC通過遞歸+部分視圖實現評論
JDBC02 利用JDBC連接資料庫

TAG:達人科技 |

您可能感興趣

通過Google Expeditions和Virtual Tours進行
通過Google Expeditions和Virtual Tours進行沉浸式教育
bcftools進行SNP calling
Google試圖僱用Vitalik Buterin進行秘密加密項目
通過Google Expeditions和Virtual Tours進行沉浸式教育
Snapchat推出3D Friendmojis進行社交互動
Telegram發布Telegram Passport,對ICO等數據進行加密
沃爾沃Polestar公司推Polestar Engineered 將電氣化進行到底
如何使用Reviewboard進行代碼Review?
Getting in shape this Summer夏日塑身進行時
Cheerble Studio推出智能骨頭Wickerbone,可以與寵物進行互動
為Dr.Martens加上綁帶?Engineered Garments進行大膽的嘗試
使用TensorFlow,Kafka和MemSQL進行實時機器學習
如何進行 code review?
全球「變醜」進行時:Blenciaga vs Prada?
SpringBoot中如何進行Bean配置
Pablo Picasso 名畫《Le Marin》即將進行拍賣
澳洲進行曲 Week 2 of January
Randomevent 監控進行中……
澳洲進行曲 Week 1 of January