zoukankan      html  css  js  c++  java
  • linq 解决winForm中控件CheckedListBox操作的问题。

    最近有幸被邀做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%’)等。下回见!

  • 相关阅读:
    HDU
    HDU
    HDU
    HDU
    HDU
    HDU
    HDU
    HDU
    HDU
    HDU
  • 原文地址:https://www.cnblogs.com/buaaboyi/p/1913245.html
Copyright © 2011-2022 走看看