combobox 如何列表检索?

soyulo 2010-06-04 06:21:34




输入w
combobox控件 下拉菜单自动打开 此时控件 显示 模糊查询数据库 表 的 地名拼音首字母 列 相符的 地名 列

同时保证 combobox控件可以正常输入w以后的参数



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public string 引用数据库连接 = System.Configuration.ConfigurationManager.ConnectionStrings["数据库连接"].ToString();

private void comboBox1_TextUpdate(object sender, EventArgs e)
{
if (comboBox1.Text == "")
{
}
else
{
string sqltext = "SELECT * FROM 地名 WHERE 地名拼音首字母 like '" + comboBox1.Text + "%'";
SqlConnection conn = new SqlConnection(引用数据库连接);

conn.Open();

SqlDataAdapter adr = new SqlDataAdapter(sqltext, conn);
DataSet das = new DataSet();
adr.Fill(das, "adr");

comboBox1.DataSource = das.Tables[0].DefaultView;
comboBox1.DisplayMember = "地名";
comboBox1.ValueMember = "";

conn.Close();
}
}
}

}


这个代码有以下几个问题我解决不了期望能得到大家的帮助

1,无法连续输入关键字,输入完w后文本框立马变成查询的第一条数据.没有办法输入第二个字母
2,输入关键字后,下拉菜单无法自动打开.
...全文
380 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Joetao 2010-06-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
combobox 自动匹配
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

否则使用textbox 结合listbox
[/Quote]
不错
noway8881 2010-06-04
  • 打赏
  • 举报
回复
上网搜索DropTextBox,就几百行代码。复杂啥啊。
soyulo 2010-06-04
  • 打赏
  • 举报
回复
哥哥啊这个太复杂了把!!!!
noway8881 2010-06-04
  • 打赏
  • 举报
回复
        private void textBox1_Enter(object sender, EventArgs e)
{
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.Down))
{
this.listBox1.Focus();
}
if ((e.KeyCode== Keys.Enter))
{
this.HideLs();
}
base.OnKeyDown(e);
}

private void textBox1_Leave(object sender, EventArgs e)
{
if (!this.listBox1.Focused)
{
this.HideLs();
}
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
this.txt = this.textBox1.Text;
if (!this.listBox1.Items.Contains(this.textBox1.Text))
{
this.OnTxTChanged(new TxtEvenargs(this.textBox1.Text));
}
}

private void UserControl1_Load(object sender, EventArgs e)
{
this.textBox1.Top = 0;
this.textBox1.Left = 0;
this.textBox1.Width = base.Width;
this.listBox1.Top = this.textBox1.Bottom;
this.textBox1.ForeColor = this.txtcolor;
this.listBox1.Font = this.dropfont;
this.listBox1.ForeColor = this.textBox1.ForeColor;
this.listBox1.HorizontalScrollbar = this.hscrollbar;
this.HideLs();
base.Height = this.textBox1.Height;
}

private void UserControl1_SizeChanged(object sender, EventArgs e)
{
this.textBox1.Top = 0;
this.textBox1.Left = 0;
this.textBox1.Width = base.Width;
this.listBox1.Top = this.textBox1.Bottom;
this.listBox1.Left = 0;
this.listBox1.Width = this.textBox1.Width;
if (base.Height < this.textBox1.Height)
{
base.Height = this.textBox1.Height;
}
this.listBox1.Height = base.Height - this.textBox1.Height;
}

// Properties
[Description("下拉框字体"), Category("自定义")]
public Font DropFont
{
get
{
return this.dropfont;
}
set
{
this.dropfont = value;
}
}

[Category("自定义"), Description("下拉框高度")]
public int DropHeight
{
get
{
return this.dropheight;
}
set
{
this.dropheight = value;
}
}

[Description("下拉框高度固定"), Category("自定义")]
public bool DropHeightFix
{
get
{
return this.dropheightfix;
}
set
{
this.dropheightfix = value;
}
}

[Category("自定义"), Description("下拉框显示的字符串数组")]
public string[] DropItems
{
get
{
return this.items;
}
set
{
this.items = value;
if (this.items.Length > 0)
{
this.ShowLs();
}
else
{
this.HideLs();
}
}
}

