欢迎来到入门教程网!

C#教程

当前位置:主页 > 软件编程 > C#教程 >

C# ListBox中的Item拖拽代码分享

来源:本站原创|时间:2020-01-10|栏目:C#教程|点击:

我们先来看下运行效果图

Form1.cs代码:

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

namespace MoveItem
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    ArrayList list = new ArrayList();
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {
      for (int i = 1; i <= 10; i++)
      {
        list.Add(i);
        string s = i.ToString();
        listBox1.Items.Add(s);
      }

    }

    private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
      
    }

    private void button1_Click(object sender, EventArgs e)
    {
      //单选,无法实现多选
      //string str = this.listBox1.Text.Trim().ToString();
      //if (listBox1.Items.Contains(str))
      //{
      //  listBox1.Items.Remove(str);
      //  listBox2.Items.Add(str);
      //}
      for (int i=0;i<listBox1.SelectedIndices.Count;i++)
      {
        listBox2.Items.Add(listBox1.Items[listBox1.SelectedIndices[i]]);
        listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
        i--;
      }
    }

    private void button2_Click(object sender, EventArgs e)
    {
      for (int i = 0; i < listBox2.SelectedIndices.Count; i++)
      {
        listBox1.Items.Add(listBox2.Items[listBox2.SelectedIndices[i]]);
        listBox2.Items.RemoveAt(listBox2.SelectedIndices[i]);
        i--;
      }
    }

    private void button3_Click(object sender, EventArgs e)
    {
      listBox2.Items.AddRange(listBox1.Items);
      listBox1.Items.Clear();
    }

    private void button4_Click(object sender, EventArgs e)
    {
      listBox1.Items.AddRange(listBox2.Items);
      listBox2.Items.Clear();
    }
  }
}

大家可以测试运行下,有其他问题可以在下方的留言区讨论,感谢大家对我们的支持。

上一篇:C#客户端程序调用外部程序的3种实现方法

栏    目:C#教程

下一篇:winform实现可拖动的自定义Label控件

本文标题:C# ListBox中的Item拖拽代码分享

本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5219.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有