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

作者:彬菌 时间:2022-04-12 14:06:41 

我们先来看下运行效果图

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

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

来源:https://www.idaobin.com/archives/967.html

标签:C#,ListBox,Item
0
投稿

猜你喜欢

  • IDEA最新版2020.1的maven工程本地依赖仓库无法使用问题(已解决)

    2023-09-21 17:57:00
  • Spring Cache框架应用介绍

    2023-06-15 22:32:59
  • Java SQL注入案例教程及html基础入门

    2023-09-22 08:34:12
  • Android编程显示网络上的图片实例详解

    2021-11-29 05:21:10
  • Java多线程之Worker Thread模式

    2021-06-11 12:11:51
  • Spring5中的WebClient使用方法详解

    2023-08-05 14:50:24
  • 解决bufferedReader.readLine()读到最后发生阻塞的问题

    2022-07-15 06:05:38
  • SpringMVC框架post提交数据库出现乱码解决方案

    2022-03-01 09:50:41
  • java统计字符串中重复字符出现次数的方法

    2022-02-15 00:52:16
  • 使用Nexus搭建Maven私服教程的方法步骤

    2023-03-05 18:56:58
  • Android应用内悬浮窗Activity的简单实现

    2023-07-28 04:03:00
  • 利用Spring Data MongoDB持久化文档数据的方法教程

    2023-05-05 02:36:54
  • Java中实现获取路径的方法汇总

    2022-12-19 08:10:54
  • java实现文件变化监控的方法(推荐)

    2023-11-08 01:18:26
  • 一篇文章彻底弄懂Java中二叉树

    2023-04-08 12:47:30
  • C#同步和异步调用方法实例

    2022-09-11 21:20:50
  • Java中的notyfy()和notifyAll()的本质区别

    2022-06-05 22:46:19
  • IDEA导入Eclipse项目的方法步骤(图文教程)

    2023-07-17 09:45:09
  • 详解Mybatis的二级缓存配置

    2023-03-20 10:48:37
  • Java类加载初始化的过程及顺序

    2021-12-09 16:12:46
  • asp之家 软件编程 m.aspxhome.com