2013-05-05 109 views
0

我有一个列表从对象列表

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); 
    } 
} 

当用户双击在列表框的人时,第二种编辑形式显示该人的详细信息。因此,如果鲍勃史密斯被点击,第二种形式将填写鲍勃史密斯作为名称,他的电话号码和他的地区。

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

回答

2

您需要绑定到列表。

属性添加到您的对象名为全名:

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将在缓存的销售人员的名单相同指数:

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

重写您的搜索过滤主要salesmanList dumpin g将结果写入“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 

} 
+0

“ValueMember”的+1。我只是现在想出了这个属性。 – Saravanan 2013-05-05 13:18:46

+0

为selectedValueChanged,文本框是另一种形式,所以我将如何填充它? – Pindo 2013-05-05 14:54:36

+0

您需要另一个名为“CurrentSalesman”的全局/静态(或其他缓存对象)。它将代表当前选定的推销员。使用从“listBox1_SelectedValueChanged”列表中选择的对象填充此缓存对象。 – 2013-05-05 15:08:13

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捕获选定的索引数据。