宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

实现选项的选择(单选,多选,全选(移除))

初始化ListBox

protectedvoid InitListBox()

        {

            ArrayList arrlist = newArrayList();

            arrlist.Add("星期一");

            arrlist.Add("星期二");

            arrlist.Add("星期三");

            arrlist.Add("星期四");

            arrlist.Add("星期五");

            arrlist.Add("星期六");

            arrlist.Add("星期日");

            LBoxSourseWeek.DataSource = arrlist;

            LBoxSourseWeek.DataBind();

 

        }

选择所有

protectedvoid btnAddAll_Click(object sender, EventArgs e)

        {

            int count = LBoxSourseWeek.Items.Count;

            int index = 0;

            //循环移动,( 每次都移动最新的第一个)

            for (int i = 0; i < count; i++)

            {

                ListItem item = LBoxSourseWeek.Items[index];

                LBoxSourseWeek.Items.Remove(item);

                LBoxDestWeek.Items.Add(item);

            }

            index++;

        }

//移除所有

        protectedvoid btnDelAll_Click(object sender, EventArgs e)

        {

            //全部从目标listbox一出(法一)

            int count = LBoxDestWeek.Items.Count;

            int index = 0;

            //循环移动,( 每次都移动最新的第一个)

            for (int i = 0; i < count; i++)

            {

                ListItem item = LBoxDestWeek.Items[index];

                LBoxDestWeek.Items.Remove(item);

                LBoxSourseWeek.Items.Add(item);

            }

            index++;

            //重新初始化控件(法二)

            //InitListBox();

            //LBoxDestWeek.Items.Clear();

        }

选择一个或者多个

        protectedvoid btnAddSel_Click(object sender, EventArgs e)

        {

            //支持选中多个的移动

            int count = LBoxSourseWeek.Items.Count;

            int index = 0;

            //循环,先判断选中,再移动,( 每次都移动最新的第一个)

            for (int i = 0; i < count; i++)

            {

                ListItem item = LBoxSourseWeek.Items[index];

                if (LBoxSourseWeek.Items[index].Selected == true)

                {

                    LBoxSourseWeek.Items.Remove(item);

                    LBoxDestWeek.Items.Add(item);

                    //将当前索引值减1

                    index–;

                }

                //获取下一个索引值

                index++;

            }

            index++;

        }

移除一个或者多个

        protectedvoid btnDelSel_Click(object sender, EventArgs e)

        {

            int count = LBoxDestWeek.Items.Count;

            int index = 0;

            //循环,先判断选中,再移动,( 每次都移动最新的第一个)

            for (int i = 0; i < count; i++)

            {

                ListItem item = LBoxDestWeek.Items[index];

                if (LBoxDestWeek.Items[index].Selected == true)

                {

                    LBoxDestWeek.Items.Remove(item);

                    LBoxSourseWeek.Items.Add(item);

                    //将当前索引值减1

                    index–;

                }

                //获取下一个索引值

                index++;

            }

            index++;

        }

将选择的选项向上移动一次

protectedvoid btnUpOne_Click(object sender, EventArgs e)

        {

            if (LBoxSourseWeek.SelectedIndex > 0 && LBoxSourseWeek.SelectedIndex <= LBoxSourseWeek.Items.Count – 1)

            {

                //记录当前选项的值

                string name = LBoxSourseWeek.SelectedItem.Text;

                string value = LBoxSourseWeek.SelectedItem.Text;

                //获取当前选项的索引号

                int index = LBoxSourseWeek.SelectedIndex;

                //交换当前选项和其前一项的索引号

                LBoxSourseWeek.SelectedItem.Text = LBoxSourseWeek.Items[index – 1].Text;

                LBoxSourseWeek.SelectedItem.Value = LBoxSourseWeek.Items[index – 1].Value;

                LBoxSourseWeek.Items[index – 1].Text = name;

                LBoxSourseWeek.Items[index – 1].Value = value;

                //设定上一行为选中行

                LBoxSourseWeek.SelectedIndex–;

            }

            else

            {

                //记录当前选项的值

                string name = LBoxSourseWeek.SelectedItem.Text;

                string value = LBoxSourseWeek.SelectedItem.Text;

                //获取当前选项的索引号

                int index = LBoxSourseWeek.SelectedIndex;

                //交换当前选项和其前一项的索引号

                LBoxSourseWeek.SelectedItem.Text = LBoxSourseWeek.Items[LBoxSourseWeek.Items.Count – 1].Text;

                LBoxSourseWeek.SelectedItem.Value = LBoxSourseWeek.Items[LBoxSourseWeek.Items.Count – 1].Value;

                LBoxSourseWeek.Items[LBoxSourseWeek.Items.Count – 1].Text = name;

                LBoxSourseWeek.Items[LBoxSourseWeek.Items.Count – 1].Value = value;

                //设定上一行为选中行

                LBoxSourseWeek.SelectedIndex = LBoxSourseWeek.Items.Count – 1;

            }

        }

将选择的选项向下移动一次

        protectedvoid btnDownOne_Click(object sender, EventArgs e)

        {

            if (LBoxSourseWeek.SelectedIndex >= 0 && LBoxSourseWeek.SelectedIndex  < LBoxSourseWeek.Items.Count – 1)

            {

                //记录当前选项的值

                string name = LBoxSourseWeek.SelectedItem.Text;

                string value = LBoxSourseWeek.SelectedItem.Text;

                //获取当前选项的索引号

                int index = LBoxSourseWeek.SelectedIndex;

                //交换当前选项和其前一项的索引号

                LBoxSourseWeek.SelectedItem.Text = LBoxSourseWeek.Items[index + 1].Text;

                LBoxSourseWeek.SelectedItem.Value = LBoxSourseWeek.Items[index + 1].Value;

                LBoxSourseWeek.Items[index + 1].Text = name;

                LBoxSourseWeek.Items[index + 1].Value = value;

                //设定上一行为选中行

                LBoxSourseWeek.SelectedIndex++;

            }

            else

            {

                //记录当前选项的值

                string name = LBoxSourseWeek.SelectedItem.Text;

                string value = LBoxSourseWeek.SelectedItem.Text;

                //获取当前选项的索引号

                int index = LBoxSourseWeek.SelectedIndex;

                //交换当前选项和其前一项的索引号

                LBoxSourseWeek.SelectedItem.Text = LBoxSourseWeek.Items[0].Text;

                LBoxSourseWeek.SelectedItem.Value = LBoxSourseWeek.Items[0].Value;

                LBoxSourseWeek.Items[0].Text = name;

                LBoxSourseWeek.Items[0].Value = value;

                //设定上一行为选中行

                LBoxSourseWeek.SelectedIndex = 0;

            }

        }