C# FORM 自定义控件:带弹出(下拉)菜单的TextBox
c# 自定义 控件 菜单 Form textbox
2023-09-11 14:21:58 时间
C# FORM 自定义控件:带弹出(下拉)菜单的TextBox
2017年05月31日 13:50:58 ~typhoon~ 阅读数 5669
程序需要做一个带下拉菜单的文本框以方便用户输入,大概类似于下图中这种TextBox:
控件有一个数据源,用的DataTable格式,还有一个值columnName来表示用Table中的哪一列数据,控件将根据这一列的数据来进行下拉框提示.
界面只添加了一个文本框和一个ListBox,
组件生成器中的代码为:
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent(){
this.textBox1 = new System.Windows.Forms.TextBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(189, 21);
this.textBox1.TabIndex = 0;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
//
// listBox1
//
this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(0, 21);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(189, 4);
this.listBox1.TabIndex = 1;
this.listBox1.Leave += new System.EventHandler(this.listBox1_Leave);
this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
this.listBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.listBox1_KeyDown);
//
// TextBoxWithDataPick
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.listBox1);
this.Controls.Add(this.textBox1);
this.Name = "TextBoxWithDataPick";
this.Size = new System.Drawing.Size(189, 25);
this.Load += new System.EventHandler(this.TextBoxWithDataPick_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox listBox1;
类代码为:
public partial class TextBoxWithDataPick : UserControl
{
public TextBoxWithDataPick()
{
InitializeComponent();
}
/// <summary>文本</summary>
public string text
{ get{ return textBox1.Text; }
set{ textBox1.Text = value;}
}
/// <summary>可供用户选择的数据集</summary>
public DataTable dataSource = null;
/// <summary>在dataSource中的列名</summary>
public string columnName = "";
private void TextBoxWithDataPick_Load(object sender, EventArgs e)
{
//控件载入时,将高度缩为与TextBox相等
this.Height = textBox1.Height;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.textBox1.Text == "" )
{
this.Height = textBox1.Height;
this.listBox1.Visible = false;
this.SendToBack();
return;
}
//这些情况下,不弹出选择框
if (dataSource == null || columnName == "" || !dataSource.Columns.Contains(columnName))
return;
//根据用户当前输入的内容,筛选出与内容相符的记录,显示在列表框中
this.listBox1.Items.Clear();
for (int i = 0; i < dataSource.Rows.Count; i++)
{
if (dataSource.Rows[i][columnName].ToString().IndexOf(textBox1.Text) == 0)
{
listBox1.Items.Add(dataSource.Rows[i][columnName].ToString());
}
}
//如果记录数不为0,则将列表显示出来
if (listBox1.Items.Count == 0)
{
this.Height = textBox1.Height;
this.listBox1.Visible = false;
this.SendToBack();
}
else
{
if (listBox1.Items.Count == 1)
{
this.Height = textBox1.Height + 20 * listBox1.Items.Count;
this.listBox1.Visible = true;
this.BringToFront();
}
else if (listBox1.Items.Count<8)
{
this.Height = textBox1.Height + 14 * listBox1.Items.Count;
this.listBox1.Visible = true;
this.BringToFront();
}
else
{
this.Height = textBox1.Height + 108;
this.listBox1.Visible = true;
this.BringToFront();
}
}
}
//用户在焦点为listBox1时按下回车,将当前选中的内容提交到TextBox中,并隐藏ListBox
private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == (int)Keys.Enter)
{
this.textBox1.Text = listBox1.SelectedItem.ToString();
this.Height = textBox1.Height;
this.listBox1.Visible = false;
this.SendToBack();
}
}
//用户双击listBox1某项时,将当前选中的内容提交到TextBox中,并隐藏ListBox
private void listBox1_DoubleClick(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
this.textBox1.Text = listBox1.SelectedItem.ToString();
this.Height = textBox1.Height;
this.listBox1.Visible = false;
this.SendToBack();
}
}
//用户在TextBox中点击向下箭头时,将焦点交给ListBox,使用户能够选择其中的项
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == (int)Keys.Down)
{
if (listBox1.Items.Count != 0)
{
listBox1.Focus();
listBox1.SelectedIndex = 0;
}
}
}
//TextBox失去焦点时,隐藏ListBox
private void textBox1_Leave(object sender, EventArgs e)
{
if (listBox1.Focused == false)
{
this.Height = textBox1.Height;
this.listBox1.Visible = false;
this.SendToBack();
}
}
private void listBox1_Leave(object sender, EventArgs e)
{
if (textBox1.Focused == false)
{
this.Height = textBox1.Height;
this.listBox1.Visible = false;
this.SendToBack();
}
}
}
相关文章
- c# 抽象类+特性 验证QQ,Mobile,Name的通用写法
- C# 消息队列
- c#基础之异常处理及自定义异常 从SQLServer转储数据到MySQL
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
- C# 插件热插拔 .NET:何时应该 “包装异常”? log4.net 自定义日志文件名称
- [C#基础]c#中的BeginInvoke和EndEndInvoke
- C# IL DASM 使用-破解c#软件方法
- Word控件Spire.Doc 【书签】教程(8):用 C#、VB.NET 中的 Word 文档中的表格替换书签
- Word控件Spire.Doc 【Table】教程(10): 如何在 C#、VB.NET 中将嵌入式 Excel 工作表转换为 Word 表格
- Word控件Spire.Doc 【图像形状】教程(2) ;在 C#、VB.NET 中从 Word 中提取图像
- Word控件Spire.Doc 【表单域】教程(四):如何在 C#、VB.NET 中删除自定义属性字段
- Word处理控件Aspose.Words功能演示:在 C# 中将 Word 文档转换为 EPUB
- C#【基础篇】形参是值参数、引用参数(ref)、输出参数(out)的区别
- C#,图像二值化(12)——基于谷底最小值的全局阈值算法(Valley-Minium Thresholding)与源代码
- C#,打印漂亮杨辉三角形(帕斯卡三角形)的源代码
- 汇川AM402、H3U和上位机C#ModebusTcp通讯
- C# 异常处理
- C# 自定义控件
- C# 坦克游戏大战中学习相关类(Program)
- C#自定义应用程序上下文对象+IOC自己实现依赖注入
- C#winform自定义控件模拟设计时界面鼠标移动和调节大小、选中效果
- C#与matlab混合编程
- C# 学习书籍推荐(步步为营)
- C#-自定义InformationBox
- C#实现Winform自定义半透明遮罩层
- C# 自定义控件制作和使用实例(winform)