关于对象的深拷贝问题,求思路?方法?
有一个窗口,上面有一个 CommandButton,名字为Command1 ,Caption为"123" 有代码如下
- VB code
private sub CopyObject() dim cbNew as commandButton dim cbOld as commandButton set cbOld = me.Command1 debug.print cbOld.Caption '此处打印 123 set cbNew = me.Command1 cbNew.caption = me.Command1.Caption + me.Command1.Caption debug.print cbOld.Caption '此处打印 123123 end sub
从这里的代码,可以知道其实 cbOld与cbNew指向了同一个CommandButton,
如何让cbOld从Command1拷贝一个完整的和Command一样的新的对象?
如果对象是 RecordSet,还有一个Clone 方法,可以其他像 TextBox ,CommandButton 之类的并不一定有Clone方法。
[解决办法]
你用set指向一个没有clone方法的对象,只能是添加了一个引用过去
你可以用代码来添加一个按钮
- VB code
Dim WithEvents cmd1 As CommandButtonPrivate Sub cmd1_Click() MsgBox "cmd1!"End SubPrivate Sub Form_Load() Set cmd1 = Me.Controls.Add("VB.CommandButton", "cmd1") With cmd1 .Visible = True .Width = 1000 .Height = 1000 .Caption = "i'm cmd1~" End WithEnd Sub