linq 解决winForm中控件CheckedListBox操作的问题。(转载)
生活随笔
收集整理的這篇文章主要介紹了
linq 解决winForm中控件CheckedListBox操作的问题。(转载)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.獲取CheckedListBox選中項(xiàng)的文本的字符串,并用分隔符連接。系統(tǒng)沒有提供相應(yīng)的屬性,方法。
這里我們利用3.0的特性給其來個(gè)擴(kuò)展方法,如下:
????????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;
????????}
?
分隔符這個(gè)可以放如參數(shù)中,更靈活的變化分隔符。
2.在得知CheckedListBox綁定list對(duì)象的某個(gè)屬性的集合之后來選中l(wèi)ist中的項(xiàng)目。如問題1中,返回的字符串,在得到字符串后如何初始化選中狀態(tài)。
?
代碼 ???????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);
????????} 3. 當(dāng)?shù)玫搅诉x擇項(xiàng)目的字符串獲取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);
?
?實(shí)現(xiàn)方法如下:
?
????????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;
????????}
?
通過遍歷ids對(duì)應(yīng)的元素和list集合中的元素利用func方法進(jìn)行比較確認(rèn)是否符合要求,取得結(jié)果。當(dāng)時(shí)主要是很多類型的對(duì)象集合需要綁定到CheckedListBox進(jìn)行操作,而設(shè)計(jì)的時(shí)候又有很多ids,或者names,或者有其一。亂啊。
這次開發(fā)主要負(fù)責(zé)代碼實(shí)現(xiàn),沒有參與需求和設(shè)計(jì),寫代碼的心思就多了,所以對(duì)其他的控件也有提煉了比較多的通用方法和封裝(如combobox的搜索效果,支持模糊匹配,like ‘%word%’)等轉(zhuǎn)自:http://www.cnblogs.com/buaaboyi/archive/2010/12/21/1913245.html轉(zhuǎn)載于:https://www.cnblogs.com/johnwonder/archive/2010/12/22/1913373.html
總結(jié)
以上是生活随笔為你收集整理的linq 解决winForm中控件CheckedListBox操作的问题。(转载)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DVT和ADVT - 为数不多的DICO
- 下一篇: Test of etco