C#窗體傳值代碼方法
一、前言
我們在做Winform窗體程序開發(fā)的時候,會經(jīng)常遇到窗體之間相互傳值。假設(shè)有下面的一個場景:一個主窗體和一個子窗體,點擊主窗體上面的按鈕給子窗體傳值,并在子窗體上面顯示出來,一般會有如下幾種方式實現(xiàn)。
二、公共屬性
我們可以在子窗體里面定義一個公共的屬性,然后在父窗體里面給公共屬性賦值,這樣可以實現(xiàn)窗體之間傳值,子窗體代碼如下:
using System;
using System.Windows.Forms;
namespace DelegateDemo
{
public partial class frmChild : Form
{
public frmChild()
{
InitializeComponent();
}
// 定義一個公共屬性,接收傳遞的值
public string strMessage { get; set; }
/// <summary>
/// 窗體加載
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmChild_Load(object sender, EventArgs e)
{
// 將接收到的值顯示在窗體上
this.lblMessage.Text = strMessage;
}
}
}
父窗體代碼:
using System;
using System.Windows.Forms;
namespace DelegateDemo
{
public partial class frmParent : Form
{
public frmParent()
{
InitializeComponent();
}
/// <summary>
/// 單擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Value_Click(object sender, EventArgs e)
{
frmChild child = new frmChild();
// 給窗體的公共屬性賦值
child.strMessage = this.txtMessage.Text.Trim();
// 顯示子窗體
child.Show();
}
}
}
這種方式有一個缺點:屬性需要設(shè)置為public,不安全。
二、公共方法
我們還可以在子窗體里面定義一個方法,通過調(diào)用方法傳值,子窗體代碼如下:
using System;
using System.Windows.Forms;
namespace DelegateDemo
{
public partial class frmChild : Form
{
public frmChild()
{
InitializeComponent();
}
// 定義一個公共屬性,接收傳遞的值
//public string strMessage { get; set; }
// 定義屬性為private
private string strMessage { get; set; }
/// <summary>
/// 給私有屬性賦值
/// </summary>
/// <param name="strText"></param>
public void SetText(string strText)
{
strMessage = strText;
}
/// <summary>
/// 窗體加載
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmChild_Load(object sender, EventArgs e)
{
// 將接收到的值顯示在窗體上
this.lblMessage.Text = strMessage;
}
}
}
父窗體代碼:
using System;
using System.Windows.Forms;
namespace DelegateDemo
{
public partial class frmParent : Form
{
public frmParent()
{
InitializeComponent();
}
/// <summary>
/// 單擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Value_Click(object sender, EventArgs e)
{
#region 調(diào)用公共屬性賦值
//frmChild child = new frmChild();
//// 給窗體的公共屬性賦值
//child.strMessage = this.txtMessage.Text.Trim();
//// 顯示子窗體
//child.Show();
#endregion
#region 調(diào)用方法賦值
frmChild child = new frmChild();
// 給窗體的公共屬性賦值
child.SetText(this.txtMessage.Text.Trim());
// 顯示子窗體
child.Show();
#endregion
}
}
}
這種方式同樣也有缺點:屬性雖然是private的了,但是方法還是public的。
三、委托
上述兩種方式都是不安全,下面我們使用委托來實現(xiàn)窗體之間傳值。
1、定義一個委托
我們在主窗體里面定義一個有參無返回值的委托:
// 定義一個有參無返回值的委托 private delegate void SendMessage(string strMessage);
2、實例化一個此委托類型的事件
在父窗體里面定義一個委托類型的事件:
// 定義一個委托類型的事件 public event SendMessage sendMessageEvent;
委托與事件的關(guān)系,事件相對于委托更安全,更低耦合。委托是一個類型,事件是委托類型的一個實例。
3、定義要執(zhí)行的方法
這里其實就是在子窗體里面定義一個給控件賦值的方法:
/// <summary>
/// 給控件賦值的方法
/// </summary>
/// <param name="strValue"></param>
public void SetValue(string strValue)
{
this.lblMessage.Text = strValue;
}
4、將方法綁定到事件
frmChild child = new frmChild(); // 將方法綁定到事件上 sendMessageEvent += new SendMessage(child.SetValue); // 也可以使用下面的簡寫形式 // sendMessageEvent += child.SetValue; child.Show();
5、觸發(fā)委托
在按鈕的點擊事件里面觸發(fā)委托:
if(sendMessageEvent!=null)
{
sendMessageEvent.Invoke(this.txtMessage.Text.Trim());
}
上面的代碼中使用的是自定義的委托,我們也可以使用.Net 框架里面自帶的Action泛型委托:
using System;
using System.Windows.Forms;
namespace DelegateDemo
{
public partial class frmParent : Form
{
// 定義一個有參無返回值的委托
public delegate void SendMessage(string strMessage);
// 定義一個委托類型的事件
public event SendMessage sendMessageEvent;
public event Action<string> actionEvent;
public frmParent()
{
InitializeComponent();
}
/// <summary>
/// 單擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Value_Click(object sender, EventArgs e)
{
#region 調(diào)用公共屬性賦值
//frmChild child = new frmChild();
//// 給窗體的公共屬性賦值
//child.strMessage = this.txtMessage.Text.Trim();
//// 顯示子窗體
//child.Show();
#endregion
#region 調(diào)用方法賦值
//frmChild child = new frmChild();
//// 給窗體的公共屬性賦值
//child.SetText(this.txtMessage.Text.Trim());
//// 顯示子窗體
//child.Show();
#endregion
#region 通過委托傳值
//frmChild child = new frmChild();
//// 將方法綁定到事件上
//// sendMessageEvent += new SendMessage(child.SetValue);
//// 也可以使用下面的簡寫形式
//sendMessageEvent += child.SetValue;
//child.Show();
#endregion
#region 使用Action
frmChild child = new frmChild();
// 將方法綁定到事件上
actionEvent += child.SetValue;
child.Show();
#endregion
// 使用自定義委托
//if (sendMessageEvent!=null)
//{
// sendMessageEvent.Invoke(this.txtMessage.Text.Trim());
//}
// 使用Action委托
if (actionEvent != null)
{
actionEvent.Invoke(this.txtMessage.Text.Trim());
}
}
}
}
以上就是本次介紹的全部相關(guān)知識點,感謝大家的學(xué)習(xí)和對腳本之家的支持。
相關(guān)文章
c#用Treeview實現(xiàn)FolderBrowerDialog 和動態(tài)獲取系統(tǒng)圖標(biāo)(運用了Win32 
其實,FolderBrowerDialog 很好用呢,有木有啊親,反正我特別的喜歡,微軟大哥把這個瀏覽文件夾的東東封裝的多好呀2013-03-03
解析C#編程的通用結(jié)構(gòu)和程序書寫格式規(guī)范
這篇文章主要介紹了C#編程的通用結(jié)構(gòu)和程序書寫格式規(guī)范,這里我們根據(jù)C#語言的開發(fā)方微軟給出的約定來作為編寫樣式參照,需要的朋友可以參考下2016-01-01
C#實現(xiàn)幾十萬級數(shù)據(jù)導(dǎo)出Excel及Excel各種操作實例
本篇文章主要介紹了C#實現(xiàn)幾十萬級數(shù)據(jù)導(dǎo)出Excel及Excel各種操作實例,這里整理了詳細的代碼,有需要的小伙伴可以參考下。2017-02-02
C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解
這篇文章主要介紹了C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04

