c# winform combobox问题

rogerlovewjh 2008-09-28 11:02:12
我需要实现象网页上一样,在下拉框中输入内容,那么combobox弹出的是从头开始匹配的相符数据。我这里需要的是只有相符合的数据出现在弹出框中,不符的不出现,有没有思路或者代码能够提供给小弟,小弟不胜感激!!!
我现在的代码是这样的

private void cbxUserName_KeyUp(object sender, KeyEventArgs e)
{
if (cbxUserName.Items.Count != 0)
{
for (int i = 0; i < cbxUserName.Items.Count; i++)
{
if (cbxUserName.Text.Trim().Length != 0)
{
if (cbxUserName.Items[i].ToString().Trim().StartsWith(cbxUserName.Text.Trim()))
{
cbxUserName.Text=cbxUserName.Items[i].ToString().Trim();
cbxUserName.DroppedDown = true;
}
}
}
}

}

此代码存在两个问题:
1.当匹配好后,弹出框中出现的是全部数据,而不是出现相匹配的数据。
2.出现弹出框后,鼠标没有了!
...全文
500 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
cooolchen 2008-09-30
  • 打赏
  • 举报
回复
好。
NowtAngell 2008-09-30
  • 打赏
  • 举报
回复
只有UP的份了...
luochaoc 2008-09-30
  • 打赏
  • 举报
回复
可看性很强!!
lussnailatnet 2008-09-30
  • 打赏
  • 举报
回复
还有鼠标的问题呢?
Anderslu 2008-09-29
  • 打赏
  • 举报
回复
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
gxingmin 2008-09-28
  • 打赏
  • 举报
回复
把上面三段代码复制并替换到一个窗体里即可
gxingmin 2008-09-28
  • 打赏
  • 举报
回复
[STAThread]
static void Main()
{
Application.Run(new frmAutoCompleteComboBox());
}

private void cbAutoComplete_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (this.chkAutoComplete.Checked)
this.AutoComplete(this.cbAutoComplete, e, this.chkAutoComplete.Checked);
}



// AutoComplete
public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e)
{
this.AutoComplete(cb, e, false);
}

public void AutoComplete(ComboBox cb, System.Windows.Forms.KeyPressEventArgs e, bool blnLimitToList)
{
string strFindStr = "";

if (e.KeyChar == (char)8)
{
if (cb.SelectionStart <= 1)
{
cb.Text = "";
return;
}

if (cb.SelectionLength == 0)
strFindStr = cb.Text.Substring(0, cb.Text.Length - 1);
else
strFindStr = cb.Text.Substring(0, cb.SelectionStart - 1);
}
else
{
if (cb.SelectionLength == 0)
strFindStr = cb.Text + e.KeyChar;
else
strFindStr = cb.Text.Substring(0, cb.SelectionStart) + e.KeyChar;
}

int intIdx = -1;

// Search the string in the ComboBox list.

intIdx = cb.FindString(strFindStr);

if (intIdx != -1)
{
cb.SelectedText = "";
cb.SelectedIndex = intIdx;
cb.SelectionStart = strFindStr.Length;
cb.SelectionLength = cb.Text.Length;
e.Handled = true;
}
else
{
e.Handled = blnLimitToList;
}

}


}
}
gxingmin 2008-09-28
  • 打赏
  • 举报
