C# winform listBox实例

listBox实例,自己备忘一下:

C# winform listBox实例_第1张图片

public partial class frmUserSelect : Form
    {
        public frmUserSelect()
        {
            InitializeComponent();
        }

        private void frmUserSelect_Load(object sender, EventArgs e)
        {
            string[] userNames = new string[3] { "张三", "李四", "王五" };
            listBox1.Items.Add(userNames[0]);
            listBox1.Items.Add(userNames[1]);
            listBox1.Items.Add(userNames[2]);
        }       

        private void bbtnAllRight_Click(object sender, EventArgs e)//全部向右
        {
            if (listBox1.Items.Count == 0)
                return;
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                listBox2.Items.Add(listBox1.Items[i]);
            }

            for (int j = 0; j < listBox2.Items.Count; j++)
            {
                listBox1.Items.Remove(listBox2.Items[j]);
            }  
        }
        private void btnAllLeft_Click(object sender, EventArgs e)//全部向左
        {
            if (listBox2.Items.Count == 0)
                return;
            for (int i = 0; i < listBox2.Items.Count; i++)
            {
                listBox1.Items.Add(listBox2.Items[i]);
            }

            for (int j = 0; j < listBox1.Items.Count; j++)
            {
                listBox2.Items.Remove(listBox1.Items[j]);
            }
        }
        private void btnLeft_One_Click(object sender, EventArgs e)//选中向左(单个)
        {
            if (listBox2.Items.Count == 0)
                return;
            if (listBox2.SelectedItem == null)
                return;
            listBox1.Items.Add(listBox2.SelectedItem);
            listBox2.Items.Remove(listBox2.SelectedItem);
        }
        private void btnRight_One_Click(object sender, EventArgs e)//选中向右(单个)
        {
            if (listBox1.Items.Count == 0)
                return;
            if (listBox1.SelectedItem == null)
                return;
            listBox2.Items.Add(listBox1.SelectedItem);
            listBox1.Items.Remove(listBox1.SelectedItem);
        }
        private void btnSelectLeft_Click(object sender, EventArgs e)//选中向左
        {
            if (listBox2.Items.Count == 0)
                return;
            for (int i = 0; i < listBox2.SelectedItems.Count; i++)
            {
                listBox1.Items.Add(listBox2.SelectedItems[i]);
            }

            for (int j = 0; j < listBox1.Items.Count; j++)
            {
                listBox2.Items.Remove(listBox1.Items[j]);
            }
        }
        private void btnSelectRight_Click(object sender, EventArgs e)//选中向右
        {
            if (listBox1.Items.Count == 0)
                return;
            for (int i = 0; i < listBox1.SelectedItems.Count; i++)
            {
                listBox2.Items.Add(listBox1.SelectedItems[i]);
            }

            for (int j = 0; j < listBox2.Items.Count; j++)
            {
                listBox1.Items.Remove(listBox2.Items[j]);
            }
        }

   
    }

 

你可能感兴趣的:(Csharp)