Duplicate form's controls to Other form

jessiang

Member
Joined
Jun 6, 2006
Messages
8
Programming Experience
Beginner
i have a form called frmNew, after i choose the "cmdOK" button i want a duplicate form exactly same as frmNew by programatically, i have success to create a duplicate form programatically as :

Dim frmDuplicate As New Form
frmDuplicate.Show()
frmDuplicate.Size = frmNew.Size


but when i want to add the controls from frmNew to duplicate form as:

frmDuplicate.Controls.Add(frmNew.Controls.Item(0))

the controls from frmNew will disappear but the duplicate form got display the controls.can anyone help me with this?

thanks,
 
This is where inheritance plays a really good role. All you need to do is change the line at the top of the statement and the top of frmduplicate where it says

VB.NET:
Inherits System.Windows.Forms.Form

to

VB.NET:
Inherits FormNew

Job Done.
 
Or if you want to duplicate form exactly same as frmNew by programatically then this will work:
VB.NET:
Dim frmDuplicate as New frmNew
frmDuplicate.Show()
 
This is where inheritance plays a really good role. All you need to do is change the line at the top of the statement and the top of frmduplicate where it says

VB.NET:
Inherits System.Windows.Forms.Form

to

VB.NET:
Inherits FormNew

Job Done.

Curious use of inheritance there.. providing a differentially typed but otherwise identical clone of an existing object.. :)
 
Inheriting should be used while developing not programmtically at run time. Declaring the frmNew as another object is the correct answer for this scenario. If you wish to develop the frmNew as you would a Windows.Forms.Form then yes you would Inherit it at design time and make any changes (if needed) that you desire.
 
Inheriting should be used while developing not programmtically at run time. Declaring the frmNew as another object is the correct answer for this scenario. If you wish to develop the frmNew as you would a Windows.Forms.Form then yes you would Inherit it at design time and make any changes (if needed) that you desire.


Correct way?, is there such a thing? the fact that both methods are done at design time not apparent to you?. They are both hardcoded and cannot be changed. Both accomplish the same goal. So correct way?, i don't think so. Just a different way.
 
vis781.

No they are not the same. In the light that you speak EVERYTHING is done at development. What I am saying is well, let me explain in detail.

For starters lets say that no matter which way you go you eventually have to declare the object. So if we inherit like so...

VB.NET:
Class [COLOR=red]MyForm [/COLOR]
Inherits [COLOR=seagreen]newForm[/COLOR]

Then you still have to declare it to use it.

VB.NET:
Dim F as new [COLOR=red]MyForm[/COLOR]

So it already makes more since to just declare what we have.

VB.NET:
Dim F as new [COLOR=seagreen]newForm[/COLOR]

Now, to better explain what is meant by programmatically at run time rather than developing is simple. When you declare an object...

VB.NET:
Dim F as new newForm

...this may or may not ever happen depending on how you code.

And depending on certain options if can be delcared differently.

VB.NET:
If x > 10 then
Dim F as new newForm
else 
Dim F as new AnotherForm
end if

Now when you inherit you type a new Class.

VB.NET:
Class MyForm
Inherits newForm
 
'here we add a new constructor forcing us to set position of the form
Sub New(byVal Location as Point)
me.location = location
end sub
 
 
End Class

You do this to make additions to the way the newForm class originally worked. Then you declare the MyForm class as the seperate object.

VB.NET:
If x > 10 then 
Dim f as new newForm
f.show
else
Dim f as new MyForm( 500, 500)
f.show
end if

See, now the inherited version has alterations like our constructor.

If you can't dig into that and get the jist of it then it may too much for you to understand but YES there is a Correct way.


vis781
Curious use of inheritance there.. providing a differentially typed but otherwise identical clone of an existing object..
Back to what I said earlier you still have to declare and use it so if you want a clone of an object inheriting is not the solution. Just declare another object of it.
 
Maybe you should read the original posters question, you'll find it will help when you answer questions on this forum. Also thankyou for letting me know that you have to instantiate an object before using it, very enlightening. The original posters query had two distinct types

VB.NET:
FrmNew
 
FrmDuplicate

The only way to accomplish this is to use inheritance unless you want to write a lot of unnecessary code. You can look it up here...

http://www.informit.com/articles/article.asp?p=170719&rl=1

Its quite an advanced tutorial but if you need any help on understanding it let me know and i'll be glad to oblige.


If you wish to develop the frmNew as you would a Windows.Forms.Form then yes you would Inherit it at design time and make any changes (if needed) that you desire.

And whilst we're at it what does that statement mean??? FrmNew IS a 'System.Windows.Forms.Form' and any derived class would also have all the functionality of a 'System.Windows.Forms.Form. You would inherit it at design time!? When else would you inherit it?
The point of inheritance is that it enables polymorphism. The flexibility to create distinctly different types but with all the functionality of the base classes but it only need be used if necessary. You provide overridable methods to handle the events you want to do differently in the derived classes or alter members in the derived class then pass the method back to the base class. In closing this post was resolved days ago so there really isn't much point in continuing this thread.
 
Last edited:
Dim FrmDuplicate as new FrmNew

That IS A CLONE OF THE FrmNew

If you inherit

Class FrmDuplicate
Inherits FrmNew


Then you still have to instantiate it...

Dim Frm as new FrmDuplicate

WHY? GO THAT FAR?
 
Last edited by a moderator:
I refer you to an earlier ppst i made when i said that both methods achieve the same goal. However when i answer a question i try to interpret the needs of OP. From his/her first post i deduced that he/she was familiar with the NEW keyword and would know how to do instantiate and new object, atleast that was clear to me. As for this part.....
 
Last edited by a moderator:
Back
Top