max
max

Reputation: 1

Can't get list box to display SelectedText only displays "system.windows.forms.listbox+selectedobjectcollection"

I need some help i can't figure out whats wrong with my code, everytime i try to get the program to display the SelectedItems i get "system.windows.forms.listbox+selectedobjectcollection". I can get text from the combobox to the emailBankListBox1 but when i try to hop from the emailBanklistBox1 to listBox1 i get the text listed above. Any and all help is much appreciated.

private void addButton1_Click(object sender, EventArgs e)
{
    /*
    int selectIndexInt = -1;

    selectIndexInt = recipientComboBox.SelectedIndex;

    if (selectIndexInt != -1)
    {
        emailBankListBox1.Items.Add(recipientComboBox.SelectedItem += ",");
    }

    else
    {

    }
    */
    if (recipientComboBox.Text == "")
    {
        MessageBox.Show("Please enter a Recipient Email");
    }
    else
    {
        emailBankListBox1.Items.Add(recipientComboBox.Text += ",");
    }
    for (int i = 0; i < emailBankListBox1.Items.Count; i++)
    {
        emailBankListBox1.SetSelected(i, true);
    }
}

private void removeButton2_Click(object sender, EventArgs e)
{
    int selectIndexInt = -1;

    selectIndexInt = emailBankListBox1.SelectedIndex;
    if (selectIndexInt != -1)
    {
        emailBankListBox1.Items.RemoveAt(selectIndexInt);
    }

    else
    {
        MessageBox.Show("Select a name from the 'Recipients to recieve email' List");
    }
}

private void button5_Click(object sender, EventArgs e)
{
    this.Close();
}

private void button4_Click(object sender, EventArgs e)
{

    /* 
    for (int i = 0; i < emailBankListBox1.Items.Count; i++)
    {
        emailBankListBox1.SetSelected(i, true);
    }
    */

    var selectedString = emailBankListBox1.SelectedItems.ToString();
    listBox1.Items.Add(selectedString);




    /*
    //Email sending
    MailMessage myMail = new MailMessage();

    myMail.To.Add(new MailAddress(emailBankListBox1.SelectedItems.ToString()));
    myMail.From = new MailAddress("******m", "******");
    myMail.Subject = subjectTextBox.Text;
    myMail.Body = bodyTextBox.Text;

    SmtpClient smtp = new SmtpClient("smtp.gmail.com");
    smtp.Port = 587;
    smtp.EnableSsl = true;
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.UseDefaultCredentials = false;
    smtp.Credentials = new NetworkCredential("*********", "*****");

    try
    {
        smtp.Send(myMail);
        MessageBox.Show("Message Sent");
    }   
    catch
    {
        MessageBox.Show("Cannot send this message");

    }
        */
}

private void recipientComboBox_SelectedValueChanged(object sender, EventArgs e)
{

    int selectIndexInt = -1;

    selectIndexInt = recipientComboBox.SelectedIndex;

    if (selectIndexInt != -1)
    {
        emailBankListBox1.Items.Add(recipientComboBox.SelectedItem += ",");
    }

    else
    {
        if (recipientComboBox.Text == "")
        {
            MessageBox.Show("Please type or select a recipient E-mail");
        }
    }
    for (int i = 0; i < emailBankListBox1.Items.Count; i++)
    {
        emailBankListBox1.SetSelected(i, true);
    }

}

Upvotes: 0

Views: 1510

Answers (2)

nawfal
nawfal

Reputation: 73183

You are essentially calling ToString on a collection. If you want to add each selected item to a separate listbox, you could do:

foreach(var item in emailBankListBox1.SelectedItems)
    listBox1.Items.Add(item.ToString());

If you Linq, you can directly do:

listBox1.Items.AddRange(emailBankListBox1.SelectedItems.OfType<string>());

If your objects inside emailBankListBox1 are not merely strings, then you will have to set the display member property in listbox1

Upvotes: 0

Prabhu Murthy
Prabhu Murthy

Reputation: 9261

SelectedItems is a collection and not an item.

You should be doing this

var selItem = emailBankListBox1.SelectedItem;
var selectedString=selItem.ToString();
var idx = emailBankListBox1.Items.IndexOf(selItem);

Upvotes: 1

Related Questions