温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-10-10 17:57:26 来源:脚本之家 阅读:218 作者:彬菌 栏目:编程语言

我们先来看下运行效果图

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

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();
    }
  }
}

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI