ITeye博客

带下拉菜单的文本框

awark · 2011-09-26 11:05 · 编程语言 · Markdown

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

 

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

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

组件生成器中的代码为:

 

view plaincopy to clipboardprint?

  1. #region 组件设计器生成的代码
  2. /// <summary>
  3. /// 设计器支持所需的方法 - 不要
  4. /// 使用代码编辑器修改此方法的内容。
  5. /// </summary>
  6. private void InitializeComponent()
  7. {
  8. this.textBox1 = new System.Windows.Forms.TextBox();
  9. 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 plaincopy to clipboardprint?

  1. public partial class TextBoxWithDataPick : UserControl
  2. {
  3. public TextBoxWithDataPick()
  4. {
  5. InitializeComponent();
  6. }
  7. /// <summary>文本</summary>
  8. public string text
  9. { get{ return textBox1.Text; }
  10. set{ textBox1.Text = value;}
  11. }
  12. /// <summary>可供用户选择的数据集</summary>
  13. public DataTable dataSource = null;
  14. /// <summary>在dataSource中的列名</summary>
  15. public string columnName = "";
  16. private void TextBoxWithDataPick_Load(object sender, EventArgs e)
  17. {
  18. //控件载入时,将高度缩为与TextBox相等
  19. this.Height = textBox1.Height;
  20. }
  21. private void textBox1_TextChanged(object sender, EventArgs e)
  22. {
  23. if (this.textBox1.Text == "" )
  24. {
  25. this.Height = textBox1.Height;
  26. this.listBox1.Visible = false;
  27. this.SendToBack();
  28. return;
  29. }
  30. //这些情况下,不弹出选择框
  31. if (dataSource == null || columnName == "" || !dataSource.Columns.Contains(columnName))
  32. return;
  33. //根据用户当前输入的内容,筛选出与内容相符的记录,显示在列表框中
  34. this.listBox1.Items.Clear();
  35. for (int i = 0; i < dataSource.Rows.Count; i++)
  36. {
  37. if (dataSource.Rows[i][columnName].ToString().IndexOf(textBox1.Text) == 0)
  38. {
  39. listBox1.Items.Add(dataSource.Rows[i][columnName].ToString());
  40. }
  41. }
  42. //如果记录数不为0,则将列表显示出来
  43. if (listBox1.Items.Count == 0)
  44. {
  45. this.Height = textBox1.Height;
  46. this.listBox1.Visible = false;
  47. this.SendToBack();
  48. }
  49. else
  50. {
  51. if (listBox1.Items.Count == 1)
  52. {
  53. this.Height = textBox1.Height + 20 * listBox1.Items.Count;
  54. this.listBox1.Visible = true;
  55. this.BringToFront();
  56. }
  57. else if (listBox1.Items.Count<8)
  58. {
  59. this.Height = textBox1.Height + 14 * listBox1.Items.Count;
  60. this.listBox1.Visible = true;
  61. this.BringToFront();
  62. }
  63. else
  64. {
  65. this.Height = textBox1.Height + 108;
  66. this.listBox1.Visible = true;
  67. this.BringToFront();
  68. }
  69. }
  70. }
  71. //用户在焦点为listBox1时按下回车,将当前选中的内容提交到TextBox中,并隐藏ListBox
  72. private void listBox1_KeyDown(object sender, KeyEventArgs e)
  73. {
  74. if (e.KeyValue == (int)Keys.Enter)
  75. {
  76. this.textBox1.Text = listBox1.SelectedItem.ToString();
  77. this.Height = textBox1.Height;
  78. this.listBox1.Visible = false;
  79. this.SendToBack();
  80. }
  81. }
  82. //用户双击listBox1某项时,将当前选中的内容提交到TextBox中,并隐藏ListBox
  83. private void listBox1_DoubleClick(object sender, EventArgs e)
  84. {
  85. if (listBox1.SelectedItem != null)
  86. {
  87. this.textBox1.Text = listBox1.SelectedItem.ToString();
  88. this.Height = textBox1.Height;
  89. this.listBox1.Visible = false;
  90. this.SendToBack();
  91. }
  92. }
  93. //用户在TextBox中点击向下箭头时,将焦点交给ListBox,使用户能够选择其中的项
  94. private void textBox1_KeyDown(object sender, KeyEventArgs e)
  95. {
  96. if (e.KeyValue == (int)Keys.Down)
  97. {
  98. if (listBox1.Items.Count != 0)
  99. {
  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. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。