1
votes

So I have this listbox with the ItemSource of a ObservableCollection And let's say I add 2 items to the listbox then the ObservableCollection contains 2 strings right?

Now.. I'm trying to create a messagebox popup containing whatevers in the string. So let's say I have a listbox with 2 items, Cat & Bob.

Now when I press I button I want it to prompt me with two messageBoxes, one saying Cat & one saying bob. I tried doing a foreach statement but it doesnt want to execute when I press the button. Now.. The ObservableCollection is in 1 Window and the button I press is in another window so im not sure if that makes a difference.

So what I did is I declared the other window at the top of this one like so..

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

But it's not printing out anything.

How do I make the messageboxes show whatever is in the Listbox1

1
Is this literally the entire code or did you omit something? When is peps.recipients loaded? - Camilo Terevinto
"let's say I add 2 items to the listbox then the ObservableCollection contains 2 strings right?" No, please let's not say that, or even think it. To add items, add them to the ObservableCollection. Iterate over the items in the ObservableCollection. If you want to remove items, remove them from the observable collection. The ListBox just shows the user what's in the ObservableCollection, that's all it's there for. - 15ee8f99-57ff-4f92-890c-b56153
"doesnt want to execute when I press the button": How did you infer its desires? - 15ee8f99-57ff-4f92-890c-b56153
You should iterate also through Listbox1.Items - Artur Siwiak

1 Answers

0
votes

Without actually seeing your People class, or a good many other things, and recklessly making assumptions as to what you're trying to accomplish, I'm going to assume that your People class looks something like this:

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

Then after you've filled up your ListBox, you want to go through the contents, and display them in a 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);

Maybe something like that... If that doesn't work, give us a little more to work with and I'll modify the answer accordingly.