我正在尝试制作一个上移按钮和一个下移按钮,以在 Microsoft Visual Studio 2012 的列表框中移动选定的项目。我在 WDF、jquery、winforms 和其他一些形式中看到了其他示例,但是我还没有看到来自 Microsoft Visual Studio 的例子。
我试过这样的:
listBox1.AddItem(listBox1.Text, listBox1.ListIndex - 1);
但 Microsoft Visual Studio 在其列表框中没有“AddItem”属性。
有关更多信息,我有两个列表框,我想让我的上移和下移按钮与之配合使用; SelectedPlayersListBox 和 AvailablePlayersListBox。有人愿意给我提供 Microsoft Visual Studio 中“向上移动”和“向下移动”按钮的示例吗?谢谢。
无讽刺的回答。享受
private void btnUp_Click(object sender, EventArgs e)
{
MoveUp(ListBox1);
}
private void btnDown_Click(object sender, EventArgs e)
{
MoveDown(ListBox1);
}
void MoveUp(ListBox myListBox)
{
int selectedIndex = myListBox.SelectedIndex;
if (selectedIndex > 0)
{
myListBox.Items.Insert(selectedIndex - 1, myListBox.Items[selectedIndex]);
myListBox.Items.RemoveAt(selectedIndex + 1);
myListBox.SelectedIndex = selectedIndex - 1;
}
}
void MoveDown(ListBox myListBox)
{
int selectedIndex = myListBox.SelectedIndex;
if (selectedIndex < myListBox.Items.Count - 1 & selectedIndex != -1)
{
myListBox.Items.Insert(selectedIndex + 2, myListBox.Items[selectedIndex]);
myListBox.Items.RemoveAt(selectedIndex);
myListBox.SelectedIndex = selectedIndex + 1;
}
}