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


    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涉及其事件时,就要加条件!

  • 相关阅读:
    7. Spring验证、数据绑定和类型转换
    J2EE应用与移动互联网-写在前头
    IT基础设施资源的实践----写在前头
    JavaScript随笔记(一)基础概念以及变量类型
    js函数表达式
    js面形对象(2)
    js面向对象
    viPlugin安装破解
    Ubuntu12.04 使用中遇到的问题
    关于sizeof
  • 原文地址:https://www.cnblogs.com/meimao5211/p/3346318.html
Copyright © 2020-2023  润新知