ListBox控件使用 (转载)

ListBox控件属性介绍:

SelectIndex:当前选中的列表项的序号。

SelectItem:当前选中的列表项。

清除列表框中全部的列表代码:

复制代码
//获取列表框的选项数
        int count = ListBox1.Items.Count;
        int index = 0;
        //循环列表框中的列表数
        for (int i = 0; i < count; i++)
        {
            ListItem item = ListBox1.Items[index];   
            //移除列表框中的列表项
            ListBox1.Items.Remove(item);
        }
        //获取下一个选项的索引值
        index++;
复制代码

清除一个或多个列表的代码:

复制代码
//获取列表框的选项数
        int count = ListBox1.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = ListBox1.Items[index];
            if (ListBox1.Items[index].Selected==true)   //判断当前列表框中选择的列表项
            {

                ListBox1.Items.Remove(item);   //移除当前列表框中选择的列表项
                index--;
            }
            index++;
        }
复制代码

上移代码:

复制代码
//若不是第一行则上移
        if (ListBox1.SelectedIndex > 0 && ListBox1.SelectedIndex <= ListBox1.Items.Count - 1)
        {
            //保存当前选项的信息
            string name = ListBox1.SelectedItem.Text;
            string value = ListBox1.SelectedItem.Value;
            //获取当前选项的索引号
            int index = ListBox1.SelectedIndex;
            //交换当前选项与上一项的信息
            ListBox1.SelectedItem.Text = ListBox1.Items[index - 1].Text;
            ListBox1.SelectedItem.Value = ListBox1.Items[index - 1].Value;
            ListBox1.Items[index - 1].Text = name;
            ListBox1.Items[index - 1].Value = value;
            //设定上一项为当前选项
            ListBox1.SelectedIndex--;
        }
复制代码

下移代码:

复制代码
//若不是最后一行则下移
        if (ListBox1.SelectedIndex >= 0 && ListBox1.SelectedIndex <ListBox1.Items.Count - 1)
        {
           //保存当前选项的信息
            string name = ListBox1.SelectedItem.Text;
            string value = ListBox1.SelectedItem.Value;
            //获取当前选项的索引号
            int index=ListBox1.SelectedIndex;
            //交换当前选项与下一项的信息
            ListBox1.SelectedItem.Text = ListBox1.Items[index + 1].Text;
            ListBox1.SelectedItem.Value = ListBox1.Items[index + 1].Value;
            ListBox1.Items[index + 1].Text = name;
            ListBox1.Items[index + 1].Value = value;
            //设定下一项为当前选项
            ListBox1.SelectedIndex++;
        }
复制代码

左边为目标列表框,右边为源列表框。

全部左移代码:

复制代码
int count = ListBox2.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = ListBox2.Items[index];
            ListBox2.Items.Remove(item);
            ListBox1.Items.Add(item);
        }
        index++;
复制代码

单个或多个右移代码:

复制代码
int count = ListBox2.Items.Count;
        int index = 0;
        for (int i = 0; i < count; i++)
        {
            ListItem item = ListBox2.Items[index];
            if (ListBox2.Items[index].Selected == true)
            {

                ListBox2.Items.Remove(item);
                ListBox1.Items.Add(item);
                index--;
            }
            index++;
        }
复制代码

 我的例子:

 

 

转载于:https://www.cnblogs.com/zqn518/archive/2012/11/23/2784740.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值