我需要一些帮助我无法弄清楚我的代码有什么问题,每次我试图让程序显示SelectedItems我得到“system.windows.forms.listbox + selectedobjectcollection”。我可以从组合框中获取文本到emailBankListBox1,但是当我尝试从emailBanklistBox1跳到listBox1时,我得到上面列出的文本。任何和所有的帮助非常感谢。
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);
}
}
答案 0 :(得分:1)
SelectedItems是一个集合,而不是一个项目。
你应该这样做
var selItem = emailBankListBox1.SelectedItem;
var selectedString=selItem.ToString();
var idx = emailBankListBox1.Items.IndexOf(selItem);
答案 1 :(得分:0)
您实际上是在集合上调用ToString
。如果要将每个选定的项目添加到单独的列表框中,您可以执行以下操作:
foreach(var item in emailBankListBox1.SelectedItems)
listBox1.Items.Add(item.ToString());
如果你是Linq,你可以直接做:
listBox1.Items.AddRange(emailBankListBox1.SelectedItems.OfType<string>());
如果emailBankListBox1
中的对象不仅仅是字符串,那么您必须在listbox1