ComboBox简码输入的简单实现
前段时间因为需要使用ComboBox来绑定数据,并且要通过绑定对象的首字母简码快速查找到绑定项,但是控件本身并没有这项功能(不知是不是没有发现,*_*),为此编写该实现。这只是一个简单实现,可以确定是通过首字母包含查询还是以输入首字母开始查询,比较简单,可以根据需要进行修改(By:waterfrost)。
代码比较简单,直接贴代码。实例化时需要传入绑定的对象(此处用的是自定义的UserOrg,可以根据需要自己进行修改)以及作用的控件及简码匹配类型。主要方法代码如下:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms;
6 using System.Collections;
7 using ComboBoxSimplecode.UserOrgs;
8 namespace ComboBoxSimplecode
9 {
10 class cmbHelper
11 {
12 //保存每条数据与其简码对应的字典集合
13 Dictionary<string, UserOrg> item = new Dictionary<string, UserOrg>();
14 List<UserOrg> sourceData = new List<UserOrg>();
15 /// <summary>
16 /// 保存每次更新数据项集合
17 /// </summary>
18 private List<UserOrg> SourceData
19 {
20 get { return sourceData; }
21 set { sourceData = value; }
22 }
23 List<UserOrg> allSource = new List<UserOrg>();
24 /// <summary>
25 /// 保存初始化的所有数据项
26 /// 如果ComboBox数据源需要是所
27 /// 有数据绑定,则用该数据绑定
28 /// </summary>
29 private List<UserOrg> AllSource
30 {
31 get { return allSource; }
32 set { allSource = value; }
33 }
34
35 string text = "";
36 /// <summary>
37 /// 保存ComboBox的text值
38 /// 使用时赋值为ComboBox的text值
39 /// </summary>
40 private string Text
41 {
42 get { return text; }
43 set { text = value; }
44 }
45
46 private MatchEnum matchType = MatchEnum.MatchContains;
47 /// <summary>
48 /// 字符匹配类型(默认匹配字符串开头)
49 /// MatchStart为
50 /// MatchContains匹配字符串包含
51 /// </summary>
52 internal MatchEnum MatchType
53 {
54 get { return matchType; }
55 set { matchType = value; }
56 }
57 int dropDownItemCounts = 10;
58 /// <summary>
59 /// 下拉列表显示项数
60 /// 默认下拉列表显示10条数据
61 /// </summary>
62 public int DropDownItemCounts
63 {
64 get { return dropDownItemCounts; }
65 set { dropDownItemCounts = value; }
66 }
67 bool dropDownFlag = false;
68 //保存需要绑定的ComboBox控件引用
69 ComboBox cmb;
70 /// <summary>
71 /// 使用该类时,初始化ComboBox的数据源即可
72 /// </summary>
73 /// <param name="o">数据源来源</param>
74 /// <param name="cb">要绑定的ComboBox控件</param>
75 /// <param name="matchStyle">字符串匹配方式</param>
76 public cmbHelper(UserOrg[] users, ComboBox cb, MatchEnum matchType)
77 {
78 try
79 {
80 foreach (UserOrg user in users)
81 {
82 if (user != null)
83 {
84 item.Add(user.SimpleCode, user);//初始化userDTO与简码对应字典
85 sourceData.Add(user);
86 allSource.Add(user);//取得数据源的所有值集合
87 }
88 }
89 this.matchType = matchType;
90 this.cmb = cb;
91 cmb.Sorted = false;
92 cmb.DropDownHeight = cmb.ItemHeight * dropDownItemCounts;//初始化ComboBox下拉列表的高度
93 cmb.DataSource = sourceData;//初始化combobox的绑定
94 cmb.DisplayMember = "Name";
95 cmb.SelectedItem = null;
96 cmb.TextUpdate += new EventHandler(cmb_TextUpdate);
97 cmb.DropDown += new EventHandler(cmb_DropDown);
98 }
99 catch (Exception ex)
100 {
101 if (users == null)
102 {
103 throw new Exception("传入UserOrg[]为空!");
104 }
105 else
106 {
107 throw ex;
108 }
109 }
110 }
111 /// <summary>
112 /// 使用时该类时,初始化ComboBox的数据源即可
113 /// </summary>
114 /// <param name="users">数据源来源</param>
115 /// <param name="cb">要绑定的ComboBox控件</param>
116 /// <param name="ComboBoxDropDownWidth">初始化ComboBox下拉列表项数默认10项</param>
117 public cmbHelper(UserOrg[] users, ComboBox cb, MatchEnum matchType, int ComboBoxDropDownItemCount)
118 : this(users, cb, matchType)
119 {
120 this.dropDownItemCounts = ComboBoxDropDownItemCount;
121 }
122 /// <summary>
123 /// 点击combobox的下拉箭头,则绑定数据源为所有数据集合
124 /// </summary>
125 /// <param name="sender"></param>
126 /// <param name="e"></param>
127 void cmb_DropDown(object sender, EventArgs e)
128 {
129 if (!dropDownFlag)
130 {
131 cmb.DataSource = allSource;
132 cmb.IntegralHeight = false;
133 }
134 cmb.DisplayMember = "Name";
135 cmb.SelectedItem = null;
136 }
137
138 void cmb_TextUpdate(object sender, EventArgs e)
139 {
140 text = cmb.Text;
141 GetCmbDataSource(text);
142 }
143 /// <summary>
144 /// 绑定ComboBox简码输入数据源
145 /// </summary>
146 /// <param name="input">ComboBox的输入值</param>
147 private void GetCmbDataSource(string cmbText)
148 {
149 try
150 {
151 cmb.Sorted = false;
152 sourceData.Clear();
153 foreach (string i in item.Keys)
154 {
155 string iup = i.ToUpper();
156 string inputup = cmbText.ToUpper();
157 if (matchType.Equals(MatchEnum.MatchContains))
158 {
159 if (iup.Contains(inputup))
160 {
161 UserOrg itemForBind = item[i];
162 if (!sourceData.Contains(itemForBind))
163 {
164 sourceData.Add(itemForBind);
165 }
166 }
167 }
168 else if (matchType.Equals(MatchEnum.MatchStart))
169 {
170 if (iup.StartsWith(inputup))
171 {
172 UserOrg itemForBind = item[i];
173 if (!sourceData.Contains(itemForBind))
174 {
175 sourceData.Add(itemForBind);
176 }
177 }
178 }
179 }
180 if (sourceData.Count > 0)
181 {
182 cmb.DataSource = sourceData.ToArray();
183 }
184 //如果没有匹配则数据源为所有数据
185 else if (sourceData.Count <= 0 || string.IsNullOrEmpty(cmbText))
186 {
187 cmb.DataSource = allSource;
188 }
189 cmb.DisplayMember = "Name";
190 //如果项数大于设定的下拉高度则不自动拉伸,否则自动拉伸
191 if (cmb.Items.Count > dropDownItemCounts)
192 {
193 cmb.IntegralHeight = false;
194 }
195 else
196 {
197 cmb.IntegralHeight = true;
198 }
199 dropDownFlag = true;
200 cmb.DroppedDown = true;
201 dropDownFlag = false;
202 cmb.Text = text;
203 cmb.Select(cmb.Text.Length, 1);
204 Cursor.Show();
205 }
206 catch (Exception ex)
207 {
208 throw ex;
209 }
210 }
211 }
212 public enum MatchEnum : int
213 {
214 MatchStart = 1,
215 MatchContains = 2
216 }
217 }
218
示例源代码下载!
浙公网安备 33010602011771号