HTML:
    <form && ListBox1.SelectedIndex < ListBox1.Items.Count - 1)
        { //判断传来的命令名必须是 up并且所选条目的索引必须大于0 或者 down并且所选条目必须小于最大项

            int index;//为了减少代码,这里做一个对变量的判断,以后就直接调用变量,
            if (((Button)sender).CommandName == "up")
            {
                index = -1;//以后的索引本来就是在当前的条目上加一或者减,所以这个方法很不错 
            }
            else
            {
                index = 1;
            }
            ListItem lt = new ListItem(ListBox1.SelectedItem.Text, ListBox1.SelectedValue);//将当前条目的文本以及值都保存到一个临时变量里面
            ListBox1.Items[ListBox1.SelectedIndex].Text = ListBox1.Items[ListBox1.SelectedIndex + index].Text;//被选中项的值等于上一条或者下一条的值
            ListBox1.Items[ListBox1.SelectedIndex].Value = ListBox1.Items[ListBox1.SelectedIndex + index].Value;//被选中项的值等于上一条或者下一条的值
            ListBox1.Items[ListBox1.SelectedIndex + index].Text = lt.Text;//把被选中项的上一条或者下一条的值用临时变量中的取代
            ListBox1.Items[ListBox1.SelectedIndex + index].Value = lt.Value;//把被选中项的上一条或者下一条的值用临时变量中的取代
            ListBox1.SelectedIndex = ListBox1.SelectedIndex + index;//把鼠标指针放到移动后的那条上
        }
    }

注释:
移位包括二种,其一是向上移位,其二是向下移位。程序中具体的实现思路是:创建一个ListItem对象,并把要移位指定的条目中的内容先暂放在此新建的这个对象中。如果选定的是向上移位,就把当前选定的条目的上一个条目的值赋值给当前选定的条目,然后把刚才新建的对象的值,再赋值给选定条目的上一个条目,完成条目的向上移位操作。对于向下移位,可以仿效上面的做法,但和上面做法的主要区别在于不是选定条目的上一个条目了,而是选定条目的下一个条目。
在一般编程中都应该判断列表中是否有数据,这里不用了,因为加上判断,代码太多了,没意思

相关文章:

  • 2021-06-30
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-02
  • 2021-05-16
猜你喜欢
  • 2022-02-08
  • 2022-01-12
  • 2022-12-23
  • 2022-01-07
  • 2021-07-21
  • 2021-10-19
  • 2022-12-23
相关资源
相似解决方案