C#中列舉類型和radiobox聯系關系操作的辦法。本站提示廣大學習愛好者:(C#中列舉類型和radiobox聯系關系操作的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中列舉類型和radiobox聯系關系操作的辦法正文
本文實例講述了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#法式設計有所贊助。