![](/img/trans.png)
[英]Dragging and dropping control onto windows form designer does nothing
[英]Dragging and dropping a control from one form to another results in moving the control
我想將某些控件拖放到另一種形式后再得到一個副本。 我的代碼導致移動整個控件。 鑒於我希望它們具有相同的引用,因此有一種方法可以使它們兩個立即顯示,因為源每秒更新一次值。
這是我的代碼
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);
}
控件一次只能位於一個容器(窗體)上。 您要做的是在目標窗體上創建控件的新實例。 因此,而不是:
tableLayoutPanel1.Controls.Add(gauge);
做
tableLayoutPanel1.Controls.Add(new GaugeContainer());
// Bind to same data source as original control here...
然后,您需要將控件綁定到與原始控件相同的數據源,假設您當然擁有一個易於綁定的數據源。 您在設計時可能已設置的控件屬性將不會應用於新控件實例。 您需要從設計器文件中的原始表單上復制控件初始化代碼。
通過創建相同大小的面板(稱為B),並使用VisualBrush作為B的背景,可以將控件(稱為A)顯示兩次。並將A設置為Visual Brush的Visual。
但是,這是控件的惰性“圖像”,不會響應輸入等。
一種更可靠的方法是創建控件的另一個實例,並將其綁定到與原始控件相同的基礎數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.