【发布时间】: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,因为您基本上是将整个对象列表复制到自身。既然这解决了这个问题,我想知道这是否是一个错误,也许删除最后一项会以某种方式破坏列表结构。