加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在Visual Studio中向上移动,向下移动ListBox的按钮[复制]

发布时间:2020-12-16 00:22:39 所属栏目:百科 来源:网络整理
导读:参见英文答案 How to move item in listBox up and down?????????????????????????????????????15个 我正在尝试创建一个上移按钮和一个下移按钮,以移动Microsoft Visual Studio 2012中的ListBox中的选定项目.我已经在WDF,jquery,winforms和其他一些形式中看
参见英文答案 > How to move item in listBox up and down?????????????????????????????????????15个
我正在尝试创建一个上移按钮和一个下移按钮,以移动Microsoft Visual Studio 2012中的ListBox中的选定项目.我已经在WDF,jquery,winforms和其他一些形式中看到了其他示例但我没有’看过Microsoft Visual Studio的例子.

我试过这样的事情:

listBox1.AddItem(listBox1.Text,listBox1.ListIndex - 1);

但Microsoft Visual Studio在其ListBox中没有“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;

    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读