0

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

}
4

2 回答 2

1

SelectedItems是一个集合,而不是一个项目。

你应该这样做

var selItem = emailBankListBox1.SelectedItem;
var selectedString=selItem.ToString();
var idx = emailBankListBox1.Items.IndexOf(selItem);
于 2012-11-04T06:27:29.937 回答
0

你本质ToString上是在调用一个集合。如果要将每个选定的项目添加到单独的列表框中,可以执行以下操作:

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

如果你Linq,你可以直接这样做:

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

如果你里面的对象emailBankListBox1不仅仅是字符串,那么你将不得不设置显示成员属性listbox1

于 2012-11-04T07:48:11.243 回答