C#實現(xiàn)兩個窗體之間數(shù)值傳送的方法
本文實例講述了C#實現(xiàn)兩個窗體之間數(shù)值傳送的方法。分享給大家供大家參考,具體如下:
以下是本人常用的方法,其實方法很多,但我覺得這兩種我比較好理解,要是哪位朋友有比較簡單的易懂的其他方法,希望不吝賜教。
方法一:
比如要在FORM2里得到FORM1里的值,先在FORM1里定義一個公有的字符串
然后FORM2里用FORM1去實例化一個對象
最后用 f.zhi來取得FORM1里的值。(f.Show()也是一個道理,即對象名.方法名)
方法二:
比如要在FORM1里得到FORM2里的值,利用GET,SET方法。
在FORM2里放一個TEXTBOX,寫一個公有屬性
public string transsformValue { get { return this.textBox1.Text; } set { this.textBox1.Text=value; } }
在FORM1里這么寫(在里面也加一個TEXTBOX):.
FORM2 f=new FORM2(); f.transsformValue="aaaa"; textBox1=f.transsformValue; f.Show();
這樣運行后是將FORM2的文本框的值設(shè)為“aaaa”,并且顯示在FORM1里的文本框里
實例演示
FORM1里這么寫:
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 WindowsFormsApplication17 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { InputBox f = new InputBox(); f.Title = "請輸入對話框"; f.TipText = "請輸入年齡"; if (f.ShowDialog() == DialogResult.OK) this.label1.Text = f.Message; } } } //InputBox的FORMl里這么寫 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 WindowsFormsApplication17 { public partial class InputBox : Form { public InputBox() { InitializeComponent(); } public string Title { set { this.Text = value; } } public string Message { get { return this.Input.Text; } } public string TipText { set { this.Tip.Text = value; } } private void InputBox_Load(object sender, EventArgs e) { this.AcceptButton = this.btnOK; this.CancelButton = this.btnCancel; this.btnOK.DialogResult = DialogResult.OK; this.btnCancel.DialogResult = DialogResult.Cancel; } } }
運行效果截圖如下:
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
C++聯(lián)合體轉(zhuǎn)換成C#結(jié)構(gòu)的實現(xiàn)方法
這篇文章主要介紹了C++聯(lián)合體轉(zhuǎn)換成C#結(jié)構(gòu)的實現(xiàn)方法,需要的朋友可以參考下2014-08-08c# 網(wǎng)址壓縮簡單實現(xiàn)短網(wǎng)址
短網(wǎng)址,忽然一下子就冒出來的東西,長長的一個URL,提交過去,出來就只有短短的一個URL了,看起來似乎挺神奇,其實簡單分析一下,明白其中的原理,也是一件很簡單的事情,需要的朋友可以了解下2012-12-12WinForm中變Enter鍵為Tab鍵實現(xiàn)焦點轉(zhuǎn)移的方法
這篇文章主要介紹了WinForm中變Enter鍵為Tab鍵實現(xiàn)焦點轉(zhuǎn)移的方法,主要通過一個ControlTools類來實現(xiàn)該功能,需要的朋友可以參考下2014-08-08