Reputation: 105
I have two listboxes, two text boxes and buttons. When I press the buttons the items of list box 1 is moved to list box2 but i want to display the selected items in the textfields so how do i do it??
txtbox.Text = listbox3.selecteditem.value;
is not working, i also tried
txtbox.Text = listbox3.selecteditem.tostring();
this is my piece of code. 'm a fresher and new to asp .net
if (RadioButton1.Checked == true)
{
a = ListBox3.SelectedValue.ToString();
b = ListBox3.SelectedIndex;
ListBox4.Items.Add(a);
ListBox3.Items.RemoveAt(ListBox3.Items.IndexOf((ListBox3.SelectedItem)));
TextBox1.Text = RadioButton1.Text.ToString();
TextBox2.Text = ListBox3.SelectedItem.Value;
}
Upvotes: 1
Views: 36982
Reputation: 19
I had the same issue and found that you need to assign first the selected value to a string. Then assign that string to a textbox.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string value1 = listBox1.SelectedItem.ToString();
TextBox1.Text=value1;
}
Hope it helps!
Upvotes: 0
Reputation: 21
private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
Upvotes: 2
Reputation: 86
U need to edit the last line of code... Most Probably it goes like this.... Use "GetSelectedItem" instead of "SelectedItem"
TextBox2.Text = ListBox3.GetSelectedItem.toString();
Upvotes: 0
Reputation: 20575
if (RadioButton1.Checked == true)
{
var a = ListBox3.SelectedValue.ToString();
var b = ListBox3.SelectedIndex;
ListBox4.Items.Add(a);
ListBox3.Items.RemoveAt(b);
TextBox1.Text = RadioButton1.Text.ToString();
TextBox2.Text = a;
}
addtionally, i would suggest you to check the SelectedIndex
value, if none item is selected in the ListBox
, the SelectedIndex
will be -1
Better version of your code
if (RadioButton1.Checked == true)
{
var b = ListBox3.SelectedIndex;
var a = ListBox3.SelectedValue.ToString();
if (b < 0)
{
// no ListBox item selected;
return;
}
ListBox4.Items.Add(a);
ListBox3.Items.RemoveAt(b);
TextBox1.Text = RadioButton1.Text.ToString();
TextBox2.Text = a;
}
Upvotes: 3