回复
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lblTest = new System.Windows.Forms.Label();
this.chkLimitToList = new System.Windows.Forms.CheckBox();
this.chkAutoComplete = new System.Windows.Forms.CheckBox();
this.cbAutoComplete = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// lblTest
//
this.lblTest.AutoSize = true;
this.lblTest.Location = new System.Drawing.Point(8, 151);
this.lblTest.Name = "lblTest";
this.lblTest.Size = new System.Drawing.Size(0, 16);
this.lblTest.TabIndex = 7;
//
// chkLimitToList
//
this.chkLimitToList.Checked = true;
this.chkLimitToList.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkLimitToList.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.chkLimitToList.Location = new System.Drawing.Point(208, 39);
this.chkLimitToList.Name = "chkLimitToList";
this.chkLimitToList.Size = new System.Drawing.Size(80, 16);
this.chkLimitToList.TabIndex = 2;
this.chkLimitToList.Text = "Limit to List";
//
// chkAutoComplete
//
this.chkAutoComplete.Checked = true;
this.chkAutoComplete.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkAutoComplete.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.chkAutoComplete.Location = new System.Drawing.Point(208, 15);
this.chkAutoComplete.Name = "chkAutoComplete";
this.chkAutoComplete.Size = new System.Drawing.Size(96, 16);
this.chkAutoComplete.TabIndex = 1;
this.chkAutoComplete.Text = "Auto Complete";
//
// cbAutoComplete
//
this.cbAutoComplete.Items.AddRange(new object[] {
"Aaron Alex",
"Baron Bud",
"Aarts Jan",
"Zicari Roberto"});
this.cbAutoComplete.Location = new System.Drawing.Point(16, 15);
this.cbAutoComplete.MaxDropDownItems = 10;
this.cbAutoComplete.Name = "cbAutoComplete";
this.cbAutoComplete.Size = new System.Drawing.Size(176, 21);
this.cbAutoComplete.TabIndex = 0;
this.cbAutoComplete.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.cbAutoComplete_KeyPress);
//
// frmAutoCompleteComboBox
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(312, 182);
this.Controls.Add(this.lblTest);
this.Controls.Add(this.chkLimitToList);
this.Controls.Add(this.chkAutoComplete);
this.Controls.Add(this.cbAutoComplete);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "frmAutoCompleteComboBox";
this.Text = "Auto Complete ComboBox - Example";
this.ResumeLayout(false);

}
#endregion
gxingmin 2008-09-28
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Auto_Complete_ComboBox
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmAutoCompleteComboBox : System.Windows.Forms.Form
{
internal System.Windows.Forms.Label lblTest;
internal System.Windows.Forms.CheckBox chkLimitToList;
internal System.Windows.Forms.CheckBox chkAutoComplete;
internal System.Windows.Forms.ComboBox cbAutoComplete;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public frmAutoCompleteComboBox()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
warrior 2008-09-28
  • 打赏
  • 举报
回复
看这里:http://www.cnblogs.com/frederick-liu/archive/2005/11/25/284547.html
brallow 2008-09-28
  • 打赏
  • 举报
回复
我的代码:
1:响应TextChanged事件
2:先备份所有的Items,在变化时先清空,再一一添加。

private List<object> itemsAll;

private void BackupAllItems()
{
if (itemsAll == null) itemsAll = new List<object>();

foreach (object o in comboBox1.Items)
{
itemsAll.Add(o);
}
}

private void comboBox1_TextChanged(object sender, EventArgs e)
{
if (itemsAll == null) BackupAllItems();


comboBox1.Items.Clear();
foreach (var i in itemsAll)
{
if (i.ToString().StartsWith(comboBox1.Text.Trim()))
{
comboBox1.Items.Add(i);
}
}
comboBox1.DroppedDown = true;
}
paulin 2008-09-28
  • 打赏
  • 举报
回复

comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox1.AutoCompleteSource = AutoCompleteSource.AllUrl;
warrior 2008-09-28
  • 打赏
  • 举报
回复
同上,设置AutoCompleteMode和AutoCompleteSource
rogerlovewjh 2008-09-28
  • 打赏
  • 举报
回复
各位能说详细点吗?我是刚刚学的,很多不是很懂
zt_100094 2008-09-28
  • 打赏
  • 举报
回复
ComboBox.AutoCompleteMode 属性
lee118 2008-09-28
  • 打赏
  • 举报
回复
ComboBox.AutoCompleteMode 属性
北京的雾霾天 2008-09-28
  • 打赏
  • 举报
回复
使用自动完成就可以了,给自动完成源设置你的数据源。
liheng133542 2008-09-28
  • 打赏
  • 举报
回复
dig
rogerlovewjh 2008-09-28
  • 打赏
  • 举报
回复
comboBox1.AutoCompleteSource = AutoCompleteSource.AllUrl; 不是AutoCompleteSource.AllUrl; 而是AutoCompleteSource.ListItems;这才是我要的!呵呵!
rogerlovewjh 2008-09-28
  • 打赏
  • 举报
回复

comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox1.AutoCompleteSource = AutoCompleteSource.AllUrl;
正解!!!多谢大家!结贴!!!
加载更多回复(11)

110,534

社区成员

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

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

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