[Description("下拉框横向溢出显示滚动条"), Category("自定义")]
public bool HScrollbar
{
get
{
return this.hscrollbar;
}
set
{
this.hscrollbar = value;
}
}

[Description("下拉框显示最多选项数目"), Category("自定义")]
public int MaxItems
{
get
{
return this.maxitems;
}
set
{
this.maxitems = value;
}
}

[Description("文本框最小字符长度,不包括空格"), Category("自定义")]
public int Minletters
{
get
{
return this.minletters;
}
set
{
this.minletters = value;
}
}

public int SelectionLength
{
get
{
return this.selectionlength;
}
set
{
this.selectionlength = value;
if (this.selectionlength < 0)
{
this.selectionlength = 0;
}
this.textBox1.Select(this.selectionstart, this.selectionlength);
}
}

public int SelectionStart
{
get
{
return this.selectionstart;
}
set
{
this.selectionstart = value;
if (this.selectionstart < 0)
{
this.selectionstart = 0;
}
this.textBox1.Select(this.selectionstart, 0);
}
}

[Browsable(true), Category("自定义"), Description("文本框字符")]
public override string Text
{
get
{
return this.txt;
}
set
{
this.txt = value;
this.textBox1.Text = this.txt;
}
}

[Category("自定义"), Description("文本颜色")]
public Color TxtColor
{
get
{
return this.txtcolor;
}
set
{
this.txtcolor = value;
}
}

// Nested Types
public delegate void TxtChangedHandle(object sender, TxtEvenargs e);
}

public class TxtEvenargs : EventArgs
{
// Fields
public string txt = "";

// Methods
public TxtEvenargs(string txt)
{
this.txt = txt;
}
}
noway8881 2010-06-04
  • 打赏
  • 举报
回复
[DefaultEvent("TxtChanged")]
public class DropTextBox : System.Windows.Forms.UserControl
{
// Fields
private IContainer components;
private Font dropfont = new Font("宋体", 9f);
private int dropheight = 100;
private bool dropheightfix;
private bool hscrollbar;
private string[] items = new string[0];
private System.Windows.Forms.ListBox listBox1;
private int maxitems = 10;
private int minletters = 2;
private int selectionlength;
private int selectionstart;
private bool showstate = true;
private System.Windows.Forms.TextBox textBox1;
private string txt = "";
private Color txtcolor = Color.Black;

// Events
public event TxtChangedHandle TxtChanged;

// Methods
public DropTextBox()
{
this.InitializeComponent();
}

protected override void Dispose(bool disposing)
{
if (disposing && (this.components != null))
{
this.components.Dispose();
}
base.Dispose(disposing);
}

private void FillListbox()
{
if (!this.listBox1.Items.Contains(this.textBox1.Text))
{
this.listBox1.Items.Clear();
if (this.items.Length >= 1)
{
int maxitems = this.maxitems;
if (maxitems > this.items.Length)
{
maxitems = this.items.Length;
}
for (int i = 0; i < maxitems; i++)
{
this.listBox1.Items.Add(this.items[i]);
}
for (int j = 0; j < this.listBox1.Items.Count; j++)
{
if (this.listBox1.Items[j].ToString() == this.textBox1.Text)
{
this.listBox1.SelectedIndex = j;
return;
}
}
}
}
}

private void HideLs()
{
if (this.showstate)
{
this.listBox1.Items.Clear();
this.listBox1.Height = 0;
base.Height = this.textBox1.Height;
this.listBox1.Visible = false;
this.showstate = false;
}
}

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.listBox1 = new System.Windows.Forms.ListBox();
base.SuspendLayout();
this.textBox1.Location = new Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new Size(150, 0x15);
this.textBox1.TabIndex = 0;
this.textBox1.Enter += new EventHandler(this.textBox1_Enter);
this.textBox1.Leave += new EventHandler(this.textBox1_Leave);
this.textBox1.TextChanged += new EventHandler(this.textBox1_TextChanged);
this.textBox1.KeyDown += new KeyEventHandler(this.textBox1_KeyDown);
this.listBox1.BorderStyle = BorderStyle.FixedSingle;
this.listBox1.Cursor = Cursors.Default;
this.listBox1.Font = new Font("宋体", 9f);
this.listBox1.FormattingEnabled = true;
this.listBox1.HorizontalScrollbar = true;
this.listBox1.ItemHeight = 12;
this.listBox1.Items.AddRange(new object[] { "abc", "123", "我们", "他们" });
this.listBox1.Location = new Point(0, 0x15);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new Size(150, 0x4a);
this.listBox1.TabIndex = 1;
this.listBox1.Leave += new EventHandler(this.listBox1_Leave);
this.listBox1.DoubleClick += new EventHandler(this.listBox1_DoubleClick);
this.listBox1.Enter += new EventHandler(this.listBox1_Enter);
this.listBox1.SelectedIndexChanged += new EventHandler(this.listBox1_SelectedIndexChanged);
this.listBox1.KeyDown += new KeyEventHandler(this.listBox1_KeyDown);
base.AutoScaleDimensions = new SizeF(6f, 12f);
base.AutoScaleMode = AutoScaleMode.Font;
base.Controls.Add(this.listBox1);
base.Controls.Add(this.textBox1);
this.DoubleBuffered = true;
base.Name = "DropTextBox";
base.Size = new Size(0x9c, 0x5f);
base.Load += new EventHandler(this.UserControl1_Load);
base.SizeChanged += new EventHandler(this.UserControl1_SizeChanged);
base.ResumeLayout(false);
base.PerformLayout();
}

