您的位置:首页 > 编程语言 > C#

C#WinFormListBox数据项的上下移动

2015-09-25 20:45 399 查看
 private void button1_Click(object sender, EventArgs e)

        {

            int lbxLength = this.listBox1.Items.Count;//listbox的长度   

            int iselect = this.listBox1.SelectedIndex;//listbox选择的索引   

            if (lbxLength > iselect && iselect > 0)

            {

                object oTempItem = this.listBox1.SelectedItem;

                this.listBox1.Items.RemoveAt(iselect);

                this.listBox1.Items.Insert(iselect - 1, oTempItem);

                this.listBox1.SelectedIndex = iselect - 1;

            }   

        }

        private void button2_Click(object sender, EventArgs e)

        {

            int lbxLength = this.listBox1.Items.Count;//listbox的长度   

            int iselect = this.listBox1.SelectedIndex;//listbox选择的索引   

            if (iselect <lbxLength-1)

            {

                object oTempItem = this.listBox1.SelectedItem;

                this.listBox1.Items.RemoveAt(iselect);

                this.listBox1.Items.Insert(iselect + 1, oTempItem);

                this.listBox1.SelectedIndex = iselect + 1;

            }   

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: