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

标签 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”属性。

有关更多信息,我有两个列表框,我想让我的上移和下移按钮与之配合使用; 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;

    }
}

关于c# - Visual Studio 中列表框的上移、下移按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15748949/

相关文章:

asp.net - VS2012 中无法识别的标签前缀或设备过滤器 'asp'

c++ - 从 VS2012 在 C++ 中使用 ATL 创建 COM

c# - 无法将 Lambda 表达式转换为类型 'System.Type',因为它不是委托(delegate)类型

c# - 如何将来自 Web 服务的阿拉伯语值存储在数据库中

c# - 从 DataTemplate 上的按钮获取 ListBox 行对象

android - 如何避免 Activity 史

c# - C# 检查Excel文件是否为空

jquery - 按钮不能正常工作(onclick 事件)

android - 将图标与 Button android 中的文本对齐

visual-c++ - DetourFunction 未定义