国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#中枚舉類型和radiobox關聯操作的方法

2020-01-24 01:53:39
字體:
來源:轉載
供稿:網友

本文實例講述了C#中枚舉類型和radiobox關聯操作的方法。分享給大家供大家參考。具體分析如下:

有了enum我們可以列舉類型了,有了單選框和復選框我們可以鼠標來選擇了。但是編程的時候覺得讓兩個關聯起來,寫代碼比較麻煩,所以想自動的關聯起來。所以我嘗試了一下,記錄如下。

假如一個星期的enum:

public enum 星期{  星期一 = 0,  星期二,  星期三,  星期四,  星期五,  星期六,  星期天}

關聯到7個RadioButton,也就是單選框。
第一步在enum中定義星期一=0;
第二步在初始化函數中如下定義:

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中控件的順序在這些代碼中控制,假如發現順序不對,就要重新調整一下。

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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江城| 长垣县| 司法| 滦南县| 南康市| 开远市| 平安县| 枣庄市| 阿尔山市| 页游| 潢川县| 启东市| 错那县| 榆社县| 湘潭市| 新平| 扶风县| 呼和浩特市| 精河县| 岳西县| 兰溪市| 故城县| 怀柔区| 奇台县| 公安县| 七台河市| 盐城市| 芷江| 积石山| 镇安县| 台前县| 萨嘎县| 鸡泽县| 竹溪县| 香格里拉县| 从江县| 麻江县| 沭阳县| 比如县| 永吉县| 太仆寺旗|