C# ListBox 左移、右移、上移、下移

2012-11-17 22:53:45|  分类: 技术研讨 |  标签:listbox  |字号 订阅

 
 

/// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBoxOperate_OnCommand(object sender, EventArgs e)
        {
            Button btn = sender as Button;

List<string> list = new List<string>();

switch (btn.Tag.ToString())
            {
                case "ToRight":
                    if (listBox1.SelectedItems != null)
                    {
                        foreach (var item in listBox1.SelectedItems)
                        {
                            listBox2.Items.Add(item.ToString());
                        }
                        this.listBox2.SelectedIndex = this.listBox2.Items.Count - 1;
                        for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
                        {
                            listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
                            i--;
                        }
                    }
                    break;
                case "AllToRight":
                    if (this.listBox1.Items.Count > 0)
                    {
                        for (int i = 0; i < listBox1.Items.Count; i++)
                        {
                            list.Add(listBox1.Items[i].ToString());
                        }
                        for (int j = 0; j < list.Count; j++)
                        {
                            this.listBox2.Items.Add(list[j]);
                        }
                        this.listBox1.Items.Clear();
                        break;
                    }
                    else
                    {
                        break;
                    }
                case "ToLeft":
                   
                    if (listBox2.SelectedItems != null)
                    {
                        foreach (var item in listBox2.SelectedItems)
                        {
                            listBox1.Items.Add(item.ToString());
                        }
                        this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
                        for (int i = 0; i < listBox2.SelectedIndices.Count; i++)
                        {
                            listBox2.Items.RemoveAt(listBox2.SelectedIndices[i]);
                            i--;
                        }
                    }
                    break;
                case "AllToLeft":
                    if (this.listBox2.Items.Count > 0)
                    {

for (int i = 0; i < listBox2.Items.Count; i++)
                        {
                            list.Add(listBox2.Items[i].ToString());
                        }
                        for (int j = 0; j < list.Count; j++)
                        {
                            this.listBox1.Items.Add(list[j]);
                        }
                        this.listBox2.Items.Clear();
                        break;
                    }
                    else
                    {
                        break;
                    }
                case "ToUp":
                    // 上移
                    if (this.listBox2.SelectedIndices.Count > 0 &&
                        this.listBox2.SelectedIndices[0] > 0)
                    {
                        int[] newIndices =
                            this.listBox2.SelectedIndices.Cast<int>()
                            .Select(index => index - 1).ToArray();

this.listBox2.SelectedItems.Clear();

for (int i = 0; i < newIndices.Length; i++)
                        {
                            object obj = this.listBox2.Items[newIndices[i]];
                            this.listBox2.Items[newIndices[i]] = this.listBox2.Items[newIndices[i] + 1];
                            this.listBox2.Items[newIndices[i] + 1] = obj;
                            this.listBox2.SelectedItems.Add(this.listBox2.Items[newIndices[i]]);
                        }
                    }

break;
                case "ToDown":

// 下移
                    if (this.listBox2.SelectedIndices.Count > 0 &&
                        this.listBox2.SelectedIndices[this.listBox2.SelectedIndices.Count - 1] <
                        this.listBox2.Items.Count - 1)
                    {
                        int[] newIndices =
                            this.listBox2.SelectedIndices.Cast<int>()
                            .Select(index => index + 1).ToArray();

this.listBox2.SelectedItems.Clear();

for (int i = newIndices.Length; i > 0; i--)
                        {
                            object obj = this.listBox2.Items[newIndices[i - 1]];
                            this.listBox2.Items[newIndices[i - 1]] = this.listBox2.Items[newIndices[i-1]-1];
                            this.listBox2.Items[newIndices[i-1]-1] = obj;
                            this.listBox2.SelectedItems.Add(this.listBox2.Items[newIndices[i-1]]);

}
                    }
                    break;
            }
        }

注意:此方法只是简单的顺序移动,如果某一listbox涉及其事件时,就要加条件!

