C# 如何在列表框中查找项目?

C# 如何在列表框中查找项目?,c#,visual-studio-2012,listbox,C#,Visual Studio 2012,Listbox,我正在创建一个应用程序,其中有一个列表框,其中包含使用StreamReader从文本文件读取的项目。我已经创建了一个搜索表单,但我不确定下一步要做什么。谁能给我一些建议吗?这是我的密码: 我的列表框的代码(对不起,太长了) 如果无法使用列表和lambda,则不要再继续 在这里,我使用一个列表作为列表框和模拟数据的数据源,因为数据来自何处并不重要,但在这里,我重点搜索一个类中的属性,在该类中,select用于索引项,然后where搜索(在本例中为where搜索)特定项,如果找到,则使用索引移动到该

我正在创建一个应用程序,其中有一个
列表框
,其中包含使用
StreamReader
从文本文件读取的项目。我已经创建了一个搜索表单,但我不确定下一步要做什么。谁能给我一些建议吗?这是我的密码:

我的
列表框的代码(对不起,太长了)

如果无法使用
列表
和lambda,则不要再继续

在这里,我使用一个
列表作为列表框和模拟数据的数据源,因为数据来自何处并不重要,但在这里,我重点搜索一个类中的属性,在该类中,select用于索引项,然后where搜索(在本例中为where搜索)特定项,如果找到,则使用索引移动到该项。如果没有找到,则不存在代码,因为如果这里的代码对您来说是可行的,那么这对您来说很容易做到

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

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

        private void Button1_Click(object sender, EventArgs e)
        {
            var results =
                ((List<Item>)ListBox1.DataSource)
                    .Select((data, index) => new 
                    { Text = data.SerialNumber, Index = index })
                    .Where((data) => data.Text == "BB1").FirstOrDefault();
            if (results != null)
            {
                ListBox1.SelectedIndex = results.Index;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var items = new List<Item>() {
            new Item {Identifier = 1, SerialNumber = "AA1", Type = "A1"},
            new Item {Identifier = 2, SerialNumber = "BB1", Type = "A1"},
            new Item {Identifier = 3, SerialNumber = "CD12", Type = "XD1"}
        };
            ListBox1.DisplayMember = "DisplayText";
            ListBox1.DataSource = items;
        }
    }
    /// <summary>
    /// Should be in it's own class file
    /// but done here to keep code together
    /// </summary>
    public class Item
    {
        public string SerialNumber { get; set; }

        public string Type { get; set; }

        public int Identifier { get; set; }

        public string DisplayText
        {
            get
            {
                return SerialNumber + " " + this.Type;
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows.Forms;
名称空间ListBoxSearch\u cs
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
var结果=
((列表)ListBox1.DataSource)
.选择((数据、索引)=>新建
{Text=data.SerialNumber,Index=Index})
.Where((data)=>data.Text==“BB1”).FirstOrDefault();
如果(结果!=null)
{
ListBox1.SelectedIndex=results.Index;
}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
var items=新列表(){
新项{Identifier=1,SerialNumber=“AA1”,Type=“A1”},
新项{Identifier=2,SerialNumber=“BB1”,Type=“A1”},
新项目{Identifier=3,SerialNumber=“CD12”,Type=“XD1”}
};
ListBox1.DisplayMember=“DisplayText”;
ListBox1.DataSource=项目;
}
}
/// 
///应该在它自己的类文件中
///但在这里做是为了让代码保持一致
/// 
公共类项目
{
公共字符串序列号{get;set;}
公共字符串类型{get;set;}
公共int标识符{get;set;}
公共字符串显示文本
{
得到
{
返回SerialNumber+“”+this.Type;
}
}
}
}

您只发布了有效的代码。尝试一些方法,只发布与问题相关的代码。试一试。我真的不知道从哪里开始,所以我只需要指向正确的方向,这样我就可以尝试一些东西查看
ListBox.DataSource
DisplayMember
。对于搜索功能,最简单的解决方案是一个文本字段和一个按钮来启动对项目的一些过滤。关于如何在
列表框中查找项目以及如何筛选项目,已经有很多例子了。谢谢,我会看一看,试着看一下,但似乎没有任何帮助。仍然不知道该怎么做-我是这方面的初学者,所以我真的很努力给属性命名为“类型”可以吗?@3这只是一个例子,请随意命名,谢谢你的帮助
private void btnSearch_Click(object sender, EventArgs e)
    {
        frmSearchSwitch tempSearchSwitch = new frmSearchSwitch();
        tempSearchSwitch.Show();
        frmkeepSwitches.Hide();
    } 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

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

        private void Button1_Click(object sender, EventArgs e)
        {
            var results =
                ((List<Item>)ListBox1.DataSource)
                    .Select((data, index) => new 
                    { Text = data.SerialNumber, Index = index })
                    .Where((data) => data.Text == "BB1").FirstOrDefault();
            if (results != null)
            {
                ListBox1.SelectedIndex = results.Index;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var items = new List<Item>() {
            new Item {Identifier = 1, SerialNumber = "AA1", Type = "A1"},
            new Item {Identifier = 2, SerialNumber = "BB1", Type = "A1"},
            new Item {Identifier = 3, SerialNumber = "CD12", Type = "XD1"}
        };
            ListBox1.DisplayMember = "DisplayText";
            ListBox1.DataSource = items;
        }
    }
    /// <summary>
    /// Should be in it's own class file
    /// but done here to keep code together
    /// </summary>
    public class Item
    {
        public string SerialNumber { get; set; }

        public string Type { get; set; }

        public int Identifier { get; set; }

        public string DisplayText
        {
            get
            {
                return SerialNumber + " " + this.Type;
            }
        }
    }
}