日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

linq 解决winForm中控件CheckedListBox操作的问题。(转载)

發布時間:2023/12/2 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linq 解决winForm中控件CheckedListBox操作的问题。(转载) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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;
????????}

?

分隔符這個可以放如參數中,更靈活的變化分隔符。

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);
????????}
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);

?

?實現方法如下:

?

????????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對應的元素和list集合中的元素利用func方法進行比較確認是否符合要求,取得結果。當時主要是很多類型的對象集合需要綁定到CheckedListBox進行操作,而設計的時候又有很多ids,或者names,或者有其一。亂啊。

這次開發主要負責代碼實現,沒有參與需求和設計,寫代碼的心思就多了,所以對其他的控件也有提煉了比較多的通用方法和封裝(如combobox的搜索效果,支持模糊匹配,like ‘%word%’)等轉自:http://www.cnblogs.com/buaaboyi/archive/2010/12/21/1913245.html

轉載于:https://www.cnblogs.com/johnwonder/archive/2010/12/22/1913373.html

總結

以上是生活随笔為你收集整理的linq 解决winForm中控件CheckedListBox操作的问题。(转载)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。