C# ListBox 左移、右移、上移、下移的更多相关文章

  1. wpf listbox 选中项 上移下移

    原文:wpf listbox 选中项 上移下移 private void MoveUp_Click(object sender, RoutedEventArgs e)         {        ...

  2. jQuery实现左移右移

    <html> <head> <meta charset="utf-8"> <title>完成左移右移</title> & ...

  3. php修改排序,上移下移

    php修改排序,上移下移 /**    $UpDown //移动方向,up或down    $table //表名    $id //当前移动的ID    $id_col //ID字段的名称    $ ...

  4. EASYUI- EASYUI左移右移 GRID中值

    EASYUI左移右移 GRID中值 $("#addAll").click(function(){ var ids = []; var names = []; var srcrows ...

  5. 【转载】c语言数据的左移右移

    原文地址:http://www.cnblogs.com/myblesh/articles/2431806.html 由于在飞控程序中执行效率对程序的影响相当大,所以一个好的运算效率很重要.左移右移比单 ...

  6. 嵌入式C开发---用循环实现左移右移

    //将n左移m位 int byte_to_left_move(int n , int m) { int i , ret = 1 ; if(n == 0 || n < 0) { return ; ...

  7. jQuery实现表格行上移下移和置顶

    jQuery实现表格行上移下移和置顶 我们在操作列表数据的时候,需要将数据行排列顺序进行调整,如上移和下移行,将行数据置顶等,这些操作都可以在前端通过点击按钮来完成,并且伴随着简单的动态效果,轻松实现 ...

  8. JS移动li行数据,点击上移下移(是位置的互换,不是top的偏移量改变)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. table中实现数据上移下移效果

    html 由于vue+Element项目中的table,没有开放的上移下移的api,但是能对数据操作,故思路为数组中的一条数据,再重新添加一条数据,办法有点笨,但是好歹也是实现了,望有好的办法的,请留 ...

随机推荐

  1. struts2_validate表单验证

    使用代码实现 验证功能 (也就是重写ActionSupport中的validate()方法) 在validate方法中进行编写我们需要的验证功能 这里需要提几点的就是: 1.当我们需要对action中 ...

  2. js >> 右移操作符

    十进制 十六进制 二进制 右移>> 十进制值 F 1F FF

  3. pycharm执行代码可以跑,但放到linux跑就报文件找不到

    代码中包含当前路径 使用pycharm执行python,当前路径就是pycharm项目所在的路径,所以不会报错 但使用shell执行python,当前路径就会从shell所在的路径去找文件,所以找不到 ...

  4. CDR服装设计-用CorelDRAW排钻如何把圈摆均匀

    服装设计一直都是一个很火热的行业,也是一个比较高端的行业,随着时代的步伐,以前的人都是用手绘的方式来设计服装,现在不一样了,电脑可以说普及到了每一个家庭,让软件以更快的速度,更准确的数据来设计服装中的 ...

  5. BZOJ 2276: [Poi2011]Temperature 单调队列

    Code: #include<bits/stdc++.h> #define maxn 3000000 using namespace std; void setIO(string s) { ...

  6. sysbench_mysql

    ref http://seanlook.com/2016/03/28/mysql-sysbench/ 测试 当执行下面这个sysbench测试mysql的时候,你不知道的可能可能是: 这到底是在测试读 ...

  7. CentOS安装Docker-ce并配置国内镜像

    前提条件 1.系统.内核 CentOS7 要求64位系统.内核版本3.10以上 CentOS6 要求版本在6.5以上,系统64位.内核版本2.6.32-431以上 查看内核版本号 uname -r # ...

  8. HDU3336Count the string

    HDU3336Count the string Problem Description It is well known that AekdyCoin is good at string proble ...

  9. Luogu P1187 3D模型

    题目描述 一座城市建立在规则的n×m网格上,并且网格均由1×1正方形构成.在每个网格上都可以有一个建筑,建筑由若干个1×1×1的立方体搭建而成(也就是所有建筑的底部都在同一平面上的).几个典型的城市模 ...

  10. Ajax基本写法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...