[RESOLVED] Make a form very similar to existing one
Hi,
My app will have two forms which have quite a few controls but are 90% the same. frmCustomers and frmProducts.
I created frmCustomers, and now I need to make the second one.
Since the 2nd one is almost identical to the first one, I was hoping to somehow copy and rename the first one and just make the changes and save under frmProducts.
Otherwise I have to create the 2nd form from scrach.
Is it possible?
BTW, I'm coding in C# but I think this has more to do with the whole .NET IDE than the language.
Re: Make a form very similar to existing one
you should create a master form that contains everything that BOTH forms should have... then each of your 2 forms should inherit from the master form, and they will both gain all its functionality, while allowing you to add the parts that make each of the 2 forms different from eachother.
Re: Make a form very similar to existing one
Add a form to your project, and view the code behind. It should say something to the effect of
Inherits System.Windows.Forms.Form
Change this to
Inherits Form1
or whatever you main form's name is
Now go to project>properties>Startup Object and choose the name of your child form.
Re: Make a form very similar to existing one
Thanks for your replays.
Is there a way of doing it without inheritance, like I mentioned just copy the form to another name and add to the project, then change it as needed?
Re: Make a form very similar to existing one
Yes you can do that too..
in the solution explorer, simply right click the form, copy. For some reason if you right click again, you don't get a paste option, but if you go to the edit menu next to the file menu up top, you can select paste there. After the copy, rename it to what you want, and also change the class name to something else (because it will be the same as the first form and will give you a syntax error until you do). That should do it.
Oh and it won't have the "form" looking icon until you do the last step I mentioned above.
Just remember by doing it this way, you will end up making the SAME changes in 2 places, which is more work for yourself than learning a little bit about inheritence, and using it to your advantage.
Re: Make a form very similar to existing one
Thanks kleinma, it works great.
The reason I wanted to do it this way is I needed a quick and dirty way plus I don't fully understand how to inherit it properly. However, I think you're right and I'd like to try to do it with inheritance.
The question I have is:
After I inherit the first form:
A) how do I add new features to the new form that will only be available on the new form
B) how do I remove features that were on original form but are not needed in the new form
By features I mostly mean controls.
Re: Make a form very similar to existing one
I don't know what version of .NET you are using, but normally when you have a form in your project, that form inherits from the base form class which is
system.windows.forms.form
so this is your parent form, and you would add only the controls/code that is the same on both the child forms you are going to create from it.
A child object that inherits from its parent has all its abilities plus whatever you add to it.
Re: Make a form very similar to existing one
I see. So I should re-design my parent form so that it only contains controls that will be common to both forms.
I'm using VS2003
Re: [RESOLVED] Make a form very similar to existing one
usually yes that is how it is done.
There are some tricks you cause use also though.
for example, lets say you have frmParent, and frmChild1, frmChild2
if frmChild1 didn't want a certain control visible, you could simply make that control invisible in the constructor of frmChil1.