Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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:


C#
// This is your base form
public class EntityForm: Form
{

}

// This is your extended form
public class ExtendedEntityForm: EntityForm
{
   
}
 
Share this answer
 
Comments
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...
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.
C#
// 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
form.Controls.Add(grid);

// Show the dialog  (you'll see your grid in the form).
form.ShowDialog();

Is this what you had in mind?
 
Share this answer
 
v2
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900