C#窗口应用程序,搜索列表框 [英] C# window application, searching a listbox
问题描述
3
down vote
favorite
2
我在listBox中有大量项目,名为listBox1。我的顶部还有一个textBox(textBox1)。我想能够输入textBox并且listBox搜索它的项目并查找包含我正在输入的内容。
例如,说listBox包含
I have a large amount of items in a listBox called listBox1. I also have a textBox (textBox1) at the top. I want to be able to type into the textBox and the listBox searches through its items and finds ones that contain what I am typing.
For example, say the listBox contains
Cat
Dot
Carrot
and Barcolli
如果我开始输入字母 C
,那么我希望它显示 Cat
和 Carrot
,当我输入a时,它应该继续显示它们,但当我添加 r
时,它应该删除<$ c列表中的$ c> Cat 。无论如何都要这样做吗?
i从数据库获取列表框然后当我们根据文本框中显示的文本项目在文本框中输入文本时
我尝试了什么:
If I start typing the letter C
, then I want it to show both Cat
and Carrot
, when i type a it should keep showing them both, but when I add an r
it should remove Cat
from the list. Is there anyway to do this?
i am fetching listbox from databse and then when we type text in textbox according to that text item show in textbox
What I have tried:
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Edit.connectionname());
string query = "select MEDICINE from tinchure";
SqlCommand cmd = new SqlCommand(query, con);
DataTable data = new DataTable();
SqlDataReader dbr;
try
{
con.Open();
dbr = cmd.ExecuteReader();
while (dbr.Read())
{
string sname = (string)dbr["MEDICINE"]; //name is coming from database
listBox1.Items.Add(sname);
string[] array = new string[listBox1.Items.Count];
for (int i = 0; i < listBox1.Items.Count; i++)
{
object s = listBox1.Items[i];
array[i] = s.ToString();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
foreach (string str in )
{
if (str.StartsWith(textBox5.Text, StringComparison.CurrentCultureIgnoreCase))
{
listBox1.Items.Add(str);
}
}
}
推荐答案
从这里开始:在C#中键入时搜索 [ ^ ] 。这可能有助于您找到在WinForm中实现搜索未来的方法。
尝试!
Start here: Search As You Type in C#[^]. This might help you in finding a way to implement search future in your WinForm.
Try!
假设您想在winform中实现此目的。
这是一个自动建议文本框,请看看,希望这会对你有所帮助
自动提示文本框
[ ^ ]
Assuming you want to achieve this in winform.
here is an auto suggest textbox, please have a look, hope this will help you
autosuggest textbox
[^]
这篇关于C#窗口应用程序,搜索列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!