gpt4 book ai didi

c# - CopyListBoxItem 从一个 ListBox 到另一个

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:16 24 4
gpt4 key购买 nike

我有两个列表框。我想将 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);
}
}

我得到的结果:

enter image description here

最佳答案

试试这个 我不确定你为什么要寻找一个 Contains 但如果你真的需要看看 SelectedValueSelectedItem

之间的区别

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com