linq 解决winForm中控件CheckedListBox操作的问题。(转载)
2010-12-22 09:17 爱研究源码的javaer 阅读(391) 评论(0) 编辑 收藏 举报1.获取CheckedListBox选中项的文本的字符串,并用分隔符连接。系统没有提供相应的属性,方法。
这里我们利用3.0的特性给其来个扩展方法,如下:
public static string GetCheckedItemsText(this CheckedListBox box)
{
string result = box.CheckedItems.Cast<object>().Aggregate(string.Empty, (current, checkedItem) => current + (box.GetItemText(checkedItem) + "/"));
if (result.Length > 0) result = result.TrimEnd(new char[] {'/'});
return result;
}
{
string result = box.CheckedItems.Cast<object>().Aggregate(string.Empty, (current, checkedItem) => current + (box.GetItemText(checkedItem) + "/"));
if (result.Length > 0) result = result.TrimEnd(new char[] {'/'});
return result;
}
分隔符这个可以放如参数中,更灵活的变化分隔符。
2.在得知CheckedListBox绑定list对象的某个属性的集合之后来选中list中的项目。如问题1中,返回的字符串,在得到字符串后如何初始化选中状态。
代码
public static void SetCheckedItmsByNames(this CheckedListBox box, string[] names)
{
for (int i = 0; i < box.Items.Count;i++ )
{
foreach (string name in names)
{
if (box.GetItemText(box.Items[i]) == name)
{
box.SetItemChecked(i, true);
}
}
}
}
public static void SetCheckedItmsByNames(this CheckedListBox box, string names)
{
if(string.IsNullOrEmpty(names)) return;
string[] name = names.Split(new char[] {'/'});
SetCheckedItmsByNames(box,name);
}
{
for (int i = 0; i < box.Items.Count;i++ )
{
foreach (string name in names)
{
if (box.GetItemText(box.Items[i]) == name)
{
box.SetItemChecked(i, true);
}
}
}
}
public static void SetCheckedItmsByNames(this CheckedListBox box, string names)
{
if(string.IsNullOrEmpty(names)) return;
string[] name = names.Split(new char[] {'/'});
SetCheckedItmsByNames(box,name);
}
3. 当得到了选择项目的字符串获取id串,反之亦ok的方法:
//已知names,取ids
ids = ComFunction.GetNamesOrIdsFromIEnumerable(ChcckedListBox.DataSource as IEnumerable<b_station>,
(w, s) => w.name.ToString() == s ? w.id.ToString() : string.Empty,
names);
//已知ids,取names
ids = ComFunction.GetNamesOrIdsFromIEnumerable(ChcckedListBox.DataSource as IEnumerable<b_station>,
(w, s) => w.id.ToString() == s ? w.name.ToString() : string.Empty,
names);
ids = ComFunction.GetNamesOrIdsFromIEnumerable(ChcckedListBox.DataSource as IEnumerable<b_station>,
(w, s) => w.name.ToString() == s ? w.id.ToString() : string.Empty,
names);
//已知ids,取names
ids = ComFunction.GetNamesOrIdsFromIEnumerable(ChcckedListBox.DataSource as IEnumerable<b_station>,
(w, s) => w.id.ToString() == s ? w.name.ToString() : string.Empty,
names);
实现方法如下:
public static string GetNamesOrIdsFromIEnumerable<T>(IEnumerable<T> list,Func<T,string,string> func,string ids)
{
string result = string.Empty;
if (string.IsNullOrEmpty(ids)||list==null)
return result;
string[] id = ids.Split(new[]{'/'});
foreach (string s in id)
{
string temp;
foreach (T model in list)
{
temp = func(model, s);
if (string.IsNullOrEmpty(temp)) continue;
result += string.Format("{0}/", temp);
break;
}
}
if (result.Length > 0)
result = result.TrimEnd(new[] {'/'});
return result;
}
{
string result = string.Empty;
if (string.IsNullOrEmpty(ids)||list==null)
return result;
string[] id = ids.Split(new[]{'/'});
foreach (string s in id)
{
string temp;
foreach (T model in list)
{
temp = func(model, s);
if (string.IsNullOrEmpty(temp)) continue;
result += string.Format("{0}/", temp);
break;
}
}
if (result.Length > 0)
result = result.TrimEnd(new[] {'/'});
return result;
}
通过遍历ids对应的元素和list集合中的元素利用func方法进行比较确认是否符合要求,取得结果。当时主要是很多类型的对象集合需要绑定到CheckedListBox进行操作,而设计的时候又有很多ids,或者names,或者有其一。乱啊。
这次开发主要负责代码实现,没有参与需求和设计,写代码的心思就多了,所以对其他的控件也有提炼了比较多的通用方法和封装(如combobox的搜索效果,支持模糊匹配,like ‘%word%’)等http://www.cnblogs.com/buaaboyi/archive/2010/12/21/1913245.html