ListBox相互移动

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using test1.ItemBase;      //添加引用
using System.Data.SqlClient;

namespace test1
{
    public partial class listBoxTest : Form
    {
        public listBoxTest()
        {
            InitializeComponent();
        }

        SqlBaseClass G_SqlClass = new SqlBaseClass();  //声明数据库操作类的对象

        private void listBoxTest_Load(object sender, EventArgs e)
        {
            string cmdText = "select ROLES_ID,ROLESNAME from TBL_USER_ROLES,TBL_ROLES where USER_ID=2 and  TBL_USER_ROLES.ROLES_ID=TBL_ROLES.ID";
            SqlDataReader dr = G_SqlClass.GetReader(cmdText);
            while (dr.Read())
            {
                string listItem = dr[0] + "." + dr[1];
                this.listBox1.Items.Add(listItem);
            }
            dr.Close();


            string cmdText1 = "select ID,ROLESNAME from TBL_ROLES where ID NOT IN (select ROLES_ID from TBL_USER_ROLES where USER_ID=2)";
            SqlDataReader dr1 = G_SqlClass.GetReader(cmdText1);
            while (dr1.Read())
            {
                string listItem1 = dr1[0] + "." + dr1[1];
                this.listBox2.Items.Add(listItem1);
            }
            dr.Close();
        }

        private void btn_moveRight_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedItem != null)
            {
                this.listBox2.Items.Add(this.listBox1.SelectedItem);
                this.listBox1.Items.Remove(this.listBox1.SelectedItem);
            }
        }

        private void btn_moveLeft_Click(object sender, EventArgs e)
        {
            if (this.listBox2.SelectedItem != null)
            {
                this.listBox1.Items.Add(this.listBox2.SelectedItem);
                this.listBox2.Items.Remove(this.listBox2.SelectedItem);
            }
        }

        private void btn_confirm_Click(object sender, EventArgs e)
        {
            string cmdText = "delete from TBL_USER_ROLES where USER_ID=2";
            G_SqlClass.GetExecute(cmdText);
            bool flag = false;
            for (int i = 0; i < this.listBox1.Items.Count; i++)
            {
                int itemCode = Convert.ToInt32(this.listBox1.Items[i].ToString().Substring(0, 1));
                string cmdText1 = "insert into TBL_USER_ROLES(USER_ID,ROLES_ID) values('2','" + itemCode + "')";
                flag=G_SqlClass.GetExecute(cmdText1);

            }
            if (flag == true)
            {
                MessageBox.Show("权限设置成功");
            }           
        }
    }
}

你可能感兴趣的:(windows)