我有两个列表框。我想将 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);
}
}
我得到的结果:
最佳答案
试试这个 我不确定你为什么要寻找一个 Contains 但如果你真的需要看看 SelectedValue
和 SelectedItem
Use this code right here as a test to see if the expected value shows up in a 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);
}
}
关于c# - CopyListBoxItem 从一个 ListBox 到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25332311/