[英]listbox selected item in a textbox
我有兩個列表框,兩個文本框和按鈕。 當我按下按鈕時,列表框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();
}
您需要編輯最后一行代碼...最有可能是這樣。...使用“ GetSelectedItem”代替“ SelectedItem”
TextBox2.Text = ListBox3.GetSelectedItem.toString();
我遇到了同樣的問題,發現您需要首先將選定的值分配給字符串。 然后將該字符串分配給文本框。
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string value1 = listBox1.SelectedItem.ToString();
TextBox1.Text=value1;
}
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.