问个关于托拽的问题

anycall2004 2009-03-08 08:56:30
我有2个winform 想实现跨form托拽
请问该如何实现呢? 最好有例子
谢谢
...全文
96 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
abcfy2 2009-03-08
  • 打赏
  • 举报
回复
窗体有个属性就是是否启用拖拽,启用那个属性,然后在事件中加入拖拽事件
pztx1992 2009-03-08
  • 打赏
  • 举报
回复
在控件的鼠标点击事件中 复制该对象
然后再另外一个窗体的点击事件中转换加载该对象。
cppfaq 2009-03-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wangping_li 的回复:]
使用hbxtlhx的代码

Forms1.cs

C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace dragControl
{
public partial class Form1 : Form
{
private System.Windows.Forms.Button button1;

public Form1()
{
Init…
[/Quote]

Should work
wangping_li 2009-03-08
  • 打赏
  • 举报
回复
使用hbxtlhx的代码

Forms1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace dragControl
{
public partial class Form1 : Form
{
private System.Windows.Forms.Button button1;

public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.button1 = new System.Windows.Forms.Button();
//
// button1
//
this.button1.Location = new System.Drawing.Point(116, 65);
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;
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);

this.Controls.Add(this.button1);
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
this.button1.DoDragDrop(sender, DragDropEffects.Move);
}
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
Form2 frm = new Form2();
frm.Show();
}
}
}


Forms2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace dragControl
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.AllowDrop = true;
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form2_DragDrop);
this.DragOver += new System.Windows.Forms.DragEventHandler(this.Form2_DragOver);
}
private void Form2_DragOver(object sender, DragEventArgs e)
{
if (e.AllowedEffect == DragDropEffects.Move)
{
e.Effect = DragDropEffects.Move;
}
}

private void Form2_DragDrop(object sender, DragEventArgs e)
{
Button btn = e.Data.GetData("System.Windows.Forms.Button") as Button;
if (btn != null)
{
if (btn.Parent != null)
{
btn.Parent.Controls.Remove(btn);
btn.Location = this.PointToClient(new Point(e.X, e.Y));
this.Controls.Add(btn);
}
}
}
}
}

lateknow 2009-03-08
  • 打赏
  • 举报
回复
在第一个form中的要被拖拽的控件的MouseDown事件时,执行this.DoDragDrop(要拖拽的控件或者其它数据, DragDropEffects.Move);
在第二个form的接受控件的DragDrop事件里 object o = e.Data.GetData(typeof(对象));o就是拖曳的东西,你爱怎么处理就是你的事了.

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