将控件从用户控件复制并移动到窗体; @运行 [英] copy and move controls from usercontrol to Form; @RUNTIME

查看:90
本文介绍了将控件从用户控件复制并移动到窗体; @运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将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;

        }

推荐答案

难怪;您缺少对拖放的任何处理.某些代码应执行控件的移动.假设您有两个父控件oldParentControlnewParentControl,而拖动的控件是movingControl.您的所有活动最终都应以类似
的方式完成
No wonder; you are missing any handling on drag drop. Some code should do the moving of the control. Let''s say you have two parent controls, oldParentControl and newParentControl and the control being dragged, movingControl. All your activity should ultimately be complete with something like

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将需要阅读有关从头开始拖动和重写代码的所有相关部分的信息.

—SA



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.

—SA


这篇关于将控件从用户控件复制并移动到窗体; @运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