C# Visual Studio中列表框的上移、下移按钮

C# Visual Studio中列表框的上移、下移按钮,c#,button,visual-studio-2012,selected,C#,Button,Visual Studio 2012,Selected,我正在尝试使用“上移”按钮和“下移”按钮来移动Microsoft Visual Studio 2012中列表框中的选定项。我在WDF、jquery、winforms和其他一些表单中看到了其他示例,但我还没有看到来自Microsoft Visual Studio的示例 我试过这样的方法: listBox1.AddItem(listBox1.Text, listBox1.ListIndex - 1); 但是Microsoft Visual Studio的列表框中没有“AddItem

我正在尝试使用“上移”按钮和“下移”按钮来移动Microsoft Visual Studio 2012中列表框中的选定项。我在WDF、jquery、winforms和其他一些表单中看到了其他示例,但我还没有看到来自Microsoft Visual Studio的示例

我试过这样的方法:

        listBox1.AddItem(listBox1.Text, listBox1.ListIndex - 1);
但是Microsoft Visual Studio的列表框中没有“AddItem”属性


更多信息,我有两个列表框,我想让我的上下移动按钮工作;选择的播放器列表框和可用的播放器列表框。有没有人能给我举个例子,说明微软VisualStudio中的上下移动按钮?谢谢。

您正在查找
ListBox.Items.Add()

对于向上移动,类似这样的操作应该有效:

void MoveUp()
{
    if (listBox1.SelectedItem == null)
        return;

    var idx = listBox1.SelectedIndex;
    var elem = listBox1.SelectedItem;
    listBox1.Items.RemoveAt(idx);
    listBox1.Items.Insert(idx - 1, elem);
}

要下移,只需将idx-1更改为idx+1即可。享受

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;

    }
}
private void btnUp\u单击(对象发送方,事件参数e)
{
向上移动(列表框1);
}
私有void btnDown\u单击(对象发送者,事件参数e)
{
向下移动(列表框1);
}
无效上移(列表框myListBox)
{
int selectedIndex=myListBox.selectedIndex;
如果(已选择索引>0)
{
myListBox.Items.Insert(selectedIndex-1,myListBox.Items[selectedIndex]);
myListBox.Items.RemoveAt(选择索引+1);
myListBox.SelectedIndex=SelectedIndex-1;
}
}
无效向下移动(列表框myListBox)
{
int selectedIndex=myListBox.selectedIndex;
if(selectedIndex
您在使用Winforms吗?您有点困惑。在“.NET Framework”中的
System.Windows.Forms.ListBox
中找不到名为
AddItem
的方法,该方法与“Visual Studio”不同。可能是@antosemenov的副本,而不是真正的Anton。我正在使用Microsoft Visual Studio。是的,我也检查了这个问题,但Visual Studio不是这样工作的,所以。这就是为什么我要问这个问题寻求更多的帮助。哦,我们之间有高度的不理解。。。我知道您正在visual studio的帮助下创建应用程序。。。您使用的语言是C#,但什么是项目类型?您可能在winforms或WPF上。对于每个选项,可能有两个子选项-您使用的是标准组件(随VS一起提供)和3d party组件。哪个是你的?ListIndex部分呢?@Nefarion我还建议在重新添加元素后重置SelectedIndex/项。谢谢你,Nefarion。没有错误,但它不会移动项目:CHi(:你是最好的。哇,太棒了。谢谢。