C# 克隆列表框c中的项目#
我有两个列表框,希望能够复制所选项目从一个到另一个有多少次我想。我成功地做到了这一点,但我在第二个列表框上有按钮,允许我上下移动。现在,当第二个列表框中有相同的项目(例如“gills”和“gills”)时,它的行为不正常并崩溃 有没有办法让它们在第二个列表框中作为单独的项目 代码 当我没有得到复制品时,它工作得很好,但当我按下上/下键时,它们只是随机跳转C# 克隆列表框c中的项目#,c#,listbox,clone,C#,Listbox,Clone,我有两个列表框,希望能够复制所选项目从一个到另一个有多少次我想。我成功地做到了这一点,但我在第二个列表框上有按钮,允许我上下移动。现在,当第二个列表框中有相同的项目(例如“gills”和“gills”)时,它的行为不正常并崩溃 有没有办法让它们在第二个列表框中作为单独的项目 代码 当我没有得到复制品时,它工作得很好,但当我按下上/下键时,它们只是随机跳转 (我不包括向下,因为它与向上几乎相同)只包括代码,因为其余的答案都涵盖了它: private void buttonAdd_Click(ob
(我不包括向下,因为它与向上几乎相同)只包括代码,因为其余的答案都涵盖了它:
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();
}
}
只是代码,因为其他答案都涵盖了它:
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();
}
}
当您有多个相等的项目时,可以使用SelectedIndex而不是SelectedItem。我还建议检查它是否为-1。当您有多个相等的项时,可以使用SelectedIndex而不是SelectedItem。我还建议检查它是否不是-1。up案例的问题是以下代码集
object selected = listBox2.SelectedItem;
int index = list2.Items.IndexOf(selected);
只有在列表中有唯一项时,此代码才能正常工作。一旦有重复项,值index
将是列表中saygills
的第一个实例的索引,而不一定是所选值的索引
您似乎镜像了listBox2
和list2
中的项目。如果是这种情况,那么您可以直接在listBox2
上使用SelectedIndex属性,因为两个LIS中的索引都是相等的
int index = listBox2.SelectedIndex;
up案例的问题是以下代码集
object selected = listBox2.SelectedItem;
int index = list2.Items.IndexOf(selected);
只有在列表中有唯一项时,此代码才能正常工作。一旦有重复项,值index
将是列表中saygills
的第一个实例的索引,而不一定是所选值的索引
您似乎镜像了listBox2
和list2
中的项目。如果是这种情况,那么您可以直接在listBox2
上使用SelectedIndex属性,因为两个LIS中的索引都是相等的
int index = listBox2.SelectedIndex;
如果您试图使用对象列表,请尝试实现Iclonnable。这将一次又一次地复制同一项目。另请注意,要将项目移动到顶部或底部,不必删除列表中的项目并重新插入。但是您可以更改项目的索引。希望这对您有所帮助。如果您试图使用对象列表,请尝试实现iClonable。这将一次又一次地复制同一项目。另请注意,要将项目移动到顶部或底部,不必删除列表中的项目并重新插入。但是您可以更改项目的索引。希望这能有所帮助。你似乎在环游世界做一些简单的事情。我将使用列表和数据绑定列表来处理这个问题
// 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();
}
看起来你环游世界是为了做一些简单的事情。我将使用列表和数据绑定列表来处理这个问题
// 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();
}
当它崩溃时有什么错误?它“崩溃”。。。太笼统了。什么是异常和堆栈跟踪?同时发布您的按钮事件处理代码。您没有发布足够的信息以获得明确的答案。我猜,如果您应该更多地依赖列表中项目的索引,那么您正在基于实际字符串进行处理。是否有完整源代码的最终解决方案?崩溃时会出现什么错误?它“崩溃”。。。太笼统了。什么是异常和堆栈跟踪?同时发布您的按钮事件处理代码。您没有发布足够的信息以获得明确的答案。我猜,如果您应该更多地依赖列表中项目的索引,您是在基于实际字符串进行处理。是否有完整源代码的最终解决方案?“要将项目移动到顶部或底部,您不必删除列表中的项目并重新插入。但您可以更改项目的索引。”如何通过更改项目索引来移动列表框中的项目?据我所知,没有任何属性或方法可用于执行此操作。至少,不要这样绑定。“要将项目移动到顶部或底部,您不必删除列表中的项目并重新插入。但您可以更改项目的索引。”如何通过更改项目索引来移动列表框中的项目?据我所知,没有任何属性或方法可用于执行此操作。至少,没有这样的限制。