• C# Combobox


    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Windows.Forms;
    
    namespace KK.Control.Helper
    {
        public class CmbHelper
        {
            //定义一个用于保存静态变量的实例
            private static CmbHelper instance = null;
            //定义一个保证线程同步的标识
            private static readonly object locker = new object();
            //构造函数为私有,使外界不能创建该类的实例
            private CmbHelper() { }
            public static CmbHelper Instance
            {
                get
                {
                    if (instance == null)
                    {
                        lock (locker)
                        {
                            if (instance == null) instance = new CmbHelper();
                        }
                    }
                    return instance;
                }
            }
    
            public TModel GetTModel<TModel>(ComboBox cmb)
            {
                //ZB.QueueSys.Model.QS_QUEUE queue = this.cmbQueue.SelectedItem as ZB.QueueSys.Model.QS_QUEUE;
                //StudentInfos stu = CmbHelper.Instance.GetTModel<StudentInfos>(this.cmbTest);
                return (TModel)cmb.SelectedItem;
            }
    
            public void BindingCmb<TModel>(List<TModel> list, ComboBox cmb, string displayMember)
            {
                if (list == null || list.Count <= 0)
                {
                    cmb.DataSource = null;
                    return;
                }
                cmb.DataSource = list;
                cmb.DisplayMember = displayMember;
                cmb.SelectedIndex = 0;
                //List<V_APPLYDEPART> departs = AppDepartBLL.GetApplyDeparts();
                //if (departs != null && departs.Count > 0) departs.Insert(0, new V_APPLYDEPART { APPLYDEPART = PubVariable.All, DEPARTID = "-1" });
                //PubHelper.Instance.BindingCmb(departs, this.cmbDepart, "APPLYDEPART");
            }
    
            public void BindingCmbAndInsetAll<TModel>(List<TModel> list, ComboBox cmb, string displayMember, string all = "全部")
            {
                if (list == null || list.Count <= 0)
                {
                    cmb.DataSource = null;
                    return;
                }
                cmb.Items.Clear();
                cmb.Items.Add(all);
                foreach (TModel item in list)
                {
                    cmb.Items.Add(item);
                }
                //cmb.DataSource = list;
                cmb.DisplayMember = displayMember;
                cmb.SelectedIndex = 0;
                //PubHelper.Instance.BindingCmb(departs, this.cmbDepart, "APPLYDEPART");
            }
    
            /// <summary>
            /// 为ComboBox绑定数据源并提供下拉提示
            /// </summary>
            /// <typeparam name="T">泛型</typeparam>
            /// <param name="combox">ComboBox</param>
            /// <param name="list">数据源</param>
            /// <param name="displayMember">显示字段</param>
            /// <param name="valueMember">隐式字段</param>
            /// <param name="displayText">下拉提示文字</param>
            public void Bind<T>(ComboBox combox, IList<T> list, string displayMember, string valueMember, string displayText = "全部")
            {
                AddItem(list, displayMember, displayText);
                combox.DataSource = list;
                combox.DisplayMember = displayMember;
                if (!string.IsNullOrEmpty(valueMember))
                    combox.ValueMember = valueMember;
            }
    
            private void AddItem<T>(IList<T> list, string displayMember, string displayText)
            {
                Object _obj = Activator.CreateInstance<T>();
                Type _type = _obj.GetType();
                if (!string.IsNullOrEmpty(displayMember))
                {
                    PropertyInfo _displayProperty = _type.GetProperty(displayMember);
                    _displayProperty.SetValue(_obj, displayText, null);
                }
                list.Insert(0, (T)_obj);
            }
    
    
        }
    }
    

      

    博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
  • 相关阅读:
    [置顶] 一个懦弱的IT人
    Android ListView的理解(一)
    不允许调用库函数,也不允许使用任何全局或局部变量编写strlen函数
    http-使用get和post方式提交数据
    ILOG的一个基本应用——解决运输问题、转运问题
    原生js获取execl里面的值 主要使用ActiveXObject
    (顺序表的应用5.4.2)POJ 1591 M*A*S*H(约瑟夫环问题的变形——变换步长值)
    HDU 3032 Nim or not Nim? (sg函数)
    Hadoop入门实践之从WordCount程序说起
    仅复制备份(简单恢复模式)
  • 原文地址:https://www.cnblogs.com/YYkun/p/14814073.html
Copyright © 2020-2023  润新知