我有两个ListBoxes。我想将SelectedItem
从第一个ListBox复制到第二个。
为什么这段代码不起作用?
private void frm_addDispatchBoard2_Load(object sender, EventArgs e)
{
using(propertiesManagementDataContext db = new propertiesManagementDataContext())
{
var Buildings = db.Buildings.Select(q => new { q.BuildingLandNumber, q.BuildingId });
listBox_allBuildings.DataSource = Buildings;
listBox_allBuildings.DisplayMember = "BuildingLandNumber";
listBox_allBuildings.ValueMember = "BuildingId";
}
}
private void btn_addBuilding_Click(object sender, EventArgs e)
{
if(listBox_allBuildings.SelectedIndex > 0)
{
listBox_selectedBuildings.Items.Add(listBox_allBuildings.SelectedItem);
}
}
我得到的结果:
答案 0 :(得分:0)
尝试这个我不确定你为什么要寻找包含但如果你真的需要看看SelectedValue
和SelectedItem
之间的区别
在此处使用此代码作为测试,以查看期望值是否显示在MessageBox
中
string selected = listBox_allBuildings.GetItemText(listBox_allBuildings.SelectedValue);
MessageBox.Show(selected);
这可以帮助您查看右侧列表框中的值
private void btn_addBuilding_Click(object sender, EventArgs e)
{
if(listBox_allBuildings.SelectedIndex != -1)
{
var selected = listBox_allBuildings.GetItemText(listBox_allBuildings.SelectedValue);
listBox_selectedBuildings.Items.Add(selected);
}
}