2010-03-13 52 views
1

我有2个列表框,希望能够将所选项目从一个项目复制到另一个项目中,并且我想要多次。我设法做到这一点,但我有第二个列表框上的按钮,允许我去上下。现在,当第二个列表框中的项目是相同的(例如“鳃”和“鳃”),它不会行为正常并崩溃。在列表框中克隆项目c#

有没有一种方法可以让他们在第二个列表框中充当独立项目?

代码

private void buttonUp_Click(object sender, EventArgs e) 
    { 
     object selected = listBox2.SelectedItem; 
     int index = list2.Items.IndexOf(selected); 

     listBox2.Items.Remove(selected); 
     listBox2.Items.Insert(index - 1, selected); 
     listBox2.SetSelected(index - 1, true); 
    } 

    private void buttonAdd_Click(object sender, EventArgs e) 
    {   
     DataRowView selected = (DataRowView)listBox1.SelectedItem;    
     string item = selected["title"].ToString(); 
     listBox2.Items.Add(item); 

    } 

它正常工作时,我havnt有重复,但是当我这样做,他们只是跳来跳去随机当我按下向上/向下。

(IVE不包括辞去其几乎一样了)

+0

什么是它崩溃时的错误? – Turnkey 2010-03-13 17:20:22

+0

它“崩溃”......方式太笼统。什么是异常和堆栈跟踪?发布您的按钮事件处理代码。 – 2010-03-13 17:20:30

+0

您还没有发布足够的信息来获得确定的答案。我猜你是在根据实际的字符串进行处理的情况下,你应该更多地依赖列表中的项目索引。 – 2010-03-13 17:21:26

回答

0

只需将代码,因为答案的其余部分覆盖它反正:

private void buttonAdd_Click(object sender, EventArgs e) 
    { 

     DataRowView selected = listBox1.SelectedItem as DataRowView; 
     if (selected != null) 
     { 
      string item = selected["title"].ToString(); 
      listBox2.Items.Add(item); 
     } 
    } 

    private void buttonUp_Click(object sender, EventArgs e) 
    { 
     string selected = listBox2.SelectedItem as string; 
     int oldIndex = listBox2.SelectedIndex; 
     int newIndex = oldIndex; 

     if (!string.IsNullOrEmpty(selected) && listBox2.Items.Count > 1 && oldIndex > 0) 
     { 
      listBox2.SuspendLayout(); 

      listBox2.Items.RemoveAt(oldIndex); 
      newIndex = oldIndex - 1; 
      listBox2.Items.Insert(newIndex, selected); 
      listBox2.SelectedIndex = newIndex; 

      listBox2.ResumeLayout(); 

     } 
    } 
+0

我最后一次编辑还为时过早。 – 2010-03-13 18:29:57

0

可以使用的SelectedIndex代替的SelectedItem当你有多个项目都是平等的。我还建议检查它不是-1。

0

上行情况的问题是下面的一组代码。

object selected = listBox2.SelectedItem; 
int index = list2.Items.IndexOf(selected); 

只有在列表中有唯一项目时,此代码才能正常工作。一旦有重复项目,值index将成为列表中第一个实例gills的索引,而不一定是所选值的索引。

看起来像你镜像listBox2list2中的项目。如果是这种情况,那么你可以直接在listBox2上使用SelectedIndex属性,因为索引在两个liss中都是相等的。

int index = listBox2.SelectedIndex; 
0

如果您尝试使用对象列表,请尝试实施Iclonnable。这将通过&覆盖同一项目的副本。另外请注意,要将项目移到顶部或底部,您不必删除列表&中的项目,然后将其重新插入。但是你可以改变物品的索引。希望这可以帮助。

+0

“将项目移动到顶部或底部,则不必删除列表中的项目并将其重新插入,但可以更改项目的索引。如何通过更改项目索引来移动ListBox中的项目?没有财产或方法可以做我知道的。至少,不是这样绑定的。 – 2010-03-13 18:22:15

1

看起来你正在世界各地旅行,做一些简单的事情。我会用List和数据绑定列表来处理这个问题。

// Add code 
     DataRowView selected = listBox1.SelectedItem as DataRowView; 
     if (selected != null) 
     { 
      _myList.Add(selected); // Adds at end 
      BindList2(); 
     } 

// Move up code 
    int selectedIndex = listBox2.SelectedIndex; 
    if(selectedIndex > 0) 
    { 
     var temp = _myList[selectedIndex]; 
     _myList.Remove(temp); 
     _myList.InsertAt(selectedIndex - 1, temp); 
     BindList2(); 
    } 

// BindList2 
public void BindList2() 
{ 
    listBox2.DataSource = _myList; 
    listBox2.DataBind(); 
}