从对象列表中查找对象的索引

时间:2013-05-05 12:41:01

标签: c# list listbox

我有一个清单

List<SalesmanDetails> salesmanList

推销员详细信息包括姓名,电话号码,地区。 我有两个窗体,一个有列表框,另一个允许用户通过更改文本框中的值来编辑信息。

列表框由“查看全部”按钮填充,该按钮显示列表中的所有销售人员或用户可以搜索名称(例如Bob Smith),列表中的所有Bob Smith将显示在列表框中

这是我查看全部的代码:

try
            {                    

                listBox1.Items.Clear();
                foreach (SalesmanDetails details in salesmanList)
                {
                    listBox1.Items.Add(String.Format("{0} {1}", details.firstName, details.surname));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

这是搜索名称的代码:

private void btnSearchName_Click(object sender, EventArgs e)
{
    try
    {
        foreach (SalesmanDetails search in salesmanList)
        {
            if (search.firstName.ToLower().Contains(searchName.ToLower()) | search.surname.ToLower().Contains(searchName.ToLower()))
            {
                listBox1.Items.Add(String.Format("{0} {1}", search.firstName, search.surname));                        
            }
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

当用户双击列表框中的某个人时,将显示第二个用于编辑的表单以及该人员的详细信息。因此,如果单击Bob Smith,则第二个表单将填充Bob Smith作为名称,电话号码和他的区域。

如何在搜索某人时填充编辑表单?

2 个答案:

答案 0 :(得分:2)

您需要绑定到列表。

向名为FullName的对象添加属性:

public string FullName { get { return String.Format("{0} {1}", search.firstName, search.surname); } }

然后绑定:

listBox1.DataSource = currentSalesmanList;  // filtered or all
listBox1.DisplayMember = "FullName"
listBox1.ValueMember = "SalesmanId";  // (optional) something to uniquely identify

然后处理SelectedValueChanged事件 - SelectedIndex将是您的Salesmen缓存列表中的相同索引:

private void listBox1_SelectedValueChanged(object sender, EventArgs e)
{
    if (listBox1.SelectedIndex != -1)
    {
        Salesman salesman = currentSalesmanList[listBox1.SelectedIndex];
        textBox1.Text = salesman.firstName;
    }
}

重写搜索以过滤主salesmanList,将结果转储到“currentSalesmanList”,然后绑定到它。

private void btnSearchName_Click(object sender, EventArgs e)
{
    currentSalesmanList = salesmanList.Where((s) => s.firstName.ToLower().Contains(searchName.ToLower()));

    listBox1.DataSource = currentSalesmanList;  // filtered or all
    listBox1.DisplayMember = "FullName"
    listBox1.ValueMember = "SalesmanId";  // (optional) something to uniquely identify

}

答案 1 :(得分:0)

做一招:

定义一个全局数组,该数组将保存已通过搜索过滤的人的索引。

在这个例子中

public List<Int32> myList = new List<Int32>();

然后在搜索功能中定义一个计数器。并改变它:

private void btnSearchName_Click(object sender, EventArgs e)
    {
        Int32 counter = 0;
        try
        {
            foreach (SalesmanDetails search in salesmanList)
            {
                if (search.firstName.ToLower().Contains(searchName.ToLower()) | search.surname.ToLower().Contains(searchName.ToLower()))
                {
                    listBox1.Items.Add(String.Format("{0} {1}", search.firstName, search.surname));
                    myList.Add(counter);
                }
                counter++;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

您将在myList中拥有已过滤人员的真实索引。

使用此列表,您可以通过sealsmanList捕获所选索引的数据。

相关问题