133-c-sharp-mdi-application

Reading Time: 2 minutes

Making Multiple-Document Interface (MDI) in C# is your next big step towards professional programming. Any good windows forms application has to have multi functionality, i.e. many child forms that should be accessible from parent form by using menu. Actually, every child form in C# MDI application is small but functional application. So, once when you have created your application in particular form, you need to attach it to the parent form – main form of your application that contains menu. In order to do so, you will need to adjust several initial properties in your parent form. Properties that you need to adjust are following:

—put your WindowState to Maximized.

—and what is functionally more important, put IsMdiContainer on True

ismdicontainer

Without putting IsMdiContainer property on True your parent form will not be able to be container for other child forms, so your application won’t be MDI. Once when properties are set, menu can be added to your parent form by adding MenuStrip from Menus and Toolbars.

Then you add items to your menu and you can start coding. For example, add menu item “File”, then “Exit”, and then write following code:

DialogResult dlDialog = MessageBox.Show(“Do you really want to exit”, “Check”, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dlDialog == DialogResult.Yes)
{

Application.Exit();

}

For other items, you need to prepare other forms in your MDI application. For example, you can add login window as new windows form.

add-new-form-to-project

Add->New Item->Windows Form

windows-form-c-sharp

StartPosition of Login form should be set to CenterScreen.

start-position-center-screen

FormBorderStyle of Login form should be set to None.

formborderstyle

Then, two text fields and two command buttons should be added.

TextBox for password should have PasswordChar property set to *.

passwordchar

Cancel button has to have exit code:
Application.Exit();

OK button has to check if user logged well. Code is:

string str_username, str_password;
str_username = txtUser.Text;
str_password = txtPassword.Text;

if ((str_username == “test”) && (str_password == “test”))
{

this.DialogResult = DialogResult.OK;

}
else
{

Application.Exit();

}

In this example, login is “test” and password is also “test”. For professional application, login verification should be done from the database. Form DialogResult property has to be set on DialogResult.OK if verification was right.

Main Form must create object of Login form in constructor
public Form1()
{

InitializeComponent();

Login objLogin = new Login();

if (objLogin.ShowDialog() == DialogResult.OK )

{

MessageBox.Show(“Welcome”, “Welcome”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}

}
In this example, main form welcomes user if login was right.

Two more items can be added to menu (Addition, Subtraction), and corresponding forms.
private void additionToolStripMenuItem_Click(object sender, EventArgs e)
{

FormAddition newMDIChildAdd = new FormAddition();
// Set the Parent Form of the Child window.
newMDIChildAdd.MdiParent = this;
// Display the new form.
newMDIChildAdd.Show();

}

private void subtractionToolStripMenuItem_Click(object sender, EventArgs e)
{

FormSubtract newMDIChildSubtract = new FormSubtract();
newMDIChildSubtract.MdiParent = this;
newMDIChildSubtract.Show();

}

Now full function windows MDI application is ready together with login window!

mdi-application

External links:

C Sharp MDI Application Creation on Codeproject
C Sharp MDI Application Creation on Microsoft
C Sharp MDI Application Creation on Authorcode

Leave a Reply

Your email address will not be published. Required fields are marked *

*