问个拖拉的问题,
我有两个窗体,需要把其中一个窗体中的控件拖拉到另外一个窗体中,如何实现?
我有两个窗体,需要把其中一个窗体中的控件拖拉到另外一个窗体中,如何实现?
解决方案 »
- 请问这个控件如何用。
- 初学者问:vs2003中启动正常,直接运行.exe报system.outofmemoryException错误
- 每一行什么意思???
- texbox.value.ToString与textbox.text的区别在哪?
- 问一个关于 AggregateCacheDependency 缓存的问题
- listbox.item.add里面添加一个object以后排序是从零开始的,我有什么办法让每个object的排序也添加值
- 水晶报表能实现这样的功能吗
- 我写的解析常量表达式求值的程序,大家给点意见
- DataList如何实现分页?
- 请问,将鼠标移动到label控件上,鼠标成手的形状,代码如何写?
- 高分请教!高手 关于DataSet 问题
- 高分求解--Cell插件显示问题
在例子中有两个窗体,其中Form1上有一个Label,可以用鼠标拖动到From2上,可以在Button1的点击中打开Form2
---------------------
//第一个窗体的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace dragDropApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void label1_MouseDown(object sender, MouseEventArgs e)
{
this.DoDragDrop(this.label1, DragDropEffects.Move);
}
protected override void OnDragOver(DragEventArgs drgevent)
{
base.OnDragOver(drgevent);
drgevent.Effect = DragDropEffects.Move;
}
protected override void OnDragDrop(DragEventArgs drgevent)
{
base.OnDragDrop(drgevent);
if (drgevent.Data.GetDataPresent(typeof(Label)))
{
if (drgevent.Effect == DragDropEffects.Move)
{
Label lbl = drgevent.Data.GetData(typeof(Label)) as Label;
if (lbl != null)
{
lbl.Location = PointToClient(new Point(drgevent.X, drgevent.Y));
this.Controls.Add(lbl);
}
}
}
} private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show(this);
}
}
}//第二个窗体的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace dragDropApp
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
protected override void OnDragEnter(DragEventArgs drgevent)
{
base.OnDragEnter(drgevent);
if (drgevent.AllowedEffect == DragDropEffects.Move)
{
drgevent.Effect = DragDropEffects.Move;
}
}
protected override void OnDragOver(DragEventArgs drgevent)
{
base.OnDragOver(drgevent);
drgevent.Effect = DragDropEffects.Move;
}
protected override void OnDragDrop(DragEventArgs drgevent)
{
base.OnDragDrop(drgevent);
if (drgevent.Data.GetDataPresent(typeof(Label)))
{
if (drgevent.Effect == DragDropEffects.Move)
{
Label lbl = drgevent.Data.GetData(typeof(Label)) as Label;
if (lbl != null)
{
lbl.Location = PointToClient(new Point(drgevent.X, drgevent.Y));
this.Controls.Add(lbl);
}
}
}
}
}
}//启动窗体的类
using System;
using System.Collections.Generic;
using System.Windows.Forms;namespace dragDropApp
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}