Click here to Skip to main content
13,055,852 members (50,109 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I need a form similar to an older one. How to use the older form to create a new form to interact with different entity of the database (the new form if created, shall be using two more controls) ?

Just as we inherit classes, can we inherit windows forms too?

If yes, then how to?
Posted 10-Oct-12 1:19am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Yes, you can derive a form from another form (as long as it is not marked as sealed). How? Exactly the same way as you do with any other class.

Suppose you start with EntityForm and then you want to create form ExtendedEntityForm which adds, say, a couple of controls to the EntityForm. To do this, you just have to do the following:

// This is your base form
public class EntityForm: Form
// This is your extended form
public class ExtendedEntityForm: EntityForm
Anaya Upadhyay 11-Oct-12 0:43am
I needed the form in GUI not from the classes. I was clearing my ideas illustrating classes.
For example: Firstly, A form containing two textboxes for entering name and address.
Second, I need a similar form containing two textboxes and a grid (not in the previous one) but i want to use the same form.
is it possible to implement?
Frederico Barbosa 11-Oct-12 5:00am
I just posted a new version...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Well, if you don't want to create a derived form and prefer to extend the existing form at runtime, you can configure your grid(or some other control) and then add it to the Controls collection of your form.
// This is an instance of your existing form (with your two text boxes)
EntityForm form = new EntityForm();
// Create a new grid (or some other control)
System.Windows.Forms.DataGrid grid = new DataGrid ();
// Configure the control to fit your needs
grid.Visible = true;
grid.Location = new Point(100, 100);
// ...
// You might want to change form size here to accommodate the new control 
// ...
// Add the grid to the Controls collection of the form instance
// Show the dialog  (you'll see your grid in the form).

Is this what you had in mind?
Anaya Upadhyay 11-Oct-12 5:52am
Thanks for the help, it seems I'm now able to use a single form to create another form, but how to implement the work of a single form for two different things. How to structure the code or how to call a single form from two different menus to perform two different things.
Frederico Barbosa 11-Oct-12 6:04am
Why don't you add a SplitContainer to your form, in one of the panels you add the text boxes and on the other the grid (everything pre-configured as you need). And then hide/show the panel where the grid is. You may also add a special constructor to your form with a parameter that determines the behavior. Just a thought.
Anaya Upadhyay 11-Oct-12 6:06am
I was not talking about the interface but the work need to be different.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 11 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100