控制从一种形式复制到另一种控制事件不会复制为什么? [英] Copy of control from one form to another control event would not copy why?

查看:88
本文介绍了控制从一种形式复制到另一种控制事件不会复制为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

任何人都可以解释一下它是什么吗?





谢谢提前

Hi
can any one explain me what is it?


Thanks in advance

推荐答案

你的意思是:在设计时你将一个控件从一个desing-Form复制到另一个但是事件的代码(你的意思是事件处理程序)控件使用的是不是被复制了?

如果你的意思是别的,请尝试找一个更好的问题描述....

如果你有上述问题:想一想应该发生什么:代码可以转发原始表单的内部/私有变量,不会在另一个表单上工作。因此,如果复制控件,Forms-Designer永远不会复制(eventhandler)代码。因此,您可以在另一个表单上重新创建事件处理程序(或使用其他一些技术来避免代码重复 - MVVM?)
So you mean: During design-time you copy a control from one desing-Form to another but the code for the Events (you mean the eventhandlers) used by the control isn't copied?
If you mean something else please try to find a better description of your problem....
If you have above described problem: Think about what should happen: The code may relay on internal/private variables of the original Form and won't work on another Form. So the Forms-Designer never copies (eventhandler) code if you copy a control. So it's up to you to recreate the eventhandler on another form (or use some other technique to avoid code-duplication - MVVM?)


它似乎没有任何意义:你不能可能将控制复制到另一个控件事件:事件和控件不是与赋值兼容的类型。



可能,您的意思是将控件从一个表单复制到另一个表单形成。从某种意义上说,这可能是有可能的,但没有多大意义。您需要了解控件是引用类型之一。 如果你只是复制一个控制变量或一个成员,你就不会得到两个对象。你最终得到对同一个对象的两个引用。即使你使用 System.Object.Clone ,在大多数情况下它也会毫无用处,因为这样的克隆不是。请参阅:

http://en.wikipedia.org/wiki/Object_copy#Shallow_copy [ ^ ]。



基本上,要有两个不同的控件(如果它们采用相同的形式或不同的形式无关紧要),则创建控件的第二个实例并复制一些属性。同时,有趣的是,将控件从一种形式移动到另一种形式或同一形式上的不同父控件很容易:

It does not seem to make any sense: you could not possibly copy "control" to "another control event": events and controls are not assignment-compatible types.

Probably, you mean copying a control from one form to another form. This could be possible, in certain sense, but makes little sense. You need to understand that control is one of the reference type. If you just copy a control variable or a member, you don't get two objects. You end up with two references to the same object. Even if you use System.Object.Clone, it will be pretty much useless in most cases, because such clone is not deep. Please see:
http://en.wikipedia.org/wiki/Object_copy#Shallow_copy[^].

Essentially, to have two different controls (it doesn't matter if they are in the same form or different forms), you create a second instance of the control and copy some of the properties. At the same time, it's interesting that it's easy to move a control from one form to another, or to different parent control on the same form:
Control controlInFirstForm = //...
Control parentInOtherForm =  // could be that form itself,
                             // or some other container control
                             // of this form: Panel, TabPage, etc
// now, do it:
controlInFirstForm.Parent = parentInOtherForm;
// the control will immediately "jump" between forms
// it will be visible if its Location allows that
// otherwise you need to adjust Location and/or Dock



有时,这样的伎俩可以甚至需要,但很少。 :-)



-SA


这篇关于控制从一种形式复制到另一种控制事件不会复制为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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