[英]How do I iterate through all the items in a listbox
所以我有一個帶有ObservableCollection的ItemSource的列表框讓我們說我在列表框中添加了2個項目,然后ObservableCollection包含2個字符串嗎?
現在..我正在嘗試創建一個包含字符串中的whatevers的消息框彈出窗口。 所以,假設我有一個包含2個項目的列表框,Cat&Bob。
現在,當我按下I按鈕時,我希望它提示我兩個消息框,一個說Cat和一個說bob。 我嘗試做一個foreach語句但是當我按下按鈕時它不想執行。 現在.. ObservableCollection在1個窗口中,我按下的按鈕在另一個窗口中,所以我不確定這是否有所作為。
所以我所做的是我宣布另一個窗口位於這個頂部,就像這樣......
People peps = new People();
foreach(string email in peps.recipients)
{
if (comboBox1.Text == "Email")
{
MessageBox.Show(peps.Listbox1.Items.ToString());
MessageBox.Show(peps.Listbox1.Items.ToString());
}
}
但它不打印出任何東西。
如何使消息框顯示Listbox1中的任何內容
如果沒有真正看到你的People
類,或許多其他東西,並且魯莽地假設你想要完成什么,我會假設你的People
類看起來像這樣:
class People
{
public string m_sName {get; set; }
public string m_sEmail {get; set; }
public People(){}
public People(string name = null, string email = null)
{
m_sName = name;
m_sEmail = email;
}
然后在你填滿ListBox
,你想要瀏覽內容,並在MessageBox
顯示它們?....
List<people> lstPeeps = new List<people>();
foreach (var item in ListBox1.Items)
{
if (item.Contains("@")) //This checks to see if it's an email address...
lstPeeps.Add(item as peep.m_sEmail);
else lstPeeps.Add(item as peep.m_sName);
foreach (var peep in lstPeeps)
MessageBox.Show(peep.name + "\t" + peep.email);
也許是這樣的......如果這不起作用,請多給我們一些工作,我會相應地修改答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.