vb.net 如何从一个面板拖放(复制)到另一个面板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16165400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Drag and Drop(copy) from one panel to another panel
提问by nufshm
I have two panels on a form.
One panel has some controls like buttons or images and second panel is empty. I want to drag a control from panel 1 and drop it to panel 2, but it should create a copy of control, and while dragging a rectangle should be shown of same size as control and when dropped in panel 2 the dragged shape should appear there at mouse position
Actually I want to create a simulator like thing. That has some tools in panel 1 and when someone drag and drop a tool on panel 2 it should appear there at mouse position.
我在一个表格上有两个面板。
一个面板有一些控件,如按钮或图像,第二个面板是空的。我想从面板 1 拖动控件并将其放到面板 2,但它应该创建控件的副本,并且在拖动矩形时应显示与控件相同的大小,并且在面板 2 中拖放时,拖动的形状应该出现在那里在鼠标位置
其实我想创建一个模拟器之类的东西。在面板 1 中有一些工具,当有人在面板 2 上拖放工具时,它应该出现在鼠标位置。
Language doesn't matter may be C#or VB.NET
语言无关紧要可能是C#或VB.NET
采纳答案by Shim
Did you try something like this ?
你有没有尝试过这样的事情?
private void Form5_Load(object sender, EventArgs e)
{
this.panel1.AllowDrop = true;
foreach (Control c in this.panel1.Controls)
{
c.MouseDown += new MouseEventHandler(c_MouseDown);
}
this.panel1.DragOver += new DragEventHandler(panel1_DragOver);
this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
}
void c_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
c.DoDragDrop(c, DragDropEffects.Move);
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
if (c != null)
{
c.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
this.panel1.Controls.Add(c);
}
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
VB.NET
网络
Private Sub Form5_Load(sender As Object, e As EventArgs)
Me.panel1.AllowDrop = True
For Each c As Control In Me.panel1.Controls
c.MouseDown += New MouseEventHandler(AddressOf c_MouseDown)
Next
Me.panel1.DragOver += New DragEventHandler(AddressOf panel1_DragOver)
Me.panel1.DragDrop += New DragEventHandler(AddressOf panel1_DragDrop)
End Sub
Private Sub c_MouseDown(sender As Object, e As MouseEventArgs)
Dim c As Control = TryCast(sender, Control)
c.DoDragDrop(c, DragDropEffects.Move)
End Sub
Private Sub panel1_DragDrop(sender As Object, e As DragEventArgs)
Dim c As Control = TryCast(e.Data.GetData(e.Data.GetFormats()(0)), Control)
If c IsNot Nothing Then
c.Location = Me.panel1.PointToClient(New Point(e.X, e.Y))
Me.panel1.Controls.Add(c)
End If
End Sub
Private Sub panel1_DragOver(sender As Object, e As DragEventArgs)
e.Effect = DragDropEffects.Move
End Sub
回答by Sarath Avanavu
Am changing a little of code of @Shim. Here is the updated code in which a copy of your control will be placed in another panel
正在更改@Shim 的一些代码。这是更新后的代码,您的控件副本将放置在另一个面板中
Random rnd = new Random();
private void Form5_Load(object sender, EventArgs e)
{
this.panel1.AllowDrop = true;
foreach (Control c in this.panel1.Controls)
{
c.MouseDown += new MouseEventHandler(c_MouseDown);
}
this.panel1.DragOver += new DragEventHandler(panel1_DragOver);
this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
}
void c_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
c.DoDragDrop(c, DragDropEffects.Move);
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
// Here, you get a copy of your drag drop button and dynamically new button is created
Button btn = new Button();
btn.Name = "Button" + rnd.Next();
btn.Size = c.Size;
if (c != null)
{
// Add the newly created button to you Panel
btn.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
this.panel1.Controls.Add(btn);
}
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
回答by RiBoysen
this solution will drag the button(or any other component selected) while moving the mouse and place it on the place where you drop it
此解决方案将在移动鼠标的同时拖动按钮(或选择的任何其他组件)并将其放置在您放置它的位置
private SimpleButton selectedButton;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
xtraScrollableControl2.AllowDrop = true;
xtraScrollableControl2.DragEnter += XtraScrollableControl_DragEnter;
xtraScrollableControl2.DragDrop += XtraScrollableControl_DragDrop;
xtraScrollableControl2.DragOver += XtraScrollableControl_DragOver;
}
private void XtraScrollableControl_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.Data.GetDataPresent(typeof(Bitmap)) ? DragDropEffects.Copy : DragDropEffects.None;
}
private void XtraScrollableControl_DragDrop(object sender, DragEventArgs e)
{
var simpleButton = e.Data.GetData(e.Data.GetFormats()[0]) as SimpleButton;
if (simpleButton == null) return;
if (simpleButton.Parent != sender)
{
var btn = new SimpleButton
{
Dock = DockStyle.None,
Size = new Size(125, 50),
Text = simpleButton.Text,
Location = ((XtraScrollableControl) sender).PointToClient(new Point(e.X, e.Y)),
ImageList = simpleButton.ImageList,
ImageIndex = simpleButton.ImageIndex,
ImageLocation = simpleButton.ImageLocation,
Parent = ((XtraScrollableControl)sender)
};
btn.MouseDown += simpleButton_MouseDown;
((XtraScrollableControl)sender).Controls.Add(btn);
}
else
{
((XtraScrollableControl)sender).Controls.Remove(simpleButton);
simpleButton.Location = ((XtraScrollableControl)sender).PointToClient(new Point(e.X, e.Y));
((XtraScrollableControl)sender).Controls.Add(simpleButton);
}
selectedButton = null;
}
private void XtraScrollableControl_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
selectedButton.Location = ((XtraScrollableControl)sender).PointToClient(new Point(e.X, e.Y));
}
private void simpleButton_MouseDown(object sender, MouseEventArgs e)
{
var btn = sender as SimpleButton;
if (btn == null) return;
selectedButton = btn;
btn.DoDragDrop(btn, DragDropEffects.Copy);
}
hope this will help some
希望这会帮助一些
i used DevExpress components but for the standart
我使用了 DevExpress 组件,但用于标准
DevExpress XtraScrollableControl SimpleButton Microsoft Panel Button

