---
title: "带下拉菜单的文本框"
author: "awark"
date: "2011-09-26T11:05:14Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-09-26T11:05:14Z"
id: 1180445
url: "https://awark.iteye.com/blog/1180445"
---

程序需要做一个带下拉菜单的文本框以方便用户输入,大概类似于下图中这种TextBox:

 

![](http://hi.csdn.net/attachment/201008/6/0_12810639547PNn.gif)

控件有一个数据源,用的DataTable格式,还有一个值columnName来表示用Table中的哪一列数据,控件将根据这一列的数据来进行下拉框提示.

界面只添加了一个文本框和一个ListBox,

组件生成器中的代码为:

 

[view plain](http://blog.csdn.net/rrrrssss00/article/details/5792675 "view plain")[copy to clipboard](http://blog.csdn.net/rrrrssss00/article/details/5792675 "copy to clipboard")[print](http://blog.csdn.net/rrrrssss00/article/details/5792675 "print")[?](http://blog.csdn.net/rrrrssss00/article/details/5792675 "?")

01. #region 组件设计器生成的代码
02. /// &lt;summary&gt;
03. /// 设计器支持所需的方法 - 不要
04. /// 使用代码编辑器修改此方法的内容。
05. /// &lt;/summary&gt;
06. **private** **void** InitializeComponent()
07. {
08. **this**.textBox1 = **new** System.Windows.Forms.TextBox();
09. **this**.listBox1 = **new** System.Windows.Forms.ListBox();
10. **this**.SuspendLayout();
11. //
12. // textBox1
13. //
14. **this**.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
15. **this**.textBox1.Location = **new** System.Drawing.Point(0, 0);
16. **this**.textBox1.Name = "textBox1";
17. **this**.textBox1.Size = **new** System.Drawing.Size(189, 21);
18. **this**.textBox1.TabIndex = 0;
19. **this**.textBox1.TextChanged += **new** System.EventHandler(**this**.textBox1\_TextChanged);
20. **this**.textBox1.KeyDown += **new** System.Windows.Forms.KeyEventHandler(**this**.textBox1\_KeyDown);
21. **this**.textBox1.Leave += **new** System.EventHandler(**this**.textBox1\_Leave);
22. //
23. // listBox1
24. //
25. **this**.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
26. **this**.listBox1.FormattingEnabled = **true**;
27. **this**.listBox1.ItemHeight = 12;
28. **this**.listBox1.Location = **new** System.Drawing.Point(0, 21);
29. **this**.listBox1.Name = "listBox1";
30. **this**.listBox1.Size = **new** System.Drawing.Size(189, 4);
31. **this**.listBox1.TabIndex = 1;
32. **this**.listBox1.Leave += **new** System.EventHandler(**this**.listBox1\_Leave);
33. **this**.listBox1.DoubleClick += **new** System.EventHandler(**this**.listBox1\_DoubleClick);
34. **this**.listBox1.KeyDown += **new** System.Windows.Forms.KeyEventHandler(**this**.listBox1\_KeyDown);
35. //
36. // TextBoxWithDataPick
37. //
38. **this**.AutoScaleDimensions = **new** System.Drawing.SizeF(6F, 12F);
39. **this**.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
40. **this**.Controls.Add(**this**.listBox1);
41. **this**.Controls.Add(**this**.textBox1);
42. **this**.Name = "TextBoxWithDataPick";
43. **this**.Size = **new** System.Drawing.Size(189, 25);
44. **this**.Load += **new** System.EventHandler(**this**.TextBoxWithDataPick\_Load);
45. **this**.ResumeLayout(**false**);
46. **this**.PerformLayout();
47. }
48. #endregion
49. **private** System.Windows.Forms.TextBox textBox1;
50. **private** System.Windows.Forms.ListBox listBox1;

 

 

类代码为:

 

 

[view plain](http://blog.csdn.net/rrrrssss00/article/details/5792675 "view plain")[copy to clipboard](http://blog.csdn.net/rrrrssss00/article/details/5792675 "copy to clipboard")[print](http://blog.csdn.net/rrrrssss00/article/details/5792675 "print")[?](http://blog.csdn.net/rrrrssss00/article/details/5792675 "?")

001. **public** partial **class** TextBoxWithDataPick : UserControl
002. {
003. **public** TextBoxWithDataPick()
004. {
005. InitializeComponent();
006. }
007. /// &lt;summary&gt;文本&lt;/summary&gt;
008. **public** **string** text
009. { **get**{ **return** textBox1.Text; }
010. **set**{ textBox1.Text = value;}
011. }
012. /// &lt;summary&gt;可供用户选择的数据集&lt;/summary&gt;
013. **public** DataTable dataSource = **null**;
014. /// &lt;summary&gt;在dataSource中的列名&lt;/summary&gt;
015. **public** **string** columnName = "";
016. **private** **void** TextBoxWithDataPick\_Load(**object** sender, EventArgs e)
017. {
018. //控件载入时,将高度缩为与TextBox相等
019. **this**.Height = textBox1.Height;
020. }
021. **private** **void** textBox1\_TextChanged(**object** sender, EventArgs e)
022. {
023. **if** (**this**.textBox1.Text == "" )
024. {
025. **this**.Height = textBox1.Height;
026. **this**.listBox1.Visible = **false**;
027. **this**.SendToBack();
028. **return**;
029. }
030. //这些情况下,不弹出选择框
031. **if** (dataSource == **null** || columnName == "" || !dataSource.Columns.Contains(columnName))
032. **return**;
033. //根据用户当前输入的内容,筛选出与内容相符的记录,显示在列表框中
034. **this**.listBox1.Items.Clear();
035. **for** (**int** i = 0; i &lt; dataSource.Rows.Count; i++)
036. {
037. **if** (dataSource.Rows\[i]\[columnName].ToString().IndexOf(textBox1.Text) == 0)
038. {
039. listBox1.Items.Add(dataSource.Rows\[i]\[columnName].ToString());
040. }
041. }
042. //如果记录数不为0,则将列表显示出来
043. **if** (listBox1.Items.Count == 0)
044. {
045. **this**.Height = textBox1.Height;
046. **this**.listBox1.Visible = **false**;
047. **this**.SendToBack();
048. }
049. **else**
050. {
051. **if** (listBox1.Items.Count == 1)
052. {
053. **this**.Height = textBox1.Height + 20 * listBox1.Items.Count;
054. **this**.listBox1.Visible = **true**;
055. **this**.BringToFront();
056. }
057. **else** **if** (listBox1.Items.Count&lt;8)
058. {
059. **this**.Height = textBox1.Height + 14 * listBox1.Items.Count;
060. **this**.listBox1.Visible = **true**;
061. **this**.BringToFront();
062. }
063. **else**
064. {
065. **this**.Height = textBox1.Height + 108;
066. **this**.listBox1.Visible = **true**;
067. **this**.BringToFront();
068. }
069. }
070. }
071. //用户在焦点为listBox1时按下回车,将当前选中的内容提交到TextBox中,并隐藏ListBox
072. **private** **void** listBox1\_KeyDown(**object** sender, KeyEventArgs e)
073. {
074. **if** (e.KeyValue == (**int**)Keys.Enter)
075. {
076. **this**.textBox1.Text = listBox1.SelectedItem.ToString();
077. **this**.Height = textBox1.Height;
078. **this**.listBox1.Visible = **false**;
079. **this**.SendToBack();
080. }
081. }
082. //用户双击listBox1某项时,将当前选中的内容提交到TextBox中,并隐藏ListBox
083. **private** **void** listBox1\_DoubleClick(**object** sender, EventArgs e)
084. {
085. **if** (listBox1.SelectedItem != **null**)
086. {
087. **this**.textBox1.Text = listBox1.SelectedItem.ToString();
088. **this**.Height = textBox1.Height;
089. **this**.listBox1.Visible = **false**;
090. **this**.SendToBack();
091. }
092. }
093. //用户在TextBox中点击向下箭头时,将焦点交给ListBox,使用户能够选择其中的项
094. **private** **void** textBox1\_KeyDown(**object** sender, KeyEventArgs e)
095. {
096. **if** (e.KeyValue == (**int**)Keys.Down)
097. {
098. **if** (listBox1.Items.Count != 0)
099. {
100. listBox1.Focus();
101. listBox1.SelectedIndex = 0;
102. }
103. }
104. }
105. //TextBox失去焦点时,隐藏ListBox
106. **private** **void** textBox1\_Leave(**object** sender, EventArgs e)
107. {
108. **if** (listBox1.Focused == **false**)
109. {
110. **this**.Height = textBox1.Height;
111. **this**.listBox1.Visible = **false**;
112. **this**.SendToBack();
113. }
114. }
115. **private** **void** listBox1\_Leave(**object** sender, EventArgs e)
116. {
117. **if** (textBox1.Focused == **false**)
118. {
119. **this**.Height = textBox1.Height;
120. **this**.listBox1.Visible = **false**;
121. **this**.SendToBack();
122. }
123. }
124. }