Winfrom窗体间传值
生活随笔
收集整理的這篇文章主要介紹了
Winfrom窗体间传值
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.通過(guò)tag屬性傳輸,tag屬性是存儲(chǔ)與空間密切相關(guān)的數(shù)據(jù)。比如登陸界面的數(shù)據(jù)傳輸給主界面。
子窗體 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?主窗體
??? ? ? ??
定義一個(gè)類(lèi)
namespace FrmLogin {public class User{public string UserName { set; get; }public string PassWorld { set; get; }} }?
子窗體.cs
private void buttonLogin_Click(object sender, EventArgs e){User user = new User();user.UserName = this.textBoxName.Text.Trim();user.PassWorld = this.textBoxPassWorld.Text;this.Tag = user;this.DialogResult = DialogResult.OK;}父(主)窗體.cs
private void FormMain_Load(object sender, EventArgs e){FormLogin frm = new FormLogin();if (frm.ShowDialog() == DialogResult.OK){User user = frm.Tag as User;this.Text += string.Format("--歡迎您:{0}", user.UserName);}else{frm.Close();}}?
2.創(chuàng)建一個(gè)類(lèi),相當(dāng)于創(chuàng)建了全局共享的靜態(tài)變量,然后大家都來(lái)訪(fǎng)問(wèn)這個(gè)靜態(tài)變量。
全局類(lèi).cs
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace FrmLogin {public class ConText{public static User UserLogin = null;} }子窗體.cs
private void buttonLogin_Click(object sender, EventArgs e){User user = new User();user.UserName = this.textBoxName.Text.Trim();user.PassWorld = this.textBoxPassWorld.Text;//this.Tag = user; ConText.UserLogin = user;this.DialogResult = DialogResult.OK;}主窗體.cs
private void FormMain_Load(object sender, EventArgs e){FormLogin frm = new FormLogin();if (frm.ShowDialog() == DialogResult.OK){// User user = frm.Tag as User;User user = ConText.UserLogin;this.Text += string.Format("--歡迎您:{0}", user.UserName);}else{frm.Close();}}3.利用子窗體tag屬性
子窗體.cs
private void buttonLogin_Click(object sender, EventArgs e){User user = new User();user.UserName = this.textBoxName.Text.Trim();user.PassWorld = this.textBoxPassWorld.Text;FormLogin main = this.Owner as FormLogin;if (main == null){MessageBox.Show("LOGIN窗體沒(méi)有Owner,請(qǐng)檢查代碼!!");}main.Tag = user;this.DialogResult = DialogResult.OK;}父窗體.cs
private void FormMain_Load(object sender, EventArgs e){FormLogin frm = new FormLogin();if (frm.ShowDialog(this) == DialogResult.OK)//這兒一定要加this {User user = this.Tag as User;this.Text += string.Format("--歡迎您:{0}", user.UserName);}else{frm.Close();}}?
轉(zhuǎn)載于:https://www.cnblogs.com/youthshouting/p/4361036.html
總結(jié)
以上是生活随笔為你收集整理的Winfrom窗体间传值的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: (64位oracle使用32位的PLSQ
- 下一篇: 关于jdk1.5之后的自定拆装箱