How to create a base class windows Form?

Hi All,

I wanted to created a windows form with functions that are reusable.  For example, creating a function to connect to the SQL server and validate user's name and password or insert record to SQL Server just by calling the AddRecord function.

TIA



Answer this question

How to create a base class windows Form?

  • DVdR

    You would create a new windows form yourself, let's say "Form1". That form will automatically inherit from the Form class. Add your methods to Form1 for connecting to the database, at a minimum the methods need to be classified as protected so that descendent forms will be able to access the methods. Leave the rest of the form as is.

    Now every form that you create in your application from this point forward use the manner that I described earlier to inherit from Form1. You will then be able to call the methods from Form1 within all of your new forms as if the methods were directly declared in those forms.

    Does that help


  • toc996

    For example, try this:


    public class BaseForm : Form
    {
       public void MyMethod()
       {
          MessageBox.Show("Hello");
       }
    }

    public class DerivedForm : BaseForm
    {
       public DerivedForm()
       {
          MyMethod();
       }
    }

     


  • EA512

    Hi Mark,

    I am new at C# and I know "Inherited Form" is a nice feature.  But how do I find out if the inherited form already have functions that I need   If "Not", how do I include the new functions to the inherited form or build my own

    TIA.



  • JBJBJB

    Not sure what your intent is, but if you want your form to inherit from another form in VS 2005, all you need to do is right-click in the solution in the solution explorer..Add..Windows Form. A dialog will appear and you select "Inherited Form". After pressing Ok, you will be given a dialog to choose which form you want to inherit from in your current project.

    Using that method you could have all of the forms in your project descend from a common form in which you can put some of your methods. Whether that is the proper design for your situation though is up to you.

  • How to create a base class windows Form?