C#列表框拖放行

C#列表框拖放行,c#,drag-and-drop,listbox,C#,Drag And Drop,Listbox,我试图用一个列表框实现一个C#拖放行重新排序。我在网上遇到过一些代码片段,但似乎没有一个能满足我的需要 我希望您向我展示一个如何在ListBox中移动行的示例代码 谢谢 行的移动在方法“listBox1_MouseUp和listBox1_MouseDown”中完成 使用视图代码更改此设置: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using Sy

我试图用一个列表框实现一个C#拖放行重新排序。我在网上遇到过一些代码片段,但似乎没有一个能满足我的需要


我希望您向我展示一个如何在ListBox中移动行的示例代码


谢谢

行的移动在方法“listBox1_MouseUp和listBox1_MouseDown”中完成

使用视图代码更改此设置:

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;

namespace Calculator
{
    public partial class ComponentMover : Form
    {
        private Control trackedControl;
        private int gridWidth = 100, gridHeight = 20;
        private int row;

        public ComponentMover()
        {
            this.InitializeComponent();
            this.InitializeDynamic();
        }

        void draggable_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (this.trackedControl == null)
                this.trackedControl = (Control)sender;
        }

        void draggable_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (this.trackedControl != null)
            {
                int x = e.X + this.trackedControl.Location.X;
                int y = e.Y + this.trackedControl.Location.Y;
                Point moved = new Point(x - x % this.gridWidth, y - y % this.gridHeight);

                Console.WriteLine(e.X + ", " + e.Y + ", " + ", " + moved.X + ", " + moved.Y);
                if (moved != this.trackedControl.Location)
                    this.trackedControl.Location = moved;
            }
        }

        void draggable_MouseUp(object sender, MouseEventArgs e)
        {
            this.trackedControl = null;
        }

        private void AddDragListeners(Control draggable)
        {
            draggable.MouseDown += new MouseEventHandler(draggable_MouseDown);
            draggable.MouseMove += new MouseEventHandler(draggable_MouseMove);
            draggable.MouseUp += new MouseEventHandler(draggable_MouseUp);
        }
    }
}
设计器代码:

