I'm sending Selected items form a listbox to a textbox by pressing a button using this syntax:
private void btnDesserts_Click(object sender, EventArgs e)
{
textBox1.Text += lbxDesserts.SelectedItem.ToString() + ", ";
}
but when i click the button only 1 item is inserted into the texbox, but i selected 3 items.
Any idea?
Loading
Amit ChoudharyPosted Feb 18, 2010, 1:50 AM
you can achieve this by following code:
private void button1_Click(object sender, System.EventArgs e)
{
String strItem;
foreach(Object selecteditem in listBox1.SelectedItems)
{
strItem = selecteditem as String;
Textbox1.text+=strItem;
}
}
Don't forget to mark it as answer if it helps.
Lalit MPosted Feb 18, 2010, 1:03 AM
string s = "";
foreach (ListItem x in lb_words.Items) {
if (x.Selected) s += x.Text + " ";
}
OR selected listbox item display in multiline textbox using Button
private void Button1_Click(System.Object sender, System.EventArgs e)
{
TextBox1.Text = "You selected: " + ControlChars.CrLf;
for (int intLoopIndex = 0; intLoopIndex <= ListBox1.Items.Count - 1; intLoopIndex++) {
if (ListBox1.Items(intLoopIndex).Selected) {
TextBox1.Text += ListBox1.Items(intLoopIndex).Text + ControlChars.CrLf;
}
}
}
For Reference