Results 1 to 9 of 9

Thread: [RESOLVED] Make a form very similar to existing one

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Resolved [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.
    Thanks

    Tomexx.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    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?
    Thanks

    Tomexx.

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    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.
    Thanks

    Tomexx.

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    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
    Thanks

    Tomexx.

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width