在Visual Studio中向上移动,向下移动ListBox的按钮

Cin*_*ndy 3 c# button selected visual-studio-2012

我正在尝试创建一个上移按钮和一个下移按钮,以移动Microsoft Visual Studio 2012中ListBox中的选定项目.我已经看到了WDF,jquery,winforms和其他一些形式的其他示例但我还没有看过Microsoft Visual Studio的例子.

我尝试过这样的事情:

        listBox1.AddItem(listBox1.Text, listBox1.ListIndex - 1);
Run Code Online (Sandbox Code Playgroud)

但Microsoft Visual Studio在其ListBox中没有"AddItem"属性.

有关更多信息,我有两个列表框,我想让我的上下移动按钮工作; SelectedPlayersListBox和AvailablePlayersListBox.有人会非常友好地向我提供Microsoft Visual Studio中"上移"和"下移"按钮的示例吗?谢谢.

djv*_*djv 12

无讽刺的答案.请享用

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;

    }
}
Run Code Online (Sandbox Code Playgroud)