实现百度搜索框功能

实现百度搜索框功能

2023年7月9日发(作者:)

实现百度搜索框功能实现效果:

基本的实现思路是 界⾯为TextBox(提供关键字的输⼊)+ListBox(相关内容以列表显⽰),数据的获取通过委托实现。 以下是控件实现代码:View Codeusing System;using c;using entModel;using g;using ;using ;using ;namespace MyControl{ ///

/// 查询器,实现输⼊查询条件时下拉框⾃ /// 动展开并显⽰相近的内容 /// public partial class UC_QueryBox : UserControl { /// /// 声明委托,⽤来获取下拉框中需要显⽰的项 /// /// 查询关键字 /// 查询结果集 public delegate List QueryDelegate(string p_KeyWord); public QueryDelegate GetData; private ListBox m_ListBox; /// /// 设置或获取关键字输⼊框 /// public TextBox M_TextBox { set { tx_context = value; } get { return tx_context; } } /// /// 设置或获取下拉框 /// public ListBox M_ListBox { set { m_ListBox = value; } get { return m_ListBox; } } /// /// 获取或设置查询关键字 /// public string M_QueryText { get { return this.M_; } set { this.M_ = value; } } /// /// 构造⽅法 /// public UC_QueryBox() { InitializeComponent(); } /// /// 当关键字输⼊框⽂本改变时,触发查询 /// /// /// private void tx_context_TextChanged(object sender, EventArgs e) { xDispose(); //如果关键字输⼊框内容不为空 if (!OrEmpty(tx_)) { //获取下拉框数据 List items = GetData(tx_); //如果查询的数据项不为空 if ( > 0) { xInit(items); } } } /// /// 下拉框中项被选时 /// /// /// private void tx_context_KeyDown(object sender, KeyEventArgs e) { if (m_ListBox != null) { //按下键盘↓键 选择索引向下移⼀位 if (e == && m_ > 0 && m_edIndex < m_ - 1) { m_edIndex = m_edIndex + 1; } //按下键盘 ↑键 选择索引向上移⼀位 else if (e == && m_ > 0 && m_edIndex != 0) { m_edIndex = m_edIndex - 1; } //按下键盘 Enter键 选中项赋值 else if (e == && m_ > 0 && m_edIndex != -1) { //输⼊框内容赋值为下拉框中选中的项 tx_ = m_ng(); ////下拉框收起 xDispose(); } } } private void QueryBox_Load(object sender, EventArgs e) { Back(); } /// /// 收起(清空)下拉框 /// private void ListBoxDispose() { if (this.m_ListBox != null) { (m_ListBox); this.m_ListBox = null; } } /// /// 展开下拉框 /// private void ListBoxInit(List items) { //实例化下拉框 m_ListBox = new ListBox(); m_on = new Point(0, M_); m_ = new Size(M_, 100); m_lick += new MouseEventHandler(m_ListBox_MouseClick); //添加数据 m_ge(y()); (m_ListBox); //使下拉框覆盖其他控件 oFront(); } /// /// ⿏标点击下拉框中的项时 /// /// /// private void m_ListBox_MouseClick(object sender, MouseEventArgs e) { if ( == ) { if (!OrEmpty(m_ng())) { M_QueryText = m_ng(); } } xDispose(); } }}View Codenamespace MyControl{ partial class UC_QueryBox { ///

/// 必需的设计器变量。 ///

private iner components = null; ///

/// 清理所有正在使⽤的资源。 ///

/// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { e(); } e(disposing); } #region 组件设计器⽣成的代码 ///

/// 设计器⽀持所需的⽅法 - 不要 /// 使⽤代码编辑器修改此⽅法的内容。 ///

private void InitializeComponent() { _context = new x(); dLayout(); //

// tx_context //

_ = ; _on = new (0, 0); _ = new g(0); _ = "tx_context"; _ = new (150, 21); _ex = 0; _anged += new andler(_context_TextChanged); _n += new ntHandler(_context_KeyDown); //

// UC_QueryBox //

aleDimensions = new (6F, 12F); aleMode = ; ze = true; lor = arent; oundImageLayout = h; (_context); Buffered = true; = new g(0); = "UC_QueryBox"; = new (150, 22); += new andler(ox_Load); Layout(false); mLayout(); } #endregion private x tx_context; }}以下是测试项⽬代码:View Codeusing System;using c;using entModel;using ;using g;using ;using ;namespace Test{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //绑定委托 uC_a = new _elegate(GetData); } private List GetData(string p_keyWord)

{ //这⾥写具体的数据获取⽅法 return new List() { "123", "234" }; } }}View Codenamespace Test{ partial class Form1 { ///

/// 必需的设计器变量。 /// private iner components = null; /// /// 清理所有正在使⽤的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { e(); } e(disposing); } #region Windows 窗体设计器⽣成的代码 /// /// 设计器⽀持所需的⽅法 - 不要 /// 使⽤代码编辑器修改此⽅法的内容。 /// private void InitializeComponent() { _QueryBox1 = new _QueryBox(); dLayout(); //

// uC_QueryBox1 //

_ze = true; _lor = arent; _oundImageLayout = h; _on = new (64, 77); _QueryBox1.M_ListBox = null; _QueryBox1.M_QueryText = ""; _ = new g(0); _ = "uC_QueryBox1"; _ = new (150, 22); _ex = 0; //

// Form1 //

aleDimensions = new (6F, 12F); aleMode = ; Size = new (292, 266); (_QueryBox1); = "Form1"; = "Form1"; += new andler(1_Load); Layout(false); mLayout(); } #endregion private _QueryBox uC_QueryBox1; }}以上代码的不⾜之处是,当此控件在容器边缘时下拉框显⽰不全,如设置容器AutoSize为True效果也不理想。

发布者:admin,转转请注明出处:http://www.yc00.com/news/1688892352a181719.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信