[英]CopyListBoxItem from one ListBox to another
I have two ListBoxes. 我有两个列表框。 I want to copy
SelectedItem
from the first ListBox into Second one. 我想将
SelectedItem
从第一个ListBox复制到第二个。
Why this code does not work ? 为什么此代码不起作用?
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);
}
}
The result I got: 我得到的结果是:
try this I am not sure why you are looking for a Contains but if you really need that look at the difference between SelectedValue
and SelectedItem
试试这个我不确定为什么您要寻找一个包含对象,但是如果您真的需要,请查看
SelectedValue
和SelectedItem
之间的区别
Use this code right here as a test to see if the expected value shows up in a MessageBox
在此处使用此代码作为测试,以查看期望值是否显示在MessageBox中
string selected = listBox_allBuildings.GetItemText(listBox_allBuildings.SelectedValue);
MessageBox.Show(selected);
this should help you to see the values in the Listbox on the right 这应该可以帮助您查看右侧列表框中的值
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.