返回首页

简介
一个小的,但有时刺激性的问题,这可能会导致时间上的损失,在大型项目时,该解决方案有助于。移动ListBox的项目向上和向下按一下按钮。使用代码
首先,添加一个按钮或两个,这取决于你想要达到的目标,您的窗体,并设置此代码向上的按钮动作:

if (listBox1.SelectedItems.Count > 0)

{

    object selected = listBox1.SelectedItem;

    int indx = listBox1.Items.IndexOf(selected);

    int totl = listBox1.Items.Count;



    if (indx == 0)

    {

        listBox1.Items.Remove(selected);

        listBox1.Items.Insert(totl - 1, selected);

        listBox1.SetSelected(totl - 1, true);

    }

    else{

        listBox1.Items.Remove(selected);

        listBox1.Items.Insert(indx - 1, selected);

        listBox1.SetSelected(indx - 1, true);

    }

}

,然后,添加此向下按钮动作:{C}兴趣点
为了更好的可用性,这些按钮可以禁用默认情况下,选择项目时,可以启用。此外,我希望这个代码会派上用场!

回答

评论会员:添horigan 时间:2011/12/16
这正是我一直在寻找什么。代码的伟大工程!比大的好。谢谢! !
评论会员:添horigan 时间:2011/12/16
这也正是我一直在寻找的代码。伟大工程。 。 。比大!感谢
评论会员:ReymonARG 时间:2011/12/16
没有演示,基本信息
:tooMuchCode
评论会员:游客 时间:2011/12/16
此代码只要罚款列表中的所有值完全独特的。不过,如果你有多个项目列表中相同的文字,你会得到意想不到的效果,删除的项目并不总是您选择的项目也可能是其他副本相同的文字。我发现这个硬盘的方式和后绊了我想通了,如果有一个重复的文本的机会,你应该使用ListBox1.RemoveAt();.{BR}这里是我使用的完整代码:(本文类似的方法说明,但略有不同)私人无效btnMoveUp_Click(对象发件人,EventArgs的发送){//使确保项目被选中,否则跳过程序,以防止错误。如果(ListBox1.SelectedIndexGT=0){//记录原来的位置和文字。INT32iSelectedIndex=ListBox1.SelectedIndex字符串sSelectedItem=ListBox1.SelectedItem.ToString()//确定新的位置。INT32iNewIndex=(iSelectedIndex-1=GT00:iSelectedIndex-1)//删除索引号的项目,因为索引号将永远是唯一的。ListBox1.Items.RemoveAt(iSelectedIndex);//插入新的索引和orignal文本项目。ListBox1.Items.Insert(iNewIndex,sSelectedItem)//在新的位​​置选择项目。ListBox1.SelectedIndex=iNewIndex;}}私人无效btnMoveDown_Click(对象发件人,EventArgs的发送){如果(ListBox1.SelectedIndexGT=0){INT32iSelectIndex=ListBox1.SelectedIndex字符串sSelectedItem=ListBox1.SelectedItem.ToString()Int32的iUpperLimit=ListBox1.Items.Count-1INT32iNewIndex=(iSelectIndex1GT=iUpperLimitiUpperLimit:iSelectIndex1);ListBox1.Items.RemoveAt(iSelectIndex);ListBox1.Items.Insert(iNewIndex,sSelectedItem)ListBox1.SelectedIndex=iNewIndex;}}我希望可能的例子可以节省人小时,我刚刚经历了挫折并宣誓就职。你们都有一个美好的一天!tooMuchCode"有时当我闭上眼睛的代码来自淋浴头"