private void listBox1_DoubleClick(object sender, EventArgs e)
{
this.HideLs();
}

private void listBox1_Enter(object sender, EventArgs e)
{
if (this.listBox1.Items.Count > 0)
{
for (int i = 0; i < this.listBox1.Items.Count; i++)
{
if (this.listBox1.Items[i].ToString() == this.textBox1.Text)
{
this.listBox1.SelectedIndex = i;
return;
}
}
this.listBox1.SelectedIndex = 0;
}
}

private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
if (((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.Right)) || (e.KeyCode == Keys.Back))
{
this.textBox1.Focus();
this.textBox1.Select(this.textBox1.Text.Length, 0);
this.listBox1.SelectedIndex = -1;
}
if (e.KeyCode == Keys.Return)
{
this.HideLs();
}
this.OnKeyDown(e);
}

private void listBox1_Leave(object sender, EventArgs e)
{
if (!this.textBox1.Focused)
{
this.HideLs();
}
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.listBox1.SelectedIndex > -1)
{
this.textBox1.Text = this.listBox1.Text;
}
}

protected virtual void OnTxTChanged(TxtEvenargs e)
{
if (this.TxtChanged != null)
{
this.TxtChanged(this, e);
}
}

public void SelectAll()
{
this.textBox1.Focus();
this.textBox1.SelectAll();
}

private void ShowLs()
{
if (this.textBox1.Text.Trim().Length >= this.minletters)
{
this.FillListbox();
if (this.dropheightfix)
{
this.listBox1.Height = this.dropheight;
}
else
{
int num = this.listBox1.Items.Count * (this.listBox1.ItemHeight + 2);
if (num < this.dropheight)
{
this.listBox1.Height = num;
}
else
{
this.listBox1.Height = this.dropheight;
}
}
if (!this.showstate && (this.items.Length >= 1))
{
this.listBox1.Visible = true;
this.listBox1.BringToFront();
base.Height = this.textBox1.Height + this.listBox1.Height;
this.showstate = true;
}
}
}
[/code]
soyulo 2010-06-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
combobox 自动匹配
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

否则使用textbox 结合listbox
[/Quote]

textbox listbox 结合怎么写?
有代码吗?
leon9090 2010-06-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
combobox 自动匹配
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

否则使用textbox 结合listbox
[/Quote]
mark
hai_yang_09 2010-06-04
  • 打赏
  • 举报
回复
xue xi
birdlonger 2010-06-04
  • 打赏
  • 举报
回复
mark !
记得以前作过个例子,类似百度搜索提示的。回去看下还在不.
用textbox listbox 很好的选择.
wuyq11 2010-06-04
  • 打赏
  • 举报
回复
combobox 自动匹配
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

否则使用textbox 结合listbox
mngzilin 2010-06-04
  • 打赏
  • 举报
回复
你用textbox和listbox组合一个吧

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