nc's world:

天行健,君子以自强不息
地势坤,君子以厚德载物

博客园 首页 新随笔 联系 订阅 管理
        //向上下移动一条 事件
        if (((Button)sender).CommandName == "up" && ListBox6.SelectedIndex > 0 || ((Button)sender).CommandName == "down" && ListBox6.SelectedIndex < ListBox6.Items.Count - 1)
        { 
//判断传来的命令名必须是 up并且所选条目的索引必须大于0 或者 down并且所选条目必须小于最大项
           
            
int index;//为了减少代码,这里做一个对变量的判断,以后就直接调用变量,
            if (((Button)sender).CommandName == "up")
            {
                index 
= -1;//以后的索引本来就是在当前的条目上加一或者减,所以这个方法很不错 
            }
            
else
            {
                index 
= 1;
            }
            ListItem lt 
= new ListItem(ListBox6.SelectedItem.Text,ListBox6.SelectedValue);//将当前条目的文本以及值都保存到一个临时变量里面
            ListBox6.Items [ListBox6.SelectedIndex].Text = ListBox6.Items[ListBox6.SelectedIndex + index]. Text;//被选中项的值等于上一条或者下一条的值
            ListBox6.Items [ListBox6.SelectedIndex].Value = ListBox6.Items[ListBox6.SelectedIndex + index]. Value;//被选中项的值等于上一条或者下一条的值
            ListBox6.Items[ListBox6.SelectedIndex + index].Text = lt.Text;//把被选中项的上一条或者下一条的值用临时变量中的取代
            ListBox6.Items[ListBox6.SelectedIndex + index].Value = lt.Value;//把被选中项的上一条或者下一条的值用临时变量中的取代
            ListBox6.SelectedIndex = ListBox6.SelectedIndex + index;//把鼠标指针放到移动后的那条上
        }
posted on 2006-08-24 00:08  nicesnow  阅读(501)  评论(0编辑  收藏  举报