IntellisenseTextBox 控件
该控件实现功能:
1.文本框下拉智能匹配的数据。
2.使用键盘上下键控制或鼠标选择选项。
3.在文字变化的时候同时实现同步识别。
属性;
EditValue string 类型 文本内容
WaterText string 类型 水印文本内容
TextList List<string> 类型 下拉内容
目前采用了DEV控件,后期处理需做去DEV控件化。
源码:
private string editValue; /// <summary> /// 文本内容 /// </summary> public string EditValue { get { return textValue.Text; } set { editValue = value; textValue.Text = value; } } private string waterText; /// <summary> /// 水印文本内容 /// </summary> public string WaterText { get { return waterText; } set { waterText = value; if (!String.IsNullOrEmpty(waterText)) { textValue.Properties.NullText = waterText; textValue.Properties.NullValuePrompt = waterText; textValue.Properties.NullValuePromptShowForEmptyValue = true; } } } /// <summary> /// 选中的项编号 /// </summary> private int SelectIndex = 0; /// <summary> /// 选中的项内容 /// </summary> private string SelectValue = ""; private List<string> textList; /// <summary> /// 下拉内容 /// </summary> public List<string> TextList { get { return textList; } set { textList = value; } } /// <summary> /// 下拉显示 /// </summary> private List<string> ShowTextList; private bool IsDown = false; Graphics g; public IntellisenseTextBox() { InitializeComponent(); } private void IntellisenseTextBox_Load(object sender, EventArgs e) { ShowTextList = new List<string>(); } /// <summary> /// 绘制下拉内容 /// </summary> private void AddList() { int count = ShowTextList.Count(); if (count < 1) return; int invalCount = count; for (int i = 0; i < invalCount; i++) { PanelControl panel = new PanelControl(); panel.Name = "panel" + i; panel.Tag = i; panel.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder; panel.Width = this.textValue.Width + 10; panel.Height = 30; panel.Location = new Point(1, 30 + i * 30+1); panel.MouseHover += panel_MouseHover; panel.MouseMove += panel_MouseHover; panel.MouseLeave += panel_MouseLeave; panel.Click += panel_Click; LabelControl label = new LabelControl(); label.Name = "label" + i; label.Text = ShowTextList[i]; label.Location = new Point(13, 5); label.Font = this.textValue.Font; label.ForeColor = Color.FromArgb(51, 51, 51); label.MouseHover += label_MouseHover; label.MouseMove += label_MouseHover; label.MouseLeave += label_MouseLeave; label.Click += label_Click; panel.Controls.Add(label); this.Controls.Add(panel); } } /// <summary> /// 展开下拉 /// </summary> private void DropList() { int count = ShowTextList.Count(); if (count < 1) return; int invalCount = count; this.Height = this.Height + invalCount * 30 +1;//下拉行 行高为30 AddList(); g = this.CreateGraphics(); g.DrawRectangle(new Pen(Brushes.Black), 0, 0, this.Width - 1, this.Height - 1); g.Dispose(); IsDown = true; selectId = -1; } /// <summary> /// 关闭下拉 /// </summary> private void CloseList() { List<Control> controlList = new List<Control>(); foreach (Control item in this.Controls) { if (item.Name.Contains("panel")) { controlList.Add(item); } } foreach (var item in controlList) { this.Controls.Remove(item); } this.Height = 32; g = this.CreateGraphics(); Color rectColor = Color.FromArgb(204, 204, 204); g.DrawRectangle(new Pen(Brushes.Black), 0, 0, this.Width - 1, this.Height - 1); g.Dispose(); IsDown = false; } void panel_Click(object sender, EventArgs e) { PanelControl panel = (PanelControl)sender; string name = panel.Name; int index = int.Parse(name.Substring(5, name.Length - 5)); SelectIndex = index; SelectValue = ShowTextList[index]; this.textValue.Text = SelectValue; CloseList(); } void label_Click(object sender, EventArgs e) { LabelControl lab = (LabelControl)sender; string name = lab.Name; int index = int.Parse(name.Substring(5, name.Length - 5)); SelectIndex = index; SelectValue = ShowTextList[index]; this.textValue.Text = SelectValue; CloseList(); } void panel_MouseHover(object sender, EventArgs e) { PanelControl panel = (PanelControl)sender; panel.BackColor = Color.FromArgb(184, 39, 18); foreach (Control item in panel.Controls) { LabelControl lab = (LabelControl)item as LabelControl; lab.ForeColor = Color.White; } } void panel_MouseLeave(object sender, EventArgs e) { PanelControl panel = (PanelControl)sender; panel.BackColor = Color.White; foreach (Control item in panel.Controls) { LabelControl lab = (LabelControl)item as LabelControl; lab.ForeColor = Color.FromArgb(51, 51, 51); } } void label_MouseHover(object sender, EventArgs e) { LabelControl lab = (LabelControl)sender; lab.ForeColor = Color.White; PanelControl panel = (PanelControl)lab.Parent; panel.BackColor = Color.FromArgb(184, 39, 18); } void label_MouseLeave(object sender, EventArgs e) { LabelControl lab = (LabelControl)sender; lab.ForeColor = Color.FromArgb(51, 51, 51); PanelControl panel = (PanelControl)lab.Parent; panel.BackColor = Color.White; } private void textValue_TextChanged(object sender, EventArgs e) { string searchText = textValue.Text.Trim(); if (String.IsNullOrEmpty(searchText)) return; if (textList == null) return; ShowTextList.Clear(); ShowTextList = textList.Where(s => s.Contains(searchText)).ToList(); if (ShowTextList == null) return; if (ShowTextList.Count < 1) return; if (ShowTextList.Count > 4) ShowTextList = ShowTextList.GetRange(0, 4); CloseList(); DropList(); } private void IntellisenseTextBox_Paint(object sender, PaintEventArgs e) { Graphics gh = e.Graphics; Color rectColor = Color.FromArgb(204, 204, 204); gh.DrawRectangle(new Pen(Brushes.Black), 0, 0, this.Width - 1, this.Height - 1); gh.Dispose(); } private int selectId = -1; protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (IsDown) { switch (keyData) { case Keys.Down: if (selectId < ShowTextList.Count - 1) selectId++; foreach (Control item in this.Controls) { if (item.Name.Contains("panel")) { if (item.Tag.ToString() == selectId.ToString()) { PanelControl panel = (PanelControl)item as PanelControl; panel.BackColor = Color.FromArgb(184, 39, 18); foreach (Control child in panel.Controls) { LabelControl lab = (LabelControl)child as LabelControl; lab.ForeColor = Color.White; } } else { PanelControl panel = (PanelControl)item; panel.BackColor = Color.White; foreach (Control child in panel.Controls) { LabelControl lab = (LabelControl)child as LabelControl; lab.ForeColor = Color.FromArgb(51, 51, 51); } } } } break; case Keys.Up: if (selectId > 0) selectId--; foreach (Control item in this.Controls) { if (item.Name.Contains("panel")) { if (item.Tag.ToString() == selectId.ToString()) { PanelControl panel = (PanelControl)item as PanelControl; panel.BackColor = Color.FromArgb(184, 39, 18); foreach (Control child in panel.Controls) { LabelControl lab = (LabelControl)child as LabelControl; lab.ForeColor = Color.White; } } else { PanelControl panel = (PanelControl)item; panel.BackColor = Color.White; foreach (Control child in panel.Controls) { LabelControl lab = (LabelControl)child as LabelControl; lab.ForeColor = Color.FromArgb(51, 51, 51); } } } } break; case Keys.Enter: foreach (Control item in this.Controls) { if (item.Name.Contains("panel")) { if (item.Tag.ToString() == selectId.ToString()) { PanelControl panel = (PanelControl)item as PanelControl; foreach (Control child in panel.Controls) { LabelControl lab = (LabelControl)child as LabelControl; this.textValue.Text = lab.Text; CloseList(); break; } } } } break; } } return base.ProcessCmdKey(ref msg, keyData); }
文章评论