[英]How can I properly retrieve data from a ListBox of Objects and Filter it using a ComboBox with Lambda Expressions?
我正在開發一個Windows Form項目,其中有一個名為Client的類,該類具有其屬性和靜態列表。 我還有一個表格,您可以在其中注冊101%的新客戶,因此我不會在此處發布其代碼。 每次注冊客戶端時,都會將其添加到靜態列表中。
問題出在我要列出客戶的表格中。 此表單具有一個列表框 ,該列表框應向您顯示已注冊客戶端的所有ID ,一個“檢查按鈕”,用於顯示所選客戶端的所有信息(列表框中的選定ID),該標簽旁邊有一組標簽。 在上述所有內容下,我都有一個ComboBox ,其中有2個選項: Male和Female 。 此組合框用於過濾僅具有所選性別的ID。
我在列出和過濾客戶端方面遇到很多問題:
1-我的第一個問題是當我嘗試將所有注冊的客戶端添加到ListBox時,我首先嘗試使用不同的Lambda表達式 (例如:Lista.Items.Add(Client.clients.Find(i => i.Id = = i.Id).Id),但我做不到,它部分地處理了一個表達式,該表達式一遍又一遍地填充了具有相同ID的ListBox,然后經過大量研究並閱讀了許多不同的問題后,我使用了DataSource ListBox的屬性將“客戶”列表與ListBox“綁定”在一起,但是我不得不更改這種方式,因為在過濾數據(更新數據源)時無法按需要更新 ListBox。
所以之后我更改了它,而是使用了一個foreach循環來填充ListBox。
當我使用DataSource而不是foreach填充ListBox時,檢查客戶端信息並將其顯示在Form中時,我沒有任何問題,但是當我使用foreach循環填充ListBox時(兩種方法都應我以相同的方式填充ListBox)我無法使用相同的方法(在private void button1_Click(object sender, EventArgs e)
方法中)顯示Client的信息,並且得到“對象引用未設置為實例對象”錯誤。 為什么會這樣? 我怎樣才能解決這個問題?
2-另一個大問題是過濾ID時,如下面的代碼所示,我嘗試用數據源屬性填充ListBox,並使用帶有已過濾ID的foreach填充ListBox,但是我無法通過Lambda表達式來過濾ID或填充或從列表中刪除對象。
在這種情況下,填寫列表的最佳方法是什么? 如果我想使用Lambda表達式將靜態列表中注冊的ID填充到ListBox中,這不會導致我在組合框中過濾ID時出現問題,應該怎么辦? (我只想了解這一點)。
請閱讀代碼及其注釋以進一步了解。
還請檢查圖像
class Client
{
private string id;
private string name;
private byte idSex; // 1= Male 2= Female
public string Name
{
get {return name; }
set {this.name= value; }
}
public string Id
{
get { return this.id; }
set { this.id= value; }
}
public byte IdSex
{
get { return this.idSex; }
set { this.idSex = value; }
}
public static List<Client> clients = new List<Client>(); //Clients are added here
public Client(string id, string name, byte idSex)
{
this.id=id;
this.name=name;
this.idSex=idSex;
}
}
public partial class ListadoClientes : Form //ignore the name, I translated everything to English because I speak Spanish
{
public ListadoClientes()
{
InitializeComponent();
List<string> sexos = new List<string>(){ "Male", "Female" };
Lista.Items.Clear();//NOTE: 'Lista' is the name of the ListBox*****
/*Lista.DataSource = Client.clients; commented because I couldn't update the listbox when I wanted to filter the data and fill the listbox again but with IDs of only one sex*/
foreach(Client c in Client.clients)
{
Lista.Items.Add(c);
}
Lista.DisplayMember = "Id";
Lista.ValueMember = "Id";
foreach(string s in sexos)//adding the gender to the combobox
{
ComboSexo.Items.Add(s); //ComboSexo is the name of the Combobox
/* string y = Lista.GetItemText(Lista.TopIndex+1);
Lista.Items.Add(Client.clients.Find(i => i.Id!= y).Id); >>>>HERE I TRIED TO USE A LAMBDA EXPRESSION, USED OTHERS TOO*/
}
}
private void ListadoClientes_Load(object sender, EventArgs e)
{
//not used
}
private void Lista_SelectedIndexChanged(object sender, EventArgs e)
{
//not used
}
private void button1_Click(object sender, EventArgs e)
{
//THIS CODE WORKS FOR DISPLAYING INFO IN THE LABELS WHEN USING THE DATASOURCE PROPERTY FOR FILLING THE LISTBOX BUT IT DOESN'T WORK WITH THE FOREACH WAY.
string sex;// 1= Male 2= Female
//code below to save the sex of the client to a string value depending on its id
if (Cliente.clientes.Find(i => Lista.SelectedValue == i.Id).IdSex == 1)
{
sex = "Male";
}
else
{
sex = "Female";
}
LblId.Text = Client.clients.Find(i => Lista.SelectedValue == i.Id).Id;
LblName.Text = Client.clients.Find(i => Lista.SelectedValue == i.Id).Name;
LblSex.Text = sex;
}
private void ComboSexo_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void BtnFiltrarSexo_Click(object sender, EventArgs e)
{
if (ComboSexo.Text == "Male")
{
/*Lista.Items.Clear();
Lista.DataSource = Client.clients.Find(i=> i.IdSex==2);
Lista.DisplayMember = "Id";
Lista.ValueMember = "Id";
Tried to do this at first when I had the listbox filled with the DataSource*/
/*int y=Client.clients.Count;
for(int i=0; i<y;i++)
{
if ()
{
Lista.Items.Remove(c);
>>>did not work<<<
}*/
/*Lista.Items.Remove(Client.clients.Find(i=> i.IdSex!=1 )); I also tried to Remove using a Lambda Expression but this worked only removing 1 Client!! then I tried using a for loop or a foreach but I couldn't do it.
}
else if(ComboSexo.SelectedText == "Female")
{
Lista.Items.Clear();
Lista.DataSource = Client.clients.Find(i=> i.IdSex==2);
Lista.DisplayMember = "Id";
Lista.ValueMember = "Id";
//this won't work also
}
else
{
MessageBox.Show("Error you must select a sex");
}*/
}
}
}
PS:列表中的ID顯示為:123、345、864等,因此,如果按性別進行過濾,假設只有ID 345是男性,則在單擊“過濾器按鈕”后,列表框將僅顯示編號345。
填補所有客戶
Lista.Items.Clear();
Lista.Items.AddRange(Client.clients.ToArray());
只填充男性
Lista.Items.Clear();
Lista.Items.AddRange(Client.clients.Where(x => x.IdSex == 1).Select(x => x).ToArray());
只填女性
Lista.Items.Clear();
Lista.Items.AddRange(Client.clients.Where(x => x.IdSex == 2).Select(x => x).ToArray());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.