在C#应用程序的开发中,经常会用到一些公共控件,有些功能是需要写代码去实现的,但是又有共性,所以我把这些控件的功能分别建立了对应的工具类,抽象出来分享给大家。
一、CheckListBoxUtils.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ProjectName.Util
{
public class CheckedListBoxUtils
{
public static string getSelectValue(CheckedListBox clbox)
{
string checkedText = string.Empty;
for (int i = 0; i < clbox.Items.Count; i++)
{
if (clbox.GetItemChecked(i))
{
clbox.SetSelected(i, true);
checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") + clbox.SelectedValue.ToString();
}
}
return checkedText;
}
public static string getSelectText(CheckedListBox clbox)
{
string checkedText = string.Empty;
for (int i = 0; i < clbox.CheckedItems.Count; i++)
{
checkedText += (String.IsNullOrEmpty(checkedText) ? "" : ",") + clbox.GetItemText(clbox.Items[i]);
}
return checkedText;
}
public static void bindDataSource<T>(CheckedListBox clbox, IList<T> dataList, string key, string value){
clbox.DataSource = dataList;
clbox.DisplayMember = value;
clbox.ValueMember = key;
}
}
}
二话不说,先上代码,getSelectValue和getSelectText是为了获取选中的value和text,一般我们会给checkListBox绑定一组数据,value是我们实际要传递的,而text是显示给用户看的,这时候我们就要获取选中的value或者text,一般绑定可以这么操作:
private void bindDict2ListBox(Dictionary<string,string> bindDict) {
if (bindDict.Count == 0)
{
this.listBoxSelected.DataSource = null;
}
else
{
this.listBoxSelected.DataSource = new BindingSource(bindDict, null);
this.listBoxSelected.DisplayMember = "Value";
this.listBoxSelected.ValueMember = "Key";
}
}
通过Dictionary来绑定checkListBox。也可以通过指定成员变量名称,给checkListBox绑定一个对象数组,如下:
CheckedListBoxUtils.bindDataSource<CommodityAttributeConfig>(this.checkedListBoxSize,
getAttrbuteConfigList(), "attributeCode", "attributeNameCn");
其中getAttrButeConfigList()返回是一个IList<T>数组,T中有个变量名叫attributeCode和attrbuteNameCn,可以输入这两个变量名来指定,checkListBox的value和display分别是取哪个成员变量的值。
二、ComboBoxUtils.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ProjectName.Util
{
public class ComboBoxUtils
{
// ComboBox下拉框 key-value 初始化
public static void MapInitial(ComboBox comb, Dictionary<string, string> dict)
{
if (dict.Count > 0)
{
comb.DataSource = new BindingSource(dict, null);
comb.DisplayMember = "Value";
comb.ValueMember = "Key";
}
}
// ComboBox下拉框 key-value 设置选中项
public static void MapSelected(ComboBox comb, string key)
{
var comboItem = comb.Items.Cast<KeyValuePair<string,string>>()
.FirstOrDefault(item => item.Key.Equals(key));
comb.SelectedItem = comboItem;
}
// ComboBox下拉框 key-value 获取选中项的key
public static string MapKey(ComboBox comb)
{
object selectedItem = comb.SelectedItem;
if (selectedItem != null)
{
return ((KeyValuePair<string, string>)selectedItem).Key;
}
else
return null; ;
}
// ComboBox下拉框 key-value 获取选中项的Value
public static string MapValue(ComboBox comb)
{
object selectedItem = comb.SelectedItem;
if (selectedItem != null)
{
return ((KeyValuePair<string, string>)selectedItem).Value;
}
else
return null; ;
}
}
}
1.MapInitial() 给comboBox绑定Dictionary;
2.MapSelected() 设置绑定keyPair的ComboBox的选中项,根据key值;
3.MapKey() 获取选中项的key值;
4.MapValue() 获取选中项的value值;
以上的功能是针对comboBox绑定的类型是keyPair,即显示的是value值,而实际上想用的是key的情况,其他单个的选中和取值就没这么复杂。