Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
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. When I hover over the staffEmails which has 16 objects, I find Joe Doe email by copying Expression. Here is the expression for Joe Doe email
C#
(new System.Collections.Generic.Mscorlib_CollectionDebugView<FTACaseReset.Models.GetRequestorInfoModel>(staffEmails).Items[1]).Email


C#
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

C#
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;
                staffEmail.Value = staffEmails[i].Email 
                AdditionalStaffEmailListBox.Items.Add(staffEmail.Text).ToString();
            }
        }
    }
Posted
Updated 23-Aug-19 7:48am
v5
Comments
Richard MacCutchan 22-Aug-19 11:26am    
Just get the object and extract the email property. Or rework the object so that it displays the email from its ToString method. And why are calling ToString on Text fields
Member 11403304 22-Aug-19 11:40am    
Hi Richard. I am not sure how to get the object and extract the email property. That is where I am stuck. I already have code that sends emails when I populate my listbox with emails. However, project owner told me to display names and not emails in the listbox for user to select. Each item in the listbox is an object which also contains email. But I do not know how to extract email from the object. This is why I included a lot of details in my question.
Maciej Los 22-Aug-19 11:44am    
No. ListBoxItem does not contain email object, because you did not set it in your code.
BTW: Use "Reply" widget if you want system to inform users about your replies.
Member 11403304 22-Aug-19 11:58am    
I forgot to update the code here. I did make a change in my code and set emails like this staffEmail.Value = staffEmails[i].Email. However I am MailAddress(ListItem.Value.ToString()) I am getting o'object does not contain a definition for 'Value' and no accessible extension method 'Value' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) when I use Value in MailAddress
Richard MacCutchan 22-Aug-19 12:54pm    
I suggest you read up on Classes and there member accessors. You cannot possibly be writing code such as the above unless you understand the basics of OOP.

1 solution

Quote:
I am stuck on how to send more than one email to all selected listbox name
In Windows Forms that would be this simple:
private void Button1_Click(object sender, EventArgs e)
{
    IEnumerable<Employee> selItems = listBox1.SelectedItems.Cast<Employee>();

    foreach (Employee emp in selItems)
    {
        SendEMail(emp.EMail);
    }
}

private void SendEMail(string empEMail)
{
}
The code in WPF should be very similar.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900