Dragging and dropping a control from one form to another results in moving the control
I want to have another copy of some control upon dragging and dropping it to another form. My code results in moving the whole control. Is there a way to have the two of them being displayed at once given that I want them to have the same reference because the source updates values every second.
here's my code
public partial class DragDropForm : Form
{
public DragDropForm()
{
InitializeComponent();
}
private void tableLayoutPanel1_DragEnter(object sender, DragEventArgs e)
{
object data = e.Data.GetData(e.Data.GetFormats()[0]开发者_开发知识库);
if (data is GaugeContainer)
{
GaugeContainer gauge = data as GaugeContainer;
tableLayoutPanel1.Controls.Add(gauge);
}
else if (data is DataGridView)
{
DataGridView table = data as DataGridView;
tableLayoutPanel1.Controls.Add(table);
}
}
private void tableLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy ;
}
}
// IN THE SOURCE FORM !!!!
private void topCompaniesGridView_MouseDown(object sender, MouseEventArgs e)
{
this.DoDragDrop(this.topCompaniesGridView, DragDropEffects.Copy);
}
A control can only be sited on one container (form) at a time. What you want to do is create a new instance of the control on the destination form. So rather than:
tableLayoutPanel1.Controls.Add(gauge);
do
tableLayoutPanel1.Controls.Add(new GaugeContainer());
// Bind to same data source as original control here...
You then need to bind the control to the same data source as the original control, assuming you have an easily bindable data source of course. Properties on the control that you may have set at design time will not be applied to the new control instance. You'll need to copy the control initialization code from the designer file on the original form.
You can show a control (lets call it A) twice by creating a panel (let's call it B) of the same size and using a VisualBrush ar the Background of B. And setting A as the Visual of the Visual Brush.
However this is an inert 'image' of the control and will not respond to input etc.
A more robust approach is to create another instance of the control and bind it to the same underlying data as the original.
精彩评论