【问题标题】:Cloning items in a listbox c#克隆列表框中的项目c#
【发布时间】:2010-03-13 17:15:47
【问题描述】:

我有 2 个列表框,并且希望能够将选定的项目从一个复制到另一个,我想要多少次。我设法做到了,但我在第二个列表框中有按钮,可以让我上下移动。现在,当第二个列表框中的项目相同(例如“gills”和“gills”)时,它不会行为正常并崩溃。

有没有办法让它们在第二个列表框中充当单独的项目?

代码

 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);

    }

当我没有重复时它工作正常,但是当我按下上/下时它们只是随机跳来跳去。

(我不包括向下,因为它与向上几乎相同)

【问题讨论】:

  • 崩溃时出现什么错误?
  • 它“崩溃”了……太笼统了。什么是异常和堆栈跟踪?也发布您的按钮事件处理代码。
  • 您没有发布足够的信息来获得明确的答案。如果您应该更多地依赖列表中的项目索引,我猜您正在根据实际字符串进行处理。
  • 有完整源代码的最终解决方案吗?

标签: c# listbox clone


【解决方案1】:

您似乎正在环游世界,只是为了做一些简单的事情。我会使用列表和数据绑定列表来解决这个问题。

// 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();
}

【讨论】:

    【解决方案2】:

    当您有多个相同的项目时,您可以使用 SelectedIndex 代替 SelectedItem。我还建议检查它不是 -1。

    【讨论】:

      【解决方案3】:

      up case 的问题在于下面这组代码。

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

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

      您似乎反映了listBox2list2 中的项目。如果是这种情况,那么您可以直接在 listBox2 上使用 SelectedIndex 属性,因为两个 liss 中的索引将相等。

      int index = listBox2.SelectedIndex;
      

      【讨论】:

        【解决方案4】:

        如果您尝试使用对象列表,请尝试实现 Iclonnable。这将一遍又一遍地复制相同的项目。另请注意,将项目移动到顶部或底部,您不必删除列表中的项目并将它们重新插入。但是您可以更改项目的索引。希望这会有所帮助。

        【讨论】:

        • "要将项目移动到顶部或底部,您不必删除列表中的项目并重新插入它们。但您可以更改项目的索引。"如何通过更改项目索引来移动 ListBox 中的项目?据我所知,没有可用的属性或方法可以做到这一点。至少,不会这样绑定。
        【解决方案5】:

        只是代码,因为其余的答案无论如何都涵盖了它:

         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();
        
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-25
          • 2011-06-03
          • 2023-03-27
          • 1970-01-01
          相关资源
          最近更新 更多