none
How do I get email value from an object in listbox? RRS feed

  • Question

  • I have a listbox which displays first and last name. Each item in the listbox is an object. That means even though I am displaying names (because it is easier for user to look at a list of names and not emails), it is possible to get email from each selected name (object). I do not know how to do this.  
    My question is, how do I get the email associated with the selected name (item in the listbox)?


    What I have tried:
    The problem with my code is that ListItem.ToString()) holds a name that is displayed and not email. I want to get the email inside the object.
    For example Joe Doe is selected name (ListItem) in the listbox. The email of Joe Doe is inside the object. In debug when I hover over the staffEmails there are 16 objects.  When I copy Expression, Joe Doe email is here 

    (new System.Collections.Generic.Mscorlib_CollectionDebugView<FTACaseReset.Models.GetRequestorInfoModel>(staffEmails).Items[1]).Email

    Here is how I am sending the email. However ListItem shows the name and not email. I want to use email but I am not sure how to do that because Listbox is populated with a list of names. However each item in the listbox is an object. Inside the object, there is email which is what I need to get. 

    mailMessage.To.Add(to);
    List<GetRequestorInfoModel> staffEmails = new List<GetRequestorInfoModel>();
    staffEmails = await FTACaseReset.Controllers.RequestorInfoController.GetAllRequestorInfoes();
    ListBoxItem staffEmail = new ListBoxItem();
    staffEmail.Text = staffEmails[0].Email;
    foreach (var ListItem in MyListBox.SelectedItems)
    {
    MailAddress to = new MailAddress(ListItem.ToString());
    }

    Here is how I am populating the listbox

    public async void PopulateAdditionalStaffEmailListBox()
    {
        List<getrequestorinfomodel> staffEmails = new List<getrequestorinfomodel>();
        try
        {
            staffEmails = await FTACaseReset.Controllers.RequestorInfoController.GetAllRequestorInfoes();
            staffEmails = staffEmails.OrderBy(x => x.DisplayName).ToList();
            for (int i = 0; i < staffEmails.Count; i++)
            {
                ListBoxItem staffEmail = new ListBoxItem();
                staffEmail.Text = staffEmails[i].DisplayName;
                staffEmail.Text = staffEmails[i].Email;
                AdditionalStaffEmailListBox.Items.Add(staffEmail.Text).ToString();
            }
        }
    }



     
    • Edited by winkimjr2 Thursday, August 22, 2019 3:06 PM Explained I want to get email associated with selected name in the listbox
    Thursday, August 22, 2019 3:01 PM