欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中枚舉類型和radiobox關(guān)聯(lián)操作的方法

 更新時間:2015年04月29日 10:51:37   作者:靈魂  
這篇文章主要介紹了C#中枚舉類型和radiobox關(guān)聯(lián)操作的方法,實例分析了C#中枚舉類型及與控件關(guān)聯(lián)操作的相關(guā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)文章

最新評論