簡述:最近在作C#和HALCON編程,要用到單例的參數由子窗體改變父窗體的值。此例爲簡化版編程
1,點擊系統設置函數
2,彈出子窗體,在其輸入修改參數後點修改按鈕this
3,點擊肯定按鈕後,關閉子窗體後,主窗體textbox值改變spa
4,單例程序以下:code
public class Student { //建立單例類,內部靜態類方法 private Student() { }//私有構造函數 class Nested { internal static readonly Student instance = new Student(); } public static Student Instance { get { return Nested.instance; } } //建立字段和其屬性 private string name; public string Name { get { return name; } set { name = value; } } private int chinses; public int Chinses { get { return chinses; } set { chinses = value; } } private int math; public int Math { get { return math; } set { math = value; } } }
5,子窗體程序orm
public Setting() { InitializeComponent(); } Student stu= Student.Instance; //建立單例類的對 private void Setting_Load(object sender, EventArgs e) { } private void BtnChange_Click(object sender, EventArgs e) { stu.Name = this.textBoxName.Text;//把子窗體textbox顯示值賦給字段的屬性 stu.Chinses = Convert.ToInt32(this.textBoxChinese.Text); stu.Math = Convert.ToInt32(this.textBoxMath.Text); }
6,父窗體程序blog
Student stu = Student.Instance;//建立單例類 private void MainForm_Load(object sender, EventArgs e) { this.tBName.Text = "Weber";//主窗體加載顯示內容 this.tBChinese.Text = Convert.ToString(89); this.tBMath.Text = Convert.ToString(90); } private void ToolStripButton1_Click(object sender, EventArgs e) { Setting mySetting = new Setting(); mySetting.ShowDialog();//子窗體彈出 this.tBName.Text = stu.Name;//字段屬性的值賦給textbox值 this.tBMath.Text = Convert.ToString(stu.Math); this.tBChinese.Text = Convert.ToString(stu.Chinses); }
7,總結ip
新手一枚,若有錯誤請指正!get