/// <summary>
/// 填充ComboBox
/// </summary>
/// <param name="cmb"></param>
/// <param name="operation"></param>
private void FillComboBox(ComboBox cmb) {
try {
cmb.DropDownStyle = ComboBoxStyle.DropDownList;//设置combobox的样式。
ArrayList ary_Tmp = this.GetArrayList();
ary_Tmp.Insert(0, (new Array(-1, ""))); //给ArrayList添加新的空行,并给绑定的ValueMember字段赋值。
cmb.DataSource = ary_Tmp;
cmb.DisplayMember = "StrArray";
cmb.ValueMember = "IntArray";
} catch (Exception ex_FillCmb) {
MessageBox.Show("填充ComboBox失败!"+ex_FillCmb.Message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return;
}
}
/// <summary>
/// 构建一个ArrayList用来绑定
/// </summary>
/// <returns></returns>
private ArrayList GetArrayList() {
try {
ArrayList ary = new ArrayList();//创建一个二维数组。
ary.Add(new Array(0, "上午"));
ary.Add(new Array(1, "中午"));
ary.Add(new Array(2, "晚上"));
return ary;
} catch(Exception ex) {
throw ex;
}
}
/// <summary>
/// 创建一个类用来构建二维ArrayList
/// </summary>
public class Array{
public int i_array;
public string str_array;
public Array(int intArray, string strArray){
this.i_array = intArray;
this.str_array = strArray;
}
public int IntArray{ //属性
get{return i_array;}
}
public string StrArray{
get{return str_array;}
}
}