zoukankan      html  css  js  c++  java
  • [开发笔记]winfom ListBox控件选中项上下移动排序

    实现ListBox控件选中项上下移动重新排序功能

    效果图:

    移动后效果:

    代码:

    /// <summary>
            /// 上移选中项
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                //http://www.cnblogs.com/babycool 酷小孩
                //获取集合中项的数量
                int lbxlength = this.listBox1.Items.Count;
                //选中项的索引
                int isselected = this.listBox1.SelectedIndex;
    
                if (lbxlength > isselected && isselected > 0)
                {
                    object SelectItem = this.listBox1.SelectedItem;
                    this.listBox1.Items.RemoveAt(isselected);
                    this.listBox1.Items.Insert(isselected - 1, SelectItem);
                    this.listBox1.SelectedIndex = isselected - 1;
                }
    
            }
    
            /// <summary>
            /// 下移选中项
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {
                int lbxlength = this.listBox1.Items.Count;
                int isselected = this.listBox1.SelectedIndex;
                if (lbxlength > isselected && isselected < lbxlength - 1)
                {
                    object selectItem = this.listBox1.SelectedItem;
                    this.listBox1.Items.RemoveAt(isselected);
                    this.listBox1.Items.Insert(isselected + 1, selectItem);
                    this.listBox1.SelectedIndex = isselected + 1;
                }
    
    
            }
    
            //遍历每一项
            private void button3_Click(object sender, EventArgs e)
            {
    
                int count = listBox1.Items.Count;
                for (int i = 0; i < count; i++)
                {
                    MessageBox.Show(listBox1.Items[i].ToString());
                }
            }


    转载请注明出处。

  • 相关阅读:
    Dockerfile
    最近遇到的jsfl开发问题总结
    【Distributed】大型网站高并发和高可用
    【Distributed】CDN
    【Distributed】限流技巧
    【Java并发】锁机制
    【Java并发】线程通信
    【Java并发】线程安全和内存模型
    【Java并发】基础
    【Redis】基本数据类型及命令操作(超详细)
  • 原文地址:https://www.cnblogs.com/babycool/p/3112753.html
Copyright © 2011-2022 走看看