日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 使用公共字段进行窗体传值实例

發布時間:2025/4/14 C# 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 使用公共字段进行窗体传值实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

現假設有2個窗體;

從form1啟動form2;

需要在form1中,初始化設置form2窗體上的各個控件;使其顯示初始值;

此時最好的方法是用form2的公共字段;代碼看上去比較清晰;

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;namespace chzhdemo1 {public partial class Form2 : Form{public Form2(){InitializeComponent();}private void Form2_Load(object sender, EventArgs e){}public string txt1{set { textBox1.Text = value; }get { return textBox1.Text; }}public string combo1{set { comboBox1.Text = value; }get { return comboBox1.Text; }}public decimal num1{set { numericUpDown1.Value = value; }get { return numericUpDown1.Value; }}public bool rdchk1{set { radioButton1.Checked = value; }get { return radioButton1.Checked; }}public string txt2{set { textBox2.Text = value; }get { return textBox2.Text; }}public string txt3{set { textBox3.Text = value; }get { return textBox3.Text; }}} }

對要傳值的控件,的屬性值,定義公共字段,寫好它的get、set方法;

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 chzhdemo1 {public partial class Form1 : Form{Form2 f2;public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){f2 = new Form2();f2.txt1 = "黃飛鴻";f2.combo1 = "廣州市";f2.num1 = 33;f2.rdchk1 = true;f2.txt2 = "寶芝林有限公司";f2.txt3 = "無影拳";f2.ShowDialog();}} }

定義了公共字段以后在form1可以訪問form2中的公共字段;進行賦值;

form2的2個文本框,定義string類型的公共字段;數值框,定義decimal類型的公共字段;為combobox的text屬性賦值,也定義string類型的公共字段;要從form1設置form2的radiobutton選中與否,其checked屬性是bool類型,定義bool類型公共字段;

上述代碼效果如下:

?

總結

以上是生活随笔為你收集整理的C# 使用公共字段进行窗体传值实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。