ListBox实现上下移动

protected   void  btn_up_Click( object  sender, EventArgs e)
    
{
        
if (lb_busspot.SelectedIndex == -1)
        
{
            
return;
        }


        
//获得连续选中的项索引
        int[] Indices = lb_busspot.GetSelectedIndices();
        
int length = Indices.Length;

        
string text;
        
string value;
        
//如果选择的最小索引是0,表示是最上面的项
        if (Indices[0== 0)
        
{
            
return;
        }

        
//判断选择多项时是否是连续的项
        if (Indices.Length != 1 && Indices[0+ length - 1 != Indices[length - 1])
        
{
            MessageBox.Show(Page, 
"请选择连续的项!");
            
return;
        }

        
//将选中的上面一项未选中的值赋予临时变量
        text = lb_busspot.Items[Indices[0- 1].Text;
        value 
= lb_busspot.Items[Indices[0- 1].Value;

        
for (int i = 0; i < length; i++)
        
{
            lb_busspot.Items[Indices[i] 
- 1].Text = lb_busspot.Items[Indices[i]].Text;
            lb_busspot.Items[Indices[i] 
- 1].Value = lb_busspot.Items[Indices[i]].Value;
            
//保证被选中状态
            lb_busspot.Items[Indices[i] - 1].Selected = true;
            lb_busspot.Items[Indices[i]].Selected 
= false;
        }

        
//将选中的上面第一条未选中的值赋予到下面
        lb_busspot.Items[Indices[0+ length - 1].Text = text;
        lb_busspot.Items[Indices[
0+ length - 1].Value = value;

    }

    
protected   void  btn_down_Click( object  sender, EventArgs e)
    
{
        
if (lb_busspot.SelectedIndex == -1)
        
{
            
return;
        }

        
//获得连续选中的项索引
        int[] Indices = lb_busspot.GetSelectedIndices();
        
int length = Indices.Length;

        
string text;
        
string value;
        
//如果选择的是最底下的项
        if (Indices[length - 1== lb_busspot.Items.Count - 1)
        
{
            
return;
        }

        
//判断选择多项时是否是连续的项
        if (Indices.Length != 1 && Indices[0+ length - 1 != Indices[length - 1])
        
{
            MessageBox.Show(Page, 
"请选择连续的项!");
            
return;
        }

        
//将选中的下面一项未选中的值赋予临时变量
        text = lb_busspot.Items[Indices[length - 1+ 1].Text;
        value 
= lb_busspot.Items[Indices[length - 1+ 1].Value;
        
for (int i = length; i > 0; i--)
        
{
            lb_busspot.Items[Indices[i 
- 1+ 1].Text = lb_busspot.Items[Indices[i - 1]].Text;
            lb_busspot.Items[Indices[i 
- 1+ 1].Value = lb_busspot.Items[Indices[i - 1]].Value;
            
//保证被选中状态
            lb_busspot.Items[Indices[i - 1+ 1].Selected = true;
            lb_busspot.Items[Indices[i 
- 1]].Selected = false;
        }

        
//将下面第一条未选中的项的值赋予到上面
        lb_busspot.Items[Indices[0]].Text = text;
        lb_busspot.Items[Indices[
0]].Value = value;
    }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值