【问题标题】:Issue with moving items between listboxes在列表框之间移动项目的问题
【发布时间】:2014-10-23 00:57:22
【问题描述】:

我的 Windows C# WinForms 应用程序中有 2 个列表框。我已经编写了在我的 2 个列表框之间移动项目的代码。我可以选择任何/多个项目并在框之间移动它们。但是,如果我选择列表框中的底部项目并将其移动到另一个列表框,则从中移动的表中的内容显示不正确。它没有显示实际内容,而是显示:- ProgramName__.objectname 为我移动的条目上方的每个条目!这只发生在列表框中的最后一项。如果我选择显示错误的条目并将其移动到另一个列表框,则会显示正确的信息。

在我看来这是一个错误,但我不确定。我可以移动列表框中的任何其他项目,并且两个列表框都正确显示。

public class customers
{
    public int id { get; set; }
    public string name {get; set; }
}    



this.table1Box.DataSource = this.customersBindingSource;
this.table1Box.DisplayMember = "name";
this.table1Box.Name = "Table1Name";
this.table1Box.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
this.table1Box.Sorted = true;
this.table1Box.TabIndex = 13;
this.table1Box.ValueMember = "id";

this.table2Box.DataSource = this.customersBindingSource;
this.table2Box.DisplayMember = "name";
this.table2Box.Name = "Table2Name";
this.table2Box.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
this.table2Box.Sorted = true;
this.table2Box.TabIndex = 14;
this.table2Box.ValueMember = "id";


// Click method for moving table 1 -> table 2
private void table1ToTable2Button_Click(object sender, EventArgs e)
{
    foreach (int i in this.table1Box.SelectedIndices)
    {
        selectedCustomer = (customers)this.table1Box.Items[i];

        table2List.Add(selectedCustomer);     
        table1List.Remove(selectedCustomer);

    }
    this.table1Box.DataSource = this.emptyList;
    this.table1Box.DataSource = this.table1List;
    this.table1Box.Update();
    this.table2Box.DataSource = this.emptyList;
    this.table2Box.DataSource = this.table2List;
    this.table2Box.Update();
}

![程序开始时的图片]http://www.mclenaghan.com/Pic1.jpg

![移动最后一项后的图片]http://www.mclenaghan.com/Pic2.jpg

![图片移动项目2]http://www.mclenaghan.com/Pic3.jpg

【问题讨论】:

  • 你确定这是WPF吗?看起来像它的Winforms。我会在几分钟后重新标记它。
  • 是的,对不起。 Windows 窗体。呵呵。
  • 我做了一个有趣的测试。删除后,如果我执行以下操作:- table1List = table1List.ToList();问题消失了。然而,这是一个 hack,因为您基本上是将整个对象列表复制到自身。既然这解决了这个问题,我想知道这是否是一个错误,也许删除最后一项会以某种方式破坏列表结构。

标签: c# winforms listbox


【解决方案1】:

确保您绑定到BindingList<customers>

您无需将DataSource 设置为空列表,然后再次设置为实际列表,然后调用ListBox.Update()。这似乎有效,但这也意味着您在绑定中做错了。

还有一件事 - 不要手动编辑设计器生成的代码,使用属性窗格。我发现即使更改InitializeComponent 方法中的代码序列,列表框也会显示不正确。

BindingList<customers> table1List;
BindingList<customers> table2List;

public FormWith2Listboxes()
{
    InitializeComponent();

    table1List = new BindingList<customers>();
    table1List.Add(new customers() { id = 1, name = "name1" });
    table1List.Add(new customers() { id = 2, name = "name2" });
    table1List.Add(new customers() { id = 3, name = "name3" });

    table2List = new BindingList<customers>();
    table2List.Add(new customers() { id = 4, name = "name4" });

    this.table1Box.DataSource = this.table1List;
    this.table2Box.DataSource = this.table2List;
}

private void table1ToTable2Button_Click(object sender, EventArgs e)
{
    foreach (int i in this.table1Box.SelectedIndices)
    {
        var selectedCustomer = (customers)this.table1Box.Items[i];

        table2List.Add(selectedCustomer);
        table1List.Remove(selectedCustomer);
    }
}

【讨论】:

  • 谢谢。那成功了。这是我的第一个 C# 应用程序,所以我还在学习中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多