Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi friends,
I have a number of forms. There data are different from each other. There are some functions that are common to all the forms. What I want is to make a parent form and place the common code there. Please help me how can I create a common parent for all the forms.
thank you for reading my question.
Posted

Create a new class like this:

C#
public partial class FormBase : Form
{
  // put your common data members/methods in here
}
Then, you can inherit this form in all of your other forms, like this:

C#
public partial class MyForm1 : FormBase
{
}


Make sure you give each one a default constructor so you can still work with them in the designer.

EDIT =========

Why was my answer voted a 1? That makes no sense at all.
 
Share this answer
 
v2
First Create a form name it Parent, identifier should be Public:
C#
public partial class Parent : Form
{
 // You can have Protected variables and Methods
}


Now you can make child form name child

C#
public partial class child : Parent
{
 // you can use all protected variables and methods of Parent class
}
 
Share this answer
 
Comments
#realJSOP 29-Mar-11 5:26am    
This is exactly the same I answer I provided more than a month ago, except your code won't compile because you're not using Parent instead of parent in the child form definition.
[no name] 29-Mar-11 6:48am    
Thanks for sugession
Here[^] is a Walkthrough of what Microsoft call Visual Inheritance.

See if that helps.
 
Share this answer
 
All you need to learn about Form Inheritance[^]

C#
public class Form2 : Namespace1.Form1
 
Share this answer
 

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