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

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

(0)

相关推荐

  • C# 文件拖拽和pixturBox缩放与拖拽功能

    文件拖拽: 效果:将一个文件拖拽到窗体的某个控件时,将该控件的路径显示在该控件上,只要拿到了路径自然可以读取文件中的内容了. 将一个控件的属性AllowDrop设置为true,然后添加DragDrop.DragEnter时间处理函数,如下: private void txtAppPath_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.Fi

  • C#组件FormDragger窗体拖拽器详解

    适用:.net2.0+ winform项目 介绍: 类似QQ.迅雷等讲究UI体验的软件,都支持在窗口内多处地方拖动窗口,而不必老实巴交的去顶部标题栏拖,这个组件就是让winform也能这样随性拖拽,随性度或更甚.先看效果: 可拖拽的地方包括不限于: 窗体.Panel.GroupBox.TabControl等容器控件的空白区: 菜单栏.工具栏.状态栏等bar的空白区,以及无效项目: Label.PictureBox.ProgressBar等通常不与鼠标交互的控件: 一切无效控件(Enabled为f

  • C#实现TreeView节点拖拽的方法

    本文实例讲述了C#实现TreeView节点拖拽的方法.分享给大家供大家参考.具体如下: public Form1() { InitializeComponent(); treeView1.AllowDrop = true; treeView1.ItemDrag += new ItemDragEventHandler(treeView1_ItemDrag); treeView1.DragEnter += new DragEventHandler(treeView1_DragEnter); tree

  • C#利用控件拖拽技术制作拼图游戏

    主要实现的功能: 1.程序附带多张拼图随机拼图. 2.可手动添加拼图. 3.游戏成功判断. 4.30秒超时判断. Puzzle.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms

  • C# 实现的图片盖章功能,支持拖拽、旋转、放缩、保存

    实现图片盖章功能,在图片上点击,增加"图章"小图片,可以拖拽"图章"到任意位置,也可以点击图章右下角园框,令图片跟着鼠标旋转和放缩. 操作方法:1.点击增加"图章"2.选中移动图标3.点中右下角放缩旋转图章. 效果图: 实现代码如下: 1.  窗口Xaml代码 复制代码 代码如下: <Window x:Class="Lenovo.YogaPaster.ImageEditWindow"    xmlns="htt

  • C#实现Dev Grid拖拽移动行的方法

    本文实例讲述了C#实现Dev Grid拖拽移动行的方法.分享给大家供大家参考.具体如下: 完整实例代码点击此处本站下载.     拖拽时带行截图效果 实现代码如下: /// <summary> /// 拖拽帮助类 /// </summary> public static class DragHelper { /// <summary> /// BandedGridView 拖拽 /// </summary> /// <param name="

  • C#控件Picturebox实现鼠标拖拽功能

    工作需要,要在一个Form里面实现一个实时预览的picturebox图像,由于picturebox可能会遮挡到其后面的画面,所以要求picturebox可以由用户自行拖拽,类似于悬浮框. 原理说明 在网上兜了几圈之后,决定用三段代码实现.首先要注册鼠标的三个事件:移动.鼠标左键按下.鼠标左键抬起,当然,都是在picturebox上的动作.注册三个事件后,即可以在三个对应的事件函数里面实现拖拽. 代码实例 首先在窗体设计器生成的代码里面注册picturebox的鼠标事件.注:网上说当你添加pict

  • UGUI实现图片拖拽功能

    这一篇博客我们来使用UGUI实现图片的拖拽功能. 说到拖拽,那必然离不开坐标,UGUI 的坐标有点不一样,它有两种坐标,一种是屏幕坐标,还有一种就是 UI 在Canvas内的坐标(暂时叫做ugui坐标.),这两个坐标是不一样的,所以拖拽是就需要转换.因为鼠标的移动是屏幕坐标,而 UI 的移动是ugui坐标.转换的方法: RectTransformUtility.ScreenPointToLocalPointInRectangle ( - );这个方法可以把屏幕坐标转换成 ugui 坐标.至于屏幕

  • C#控件picturebox实现图像拖拽和缩放

    本文实例为大家分享了C# picturebox实现图像拖拽和缩放的具体代码,供大家参考,具体内容如下 1.核心步骤: ①新建Point类型全局变量mouseDownPoint,记录拖拽过程中鼠标位置: ②MouseDown事件记录Cursor位置: ③MouseMove事件计算移动矢量,并更新pictureBox1.Location. 代码: private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.B

  • 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

  • Android中在GridView网格视图上实现item拖拽交换的方法

    GridView基础 新建一个HelloGridView的工程 修改main.xml代码如下: <?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width=&q

  • android RecyclerView实现条目Item拖拽排序与滑动删除

    效果演示 需求和技术分析 RecyclerView Item拖拽排序::长按RecyclerView的Item或者触摸Item的某个按钮. RecyclerView Item滑动删除:RecyclerView Item滑动删除:RecyclerView的Item滑动删除. 实现方案与技术 利用ItemTouchHelper绑定RecyclerView.ItemTouchHelper.Callback来实现UI更新,并且实现动态控制是否开启拖拽功能和滑动删除功能. 实现步骤 继承抽象类ItemTo

  • Delphi实现Listbox中的item根据内容显示不同颜色的方法

    本文简述了Delphi实现Listbox中的item根据内容显示不同颜色的方法,实现步骤如下: ListBox1 的 Style 属性改为 lbOwnerDrawVariable 在ListBox的OnDrawItem事件裡,根据item的值,改变Canvas属性 示例代码如下: procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);

  • Android RecycleView实现Item拖拽效果

    基于公司产品的优化需求,其中一个需求涉及到RecycleView的拖拽,以及拖拽后item位置的持久化,目的是可以用户自定义界面偏好,并在用户下次进入本界面后,之前设置的偏好仍然有效.我写了一个小Demo用作演示效果. 先看效果(只看效果,不看颜值) 步骤1.建接口文件ItemTouchHelperViewHolder,该接口文件中描述的是选中和放开当前Item调用的方法. public interface ItemTouchHelperViewHolder { void onItemSelec

  • WPF中鼠标/键盘/拖拽事件以及用行为封装事件详解

    目录 鼠标事件 键盘输入事件 拖拽事件 用行为封装事件 用事件来实现 用行为来封装 本文主要介绍了WPF中常用的鼠标事件.键盘事件以及注意事项,同时使用一个案例讲解了拓展事件.除此之外,本文还讲述如何用行为(Behavior)来封装事件. Windows中的事件通过消息机制来完成,也就是Windows系统来捕获用户输入(如鼠标点击.键盘输入),然后Windows发送一个消息给应用程序,应用程序进行具体的处理.在Winform中,窗体中每个控件都是有独立的句柄,也就是每个控件都可以收到Window

  • vue中的可拖拽宽度div的实现示例

    主要思路 在需要拖拽宽度的区域设置一个div,高度设为 100%,宽度尽量窄一些(也不要太窄,3-6px左右) 在此div上绑定当“鼠标按下”时,触发document绑定“鼠标移动”方法和"鼠标抬起"方法 通过鼠标移动方法不断获取当前鼠标位置,设置需要变化大小div的宽高. 鼠标抬起时取消鼠标移动方法和鼠标抬起方法的绑定. <template>   <div class="container" id="content_box"&

  • Java实现JS中的escape和UNescape代码分享

    众所周知,JavaScript中escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串.下面,我们就来看看 Java语言中类似JavaScript中的escape() 和unescape() 转码方法,具体代码如下: public class EscapeUnescape { public static String escape(String src) { int i; char j; StringBuffer tmp = new StringBuffer(); tm

  • Python中pygal绘制雷达图代码分享

    pygal的安装和简介,大家可以参阅<pip和pygal的安装实例教程>,下面看看通过pygal实现绘制雷达图代码示例. 雷达图(Radar): import pygal radar_chart = pygal.Radar() radar_chart.title = 'V8 benchmark results' radar_chart.x_labels = ['Richards', 'DeltaBlue', 'Crypto', 'RayTrace', 'EarleyBoyer', 'RegEx

  • Extjs4中tree的拖拽功能(可以两棵树之间拖拽) 简单实例

    复制代码 代码如下: //只要要在treepanel中配置一下viewConfig即可,如果是两棵树之间,两棵树都要配置    viewConfig:{   plugins :{    ptype:'treeviewdragdrop',    appendOnly:true   //只能拖着带非叶节点上   },   listeners:{//监听器    drop:function(node,data,overModel,dropPosition,options){     //ajax的操作

随机推荐