cmbBind(cmbDataItem, 21);
cmbDataItem.TextUpdate += new System.EventHandler(this.cmbDataItem_TextUpdate);
/// <summary>
/// 修改comboBox中值高度
/// </summary>
/// <param name="list"></param>
/// <param name="itemHeight"></param>
public static void cmbBind(ComboBox list, int itemHeight)
{
list.DropDownStyle = ComboBoxStyle.DropDownList;
list.ItemHeight = itemHeight;
list.DrawMode = DrawMode.OwnerDrawVariable;
list.DrawItem += new DrawItemEventHandler(delegate (object sender, DrawItemEventArgs e)
{
if (e.Index < 0)
{
return;
}
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawString(list.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);
});
}
private void cmbDataItem_TextUpdate(object sender, EventArgs e)
{
string s = this.cmbDataItem.Text; //获取cb_material控件输入内容
var matchDic = from d in dicDataItem where d.Value.Contains(s) select d;
//清空combobox
this.cmbDataItem.Items.Clear();
if (matchDic.Count() >= 1) // 存在符合条件的内容
{
//将符合条件的内容加到combobox中
foreach (var item in matchDic)
{
// 根据输入的值模糊查询,将符合条件的值存储到新strListNew的集合里面
cmbDataItem.Items.Add(new CmbItem(item.Value, item.Key));
}
}
else // 不存在符合条件时
{
// 下列代码为当查询不到符合的条件时新增自身输入的值
this.cmbDataItem.Items.Add(this.cmbDataItem.Text);
}
//设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列
this.cmbDataItem.SelectionStart = this.cmbDataItem.Text.Length; // 设置光标位置,若不设置:光标位置始终保持在第一列,造成输入关键词的倒序排列
Cursor = Cursors.Default; //保持鼠标指针原来状态,有时候鼠标指针会被下拉框覆盖,所以要进行一次设置
cmbDataItem.DroppedDown = true; // 自动弹出下拉框
cmbDataItem.MaxDropDownItems = 8; // 自动弹出下拉框
}