C#中枚舉類型和radiobox關(guān)聯(lián)操作的方法
本文實例講述了C#中枚舉類型和radiobox關(guān)聯(lián)操作的方法。分享給大家供大家參考。具體分析如下:
有了enum我們可以列舉類型了,有了單選框和復選框我們可以鼠標來選擇了。但是編程的時候覺得讓兩個關(guān)聯(lián)起來,寫代碼比較麻煩,所以想自動的關(guān)聯(lián)起來。所以我嘗試了一下,記錄如下。
假如一個星期的enum:
public enum 星期 { 星期一 = 0, 星期二, 星期三, 星期四, 星期五, 星期六, 星期天 }
關(guān)聯(lián)到7個RadioButton,也就是單選框。
第一步在enum中定義星期一=0;
第二步在初始化函數(shù)中如下定義:
public MainForm() { // // The InitializeComponent() call is required //for Windows Forms designer support. // InitializeComponent(); // // TODO: Add constructor code // after the InitializeComponent() call. // int idx = 0; foreach(Control c in groupBox1.Controls) { if(c is RadioButton) { ((RadioButton)c).Text = ((星期)idx).ToString(); ((RadioButton)c).Tag = ((星期)idx); idx++; } } }
第三步添加測試代碼:
void Button1Click(object sender, EventArgs e) { foreach(Control c in groupBox1.Controls) { if(c is RadioButton) { if(((RadioButton)c).Checked == true) { 星期 week = (星期)(((RadioButton)c).Tag); MessageBox.Show(week.ToString()); } } } }
注意:groupbox中控件的順序在這些代碼中控制,假如發(fā)現(xiàn)順序不對,就要重新調(diào)整一下。
this.groupBox1.Controls.Add(this.radioButton1); this.groupBox1.Controls.Add(this.radioButton2); this.groupBox1.Controls.Add(this.radioButton3); this.groupBox1.Controls.Add(this.radioButton4); this.groupBox1.Controls.Add(this.radioButton5); this.groupBox1.Controls.Add(this.radioButton6); this.groupBox1.Controls.Add(this.radioButton7);
希望本文所述對大家的C#程序設計有所幫助。
相關(guān)文章
C#在運行時動態(tài)創(chuàng)建類型的實現(xiàn)方法
這篇文章主要介紹了C#在運行時動態(tài)創(chuàng)建類型的實現(xiàn)方法,主要通過動態(tài)生成C#代碼再編譯成程序集來實現(xiàn)動態(tài)創(chuàng)建類型的,需要的朋友可以參考下2014-09-09C#中for循環(huán)、while循環(huán)循環(huán)執(zhí)行的方法
這篇文章主要介紹了C#中for循環(huán)、while循環(huán)循環(huán)執(zhí)行的方法的相關(guān)資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起學習吧2016-06-06為IObservable實現(xiàn)自己的運算符(詳解)
下面小編就為大家?guī)硪黄獮镮Observable實現(xiàn)自己的運算符(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05同時兼容JS和C#的RSA加密解密算法詳解(對web提交的數(shù)據(jù)加密傳輸)
這篇文章主要給大家介紹了關(guān)于同時兼容JS和C#的RSA加密解密算法,通過該算法可以對web提交的數(shù)據(jù)進行加密傳輸,文中通過圖文及示例代碼介紹的非常詳細,需要的朋友們可以參考借鑒,下面來一起看看吧。2017-07-07