在列表框中查找并显示项目 [英] Find and show an item in listbox

查看:87
本文介绍了在列表框中查找并显示项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨所有

问题是我想用户在serch文本框中输入项目的名称,然后点击搜索按钮,当该项目查找显示时,告诉其他文本框中该名称的编号是我的代码

  private   void  btAdd_Click(  object  sender,EventArgs e)
{
tbTell.Text = tbTell.Text.ToString();

tbName.Text + = | + tbFamily.Text;
listnames.Items.Add(tbName.Text + | + tbTell.Text);
string number = tbTell.Text;
tbName.Text = ;
tbName.Focus();
}
private void btnSerch_Click( object sender,EventArgs e)
{

listnames.FindString(tbSerch.Text);


}





但此代码不适用于搜索

但是工作是为了在列表中的1项中添加姓氏和电话号码的名称



我尝试了什么:



i尝试使用查找字符串函数

试图为自己编写一个函数

但没有任何效果但

解决方案

试试这个,更正的代码和自定义搜索。



 private void btAdd_Click(object sender,EventArgs e)
{
string nameFamily = tbName.Text +|+ tbFamily.Text;
listnames.Items.Add(nameFamily +|+ tbTell.Text);
}

private void btnSerch_Click(object sender,EventArgs e)
{
bool isExactNameSearch = true;
string search = tbSerch.Text.Trim();
foreach(listnames.Items中的字符串项)
{
var parts = item.Split('|');
string name = parts [0] .Trim();
string number = parts [2];
if(isExactNameSearch)
{

if(name == search)//检查确切名称
{
MessageBox.Show(number);
休息;
}

}
else {
//包含搜索
if(name.Contains(search))//检查确切名称
{
MessageBox.Show(number);
休息;
}

}
}


}


根据 ListBox.FindString方法(String)(System.Windows) .Forms) [ ^ ]:

Quote:

在ListBox中查找指定的第一项string。



你的ListBox如下所示:

 listnames.Items.Add(tbName.Text +   | + tbTell.Text); 



您对搜索结果一无所知:< pre lang =c#> listnames.FindString(tbSerch.Text);



这是一个Google搜索,其中包含如何执行所需操作的示例: winform listbox text search [ ^


hi all
question is i want to user put name of the item in serch textbox and then click on search button and when that item finded show tell number of that name in an other textbox here is my code

private void btAdd_Click(object sender, EventArgs e)
{
    tbTell.Text = tbTell.Text.ToString();

    tbName.Text += " | " + tbFamily.Text;
    listnames.Items.Add(tbName.Text+" | "+tbTell.Text);
    string number = tbTell.Text;
    tbName.Text = "";
    tbName.Focus();
}
private void btnSerch_Click(object sender, EventArgs e)
{

    listnames.FindString(tbSerch.Text);


}



but this code is not working for search
but working for add a name with last name and phone number in 1 item in the list

What I have tried:

i tried to use find string function
tried to write a function for myself
but nothing worked yet

解决方案

try this, corrected code and custom search.

private void btAdd_Click(object sender, EventArgs e)
       {
           string nameFamily = tbName.Text + " | " + tbFamily.Text;
           listnames.Items.Add(nameFamily + " | " + tbTell.Text);
       }

       private void btnSerch_Click(object sender, EventArgs e)
       {
           bool isExactNameSearch = true;
           string search  = tbSerch.Text.Trim();
           foreach (string item in listnames.Items)
           {
               var parts = item.Split('|');
               string name = parts[0].Trim();
               string number = parts[2];
               if (isExactNameSearch)
               {

                   if (name == search) // checks exact name
                   {
                       MessageBox.Show(number);
                       break;
                   }

               }
               else {
                   // contains search
                   if (name.Contains(search)) // checks exact name
                   {
                       MessageBox.Show(number);
                       break;
                   }

               }
           }


       }


According to ListBox.FindString Method (String) (System.Windows.Forms)[^]:

Quote:

Finds the first item in the ListBox that starts with the specified string.


Your ListBox looks like this:

listnames.Items.Add(tbName.Text+" | "+tbTell.Text);


And you are doing nothing with your search results:

listnames.FindString(tbSerch.Text);


Here is a Google Search with examples of how to do what you want: winform listbox text search[^]


这篇关于在列表框中查找并显示项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