C#拖放复制控件 [英] C# Drag and Drop Copying Controls
本文介绍了C#拖放复制控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!
我试图做一个简单的拖放操作,从一个面板到另一个面板......但我试图拖动的是一个按钮...
问题是我的代码移动控件而不是复制..我想维护源按钮...
这是我的代码:
Hello guys!
Im trying to do a simple drag and drop, from a panel to another panel...But what im trying to drag is a button...
The problem is that my code moves the control, not copy.. I want to maintain the source button...
here is my code:
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 DragDropExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
Control ctrl = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
if (ctrl != null)
{
ctrl.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
this.panel1.Controls.Add(ctrl);
}
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
//button1.DoDragDrop(button1, DragDropEffects.Copy | DragDropEffects.Move);
button1.DoDragDrop(button1, DragDropEffects.Copy );
}
private void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void Form1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy ;
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
Control ctrl = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
if (ctrl != null)
{
ctrl.Location = this.PointToClient(new Point(e.X, e.Y));
this.Controls.Add(ctrl);
}
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
感谢!!
Rafael
thanksss!!
Rafael
推荐答案
我从你对你的问题的描述中理解了什么,&我从你的代码中看到的是:
你想要复制控件,但是你得到的是控件移动了。
解决方案很简单:
替换每个语句,如:
Controls.Add(Ctrl);
To:
Control Ctrl2 = nCtrl.Clone();
What I understand from your description of your problem, & what I see from your code is :
You want to copy the control, but what you got is, the control moved.
Solution is simple :
Replace every statement like :
Controls.Add(Ctrl);
To :
Control Ctrl2 = nCtrl.Clone();
Controls.Add(Ctrl2);
这篇关于C#拖放复制控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文