Introduction
In PMS Tutorial2 we have shown how to Update, Delete and Insert records into local database, we also explored how to use the database connection with single form. For an application with many forms the classical way of defining new object from the database connection in each form will not work. Instead, the same database connection must be used in all forms. In this tutorial we explore the Multiple Document Interface MDI, and how to make global database connection so it can be accessed from any form in the application. The tutorial is implemented by Visual Studio 2012. Object Oriented Inheritance will be used in this tutorial, so basic understanding of inheritance should be granted for understanding the contents of this tutorial.
3.1 Multiple Document Interface Main Form
Add new form to be the main form of the project. The main form usually contains the main menu bar, the tools bar and the links to the most important forms. Create new form by clicking the project icon from the solution explorer-> then Add -> then Windows Form

Write frmMain to be the form's name; then click add button. The form is then added to the project. For that new form the following properties must be changed
- Text should be changed to "Pharmacy Management Systems PMS".
- IsMdiContainer should be changed to True. As shown in the figure, it changes the form type to MDI form
- WindowState should be changed to Maximized. This makes the form maximized in startup

If the program is executed (by clicking start) right now it will display the default form; which may not be the main form. To change startup form of the application, the Main() function which holds the starting point of the program should be changed so the first form to be loaded is frmMain. To do that, from the Solution Explorer double click on Program.cs the following code will be displayed
namespace MyPharmacy
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Change Application.Run(new Form1()) to Application.Run(new frmMain()); Then run the program to see which form is displayed first.
3.2 Using Menu Bar to Open Child Forms
Add MenuStrip to the main form as shown in the figure.

Customize the menu bar by adding two main items (File and Screens) then for each item add two subitems. File => New, Screen=>Drugs,Suppliers. As shown in the figure

The menu bar is then used to open all forms of the project.
Create new Table called "Suppliers" then add random records to it, and then create new form called frmSupplier add DataGridView to it as shown in the following figure. If you can't do that, refer to PMS Tutorial1 for full explanation. Prepare the project so the project has two forms One for Suppliers and one for Drugs. You can also use the existing Drug's form from PMS Tutorial2.

To open the two forms using the menu bar, open the main form, then from the menu bar double click on "Drugs" menu item as shown in the figure.

Add the following code
private void drugsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 frmDrugs = new Form1();
frmDrugs.ShowDialog();
}
Since Form1 is the Drugs form, new instance of it is created using the "new" keyword. ShowDialgo() methods displays the form as dialog box inside the main form. Repeat the above procedure for the Supplier form and add the following code
private void suppliersToolStripMenuItem_Click(object sender, EventArgs e)
{
frmSuppliers frmSupp = new frmSuppliers();
frmSupp.ShowDialog();
}
3.3 Creating Global Database Connection for All forms
The main idea behind making global database connection is to make each separate form inherits not from the "Form" class but from "Customized Form includes DBConnection object". We call the customized form DBConnectionForm, DBConnectionForm inherits from the class "Form". The general outline of project is shown in the following figure

Step 1: Create new class call it "DBConnectionForm", open the code and then add the following code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyPharmacy
{
public class DBConnectionForm:Form
{
protected static DBConnection myConnn = new DBConnection();
private void InitializeComponent()
{
this.SuspendLayout();
//
// DBConnectionForm
//
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "DBConnectionForm";
this.ResumeLayout(false);
}
}
}

Osama HosamPosted Mar 8, 2014, 6:15 AM
I think this is a good solution, but my idea is to make DBConnectionForm as store for all global objects in the project. for example, the current logged in user, current specific customization, etc.
Sam HobbsPosted Mar 2, 2014, 11:32 AM
I would put myConnn in the Program class (Program.cs file) since the Program class must be static anyway. The database could be connected to and disconnected from in the Program class. I would create a separate form to show error information if necessary.to be used in the Program class.