namespace Calculator
{
    // Designer code.
    partial class ComponentMover
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeDynamic()
        {
            this.AddDragListeners(button1);
            this.AddDragListeners(button4);
            this.AddDragListeners(domainUpDown1);
            this.AddDragListeners(textBox1);
            this.AddDragListeners(checkBox1);
            this.AddDragListeners(radioButton1);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.domainUpDown1 = new System.Windows.Forms.DomainUpDown();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(13, 13);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(177, 43);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 3;
            this.button4.Text = "button4";
            this.button4.UseVisualStyleBackColor = true;
            // 
            // domainUpDown1
            // 
            this.domainUpDown1.Location = new System.Drawing.Point(95, 42);
            this.domainUpDown1.Name = "domainUpDown1";
            this.domainUpDown1.Size = new System.Drawing.Size(74, 20);
            this.domainUpDown1.TabIndex = 4;
            this.domainUpDown1.Text = "domainUpDown1";
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(177, 72);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 5;
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new System.Drawing.Point(281, 13);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(80, 17);
            this.checkBox1.TabIndex = 6;
            this.checkBox1.Text = "checkBox1";
            this.checkBox1.UseVisualStyleBackColor = true;
            // 
            // radioButton1
            // 
            this.radioButton1.AutoSize = true;
            this.radioButton1.Location = new System.Drawing.Point(366, 42);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(85, 17);
            this.radioButton1.TabIndex = 7;
            this.radioButton1.TabStop = true;
            this.radioButton1.Text = "radioButton1";
            this.radioButton1.UseVisualStyleBackColor = true;
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Items.AddRange(new object[] {
            "word1",
            "word2",
            "word3"});
            this.listBox1.Location = new System.Drawing.Point(13, 42);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(76, 82);
            this.listBox1.TabIndex = 8;
            this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(listBox1_MouseDown);
            this.listBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(listBox1_MouseUp);
            // 
            // ComponentMover
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(485, 159);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.radioButton1);
            this.Controls.Add(this.checkBox1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.domainUpDown1);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button1);
            this.Name = "ComponentMover";
            this.Text = "ComponentMover";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        void listBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            System.Windows.Forms.ListBox list = (System.Windows.Forms.ListBox)sender;
            int swap = list.SelectedIndex;

            if(swap != this.row)
            {
                string temp = (string)list.Items[this.row];
                list.Items[this.row] = list.Items[swap];
                list.Items[swap] = temp;
            }
        }

        void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            this.row = ((System.Windows.Forms.ListBox)sender).SelectedIndex;
        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.DomainUpDown domainUpDown1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.CheckBox checkBox1;
        private System.Windows.Forms.RadioButton radioButton1;
        private System.Windows.Forms.ListBox listBox1;
    }
}
名称空间计算器
{
//设计器代码。
部分类组件移动器
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
private void InitializeDynamic()
{
这是一个新的按钮;
此按钮。添加反光器(按钮4);
这是AddDragListeners(domainUpDown1);
这是AddDragListeners(textBox1);
这是一种新的设计方法(复选框1);
这是一种新的发光器(radioButton1);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.button1=new System.Windows.Forms.Button();
this.button4=new System.Windows.Forms.Button();
this.domainUpDown1=new System.Windows.Forms.DomainUpDown();
this.textBox1=new System.Windows.Forms.TextBox();
this.checkBox1=new System.Windows.Forms.CheckBox();
this.radioButton1=新系统.Windows.Forms.RadioButton();
this.listBox1=new System.Windows.Forms.ListBox();
这个.SuspendLayout();
// 
//按钮1
// 
this.button1.Location=新系统图纸点(13,13);
this.button1.Name=“button1”;
this.button1.Size=新系统图纸尺寸(75,23);
this.button1.TabIndex=0;
this.button1.Text=“button1”;
this.button1.UseVisualStyleBackColor=true;
// 
//按钮4
// 
this.button4.Location=新系统图纸点(177,43);
this.button4.Name=“button4”;
this.button4.Size=新系统图纸尺寸(75,23);
this.button4.TabIndex=3;
this.button4.Text=“button4”;
this.button4.UseVisualStyleBackColor=true;
// 
//域UpDown1
// 
this.domainUpDown1.Location=新系统图点(95,42);
this.domainUpDown1.Name=“domainUpDown1”;
this.domainUpDown1.Size=新系统.Drawing.Size(74,20);
this.domainUpDown1.TabIndex=4;
this.domainUpDown1.Text=“domainUpDown1”;
// 
//文本框1
// 
this.textBox1.Location=新系统.图纸.点(177,72);
this.textBox1.Name=“textBox1”;
this.textBox1.Size=新系统.Drawing.Size(100,20);
this.textBox1.TabIndex=5;
// 
//复选框1
// 
this.checkBox1.AutoSize=true;
this.checkBox1.Location=新系统图纸点(281,13);
this.checkBox1.Name=“checkBox1”;
this.checkBox1.Size=新系统图纸尺寸(80,17);
this.checkBox1.TabIndex=6;
this.checkBox1.Text=“checkBox1”;
this.checkBox1.UseVisualStyleBackColor=true;
// 
//无线电按钮1
// 
this.radioButton1.AutoSize=true;
this.radioButton1.Location=新系统图纸点(366,42);
this.radioButton1.Name=“radioButton1”;
this.radioButton1.Size=新系统图纸尺寸(85,17);
this.radioButton1.TabIndex=7;
this.radioButton1.TabStop=true;
this.radioButton1.Text=“radioButton1”;
this.radioButton1.UseVisualStyleBackColor=true;
// 
//列表框1
// 
this.listBox1.FormattingEnabled=true;
this.listBox1.Items.AddRange(新对象[]){
“字1”,
“字2”,
“word3”});
this.listBox1.Location=新系统.图纸.点(13,42);
this.listBox1.Name=“listBox1”;
this.listBox1.Size=新系统图尺寸(76,82);
this.listBox1.TabIndex=8;
this.listBox1.MouseDown+=new System.Windows.Forms.MouseEventHandler(listBox1\u MouseDown);
this.listBox1.MouseUp+=new System.Windows.Forms.MouseEventHandler(listbox1u MouseUp);
// 
//组件移动器
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统图纸尺寸(485,159);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.domainUpDown1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button1);
this.Name=“ComponentMover”;
this.Text=“ComponentMover”;
此选项为.resume布局(false);
这个。执行布局();
}
void listBox1_MouseUp(对象发送方,System.Windows.Forms.MouseEventArgs e)
{