列表框在文本框中选择的项目
本文关键字:项目 选择 文本 列表 | 更新日期: 2023-09-27 17:58:49
我有两个列表框、两个文本框和按钮。当我按下按钮时,列表框1的项目会移动到列表框2,但我想在文本字段中显示所选项目,那么我该怎么做呢??
txtbox.Text = listbox3.selecteditem.value;
不起作用,我也试过
txtbox.Text = listbox3.selecteditem.tostring();
这是我的一段代码我是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;
}
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;
}
另外,我建议您检查SelectedIndex
的值,如果在ListBox
中没有选择任何项目,则SelectedIndex
将是-1
更好版本的代码
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;
}
private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
U需要编辑最后一行代码。。。很可能是这样的。。。。使用"GetSelectedItem"而不是"SelectedItem"
TextBox2.Text = ListBox3.GetSelectedItem.toString();
我遇到了同样的问题,发现需要首先将选定的值分配给字符串。然后将该字符串分配给一个文本框。
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string value1 = listBox1.SelectedItem.ToString();
TextBox1.Text=value1;
}
希望它能有所帮助!