现在的位置: 首页 > 综合 > 正文

vs2005空间之ListBox有用的代码– //向上下移动

2013年02月16日 ⁄ 综合 ⁄ 共 1205字 ⁄ 字号 评论关闭
        //向上下移动一条 事件
        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;//把鼠标指针放到移动后的那条上
        }

抱歉!评论已关闭.