问个拖拉的问题,
我有两个窗体,需要把其中一个窗体中的控件拖拉到另外一个窗体中,如何实现?

解决方案 »

  1.   

    看看DragDrop、DragEnter等事件~~~~
      

  2.   

    控件也是一个Object,你想做什么?
      

  3.   

    我有一个例子,楼主参考一下:
    在例子中有两个窗体,其中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());
    }
    }
    }