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
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)
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 Item->Windows Form
StartPosition of Login form should be set to CenterScreen.
FormBorderStyle of Login form should be set to None.
Then, two text fields and two command buttons should be added.
TextBox for password should have PasswordChar property set to *.
Cancel button has to have exit code:
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;
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
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.
private void subtractionToolStripMenuItem_Click(object sender, EventArgs e)
FormSubtract newMDIChildSubtract = new FormSubtract();
newMDIChildSubtract.MdiParent = this;
Now full function windows MDI application is ready together with login window!