16,554
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication596
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#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.listBox1 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(48, 32);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(192, 328);
this.listBox1.TabIndex = 1;
this.listBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseDown);
this.listBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.listBox1_MouseMove);
//
// listBox2
//
this.listBox2.AllowDrop = true;
this.listBox2.ItemHeight = 12;
this.listBox2.Location = new System.Drawing.Point(288, 32);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(192, 328);
this.listBox2.TabIndex = 2;
this.listBox2.DragOver += new System.Windows.Forms.DragEventHandler(this.listBox2_DragOver);
this.listBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox2_DragDrop);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 405);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e) {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0)
listBox1.Items.Add("Item " + i.ToString());
else
listBox2.Items.Add("Item " + i.ToString());
}
}
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
Point p = Point.Empty;
private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
ListBox c = sender as ListBox;
p = new Point(e.X, e.Y);
int selectedIndex = c.IndexFromPoint(p);
if (selectedIndex == -1)
p = Point.Empty;
}
private void listBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
if ((p != Point.Empty) && ((Math.Abs(e.X - p.X) > 5) || (Math.Abs(e.Y - p.Y) > 5)))
listBox1.DoDragDrop(sender, DragDropEffects.Move);
}
private void listBox2_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void listBox2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
ListBox listBox = sender as ListBox;
Point newPoint = new Point(e.X, e.Y);
newPoint = listBox.PointToClient(newPoint);
int selectedIndex = listBox.IndexFromPoint(newPoint);
object item = listBox1.Items[listBox1.IndexFromPoint(p)];
if (selectedIndex == -1)
listBox.Items.Add(item);
else
listBox.Items.Insert(selectedIndex, item);
}
}
}