问题描述
我将UserControl
命名为container
:
然后通过选择项目在vs工具箱中添加UserControl
.
现在我只需将用户控件拖放到窗体上即可.
我已经将一些按钮,文本框等控件添加到了您的控件中.... @ Designtime
然后我运行表格...
好的
@Runtime我必须拖动&将用户控件中现有的控件拖放到窗体上,并将该用户控件移到窗体上..
我已经将此代码拖放到上,但没有移到表单上(这里我仅检查用户控件中存在的textBox1
吗????????
i hve made UserControl
named container
:
and then add that UserControl
in vs toolbox by doing choose items....
now i have just drag and drop my user control on to the form ...
i have added some controls like button ,textboxes to the uer control....@Designtime
then i run my form...
ok
@Runtime i have to drag & drop controls existing in user control to the form and also move that usercontrol on to the form..
i hav make this code it is drag and drop on to the from but doesnt move on to the form(here i have check only for textBox1
present in the user control???????
Control crl;
Point loc;
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
textBox1.DoDragDrop(textBox1.Text, DragDropEffects.Copy | DragDropEffects.Move);
}
private void textBox1_MouseMove(object sender, MouseEventArgs e)
{
crl = new Control();
var location = crl.Location;
location.Offset(e.Location.X - loc.X, e.Location.Y - loc.Y);
crl.Location = location;
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.Data.GetDataPresent(DataFormats.Text) ? DragDropEffects.Copy : DragDropEffects.None;
}
推荐答案
movingControl.Left = //...
movingControl.Top = //... new position in coordinate of the new control when it becomes a new parent
movingControl.Parent = newParentControl; //jumps out of old to new
看看你在做什么!在textBox1_MouseMove
中,您每次都会创建一个新控件,然后将其释放,因此最终将对其进行垃圾收集,而无需任何使用.如果这确实是偶数MouseMove
的处理程序(顾名思义,但这不是事实),则意味着您多次重复该毫无意义的代码!
这是无法修复的. In将需要阅读有关从头开始拖动和重写代码的所有相关部分的信息.
Just look what are you doing! In textBox1_MouseMove
you create a new control each time and them you loose it, so it will be eventually garbage-collected without any use. If this is really a handler of the even MouseMove
(as the name suggests, but this is not a fact yes), it means you repeat this pointless code many, many times!
This is beyond repair. In will need to read about dragging and rewrite all the relevant part of the code from scratch.
这篇关于将控件从用户控件复制并移动到窗体; @运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!