MenuStrip

Used to create a menu bar in the form. A menu bar is a collection of multiple menu items. It is known as invisible control. The menu items are of two types.
1) Parent Menu Items
2) Child Menu Items

API: System.Windows.Forms.MenuStrip
Naming Convension: mnuxxxxxx
 MenuStrip
Each menu item will be created as a control for “System.Windows.Forms.ToolStripMenuItem” class. The default naming convention for the menu item is: “xxxxxToolStripMenuItem”.
Properties of MenuStrip
Property Description
Dock Top, Bottom, Left, Right, Fill
TextDirection Horizontal, Vertial90, Vertical270.
Name, BackColor, ForeColor, Font, Enabled, Visible, Size, Location, BackgroundImage, BackgroundImageLayout, ContextMenuStrip

Events of MenuStrip
Event Description
ItemClicked, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

Properties of Menu Item
Property Description
IName, Text, TextAlign, BackColor, ForeColor, Font, Image, ImageAlign, Checked, Enabled, Visible, ShortcutKeys, ShowShortcutKeys, Size, Location, BackgroundImage, BackgroundImageLayout, ContextMenuStrip, BorderStyle

Events of Menu Item
Event Description
Click, DoubleClick, MouseMove, MouseEnter, MouseLeave

Demo on MenuStrip
 MenuStrip
private void showToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = Image.FromFile("c:\\globe.jpg");
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackgroundImage = null;
}
private void normalToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}

private void minimizedToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}

private void maximizedToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}

private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}

private void form3ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 f = new Form3();
f.Show();
}

ContextMenuStrip

It is also a menu related control, similar to MenuStrip. But the context menu would be displayed, when the user right clicks on a control or a form, at run time. This is also a collection of menu items. The context menu is also called as “Shortcut menu”. The “ContextMenuStrip” is an invisible control.
ContextMenuStrip
API: System.Windows.Forms.ContextMenuStrip
Naming Convension: conMnuxxxxxx

Properties of ContextMenuStrip
Property Description
Name, BackColor, Font, Enabled, Size, Location, BackgroundImage, BackgroundImageLayout, ContextMenuStrip

Events of ContextMenuStrip
Event Description
ItemClicked, Click, DoubleClick, MouseMove, MouseEnter, MouseLeave, KeyPress, Enter, Leave

Demo on ContextMenuStrip
ContextMenuStrip
private void displayIconToolStripMenuItem_Click(object sender, EventArgs e)
{
if (displayIconToolStripMenuItem.Checked == true)
{
displayIconToolStripMenuItem.Checked = false;
this.ShowIcon = false;
}
else
{
displayIconToolStripMenuItem.Checked = true;
this.ShowIcon = true;
}
}

private void redToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackColor = Color.Red;
redToolStripMenuItem.Checked = true;
greenToolStripMenuItem.Checked = false;
blueToolStripMenuItem.Checked = false;
}
private void greenToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackColor = Color.Green;
redToolStripMenuItem.Checked = false;
greenToolStripMenuItem.Checked = true;
blueToolStripMenuItem.Checked = false;
}

private void blueToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackColor = Color.Blue;
redToolStripMenuItem.Checked = false;
greenToolStripMenuItem.Checked = false;
blueToolStripMenuItem.Checked = true;
}

private void showInTaskBarToolStripMenuItem_Click(object sender, EventArgs e)
{
if (showInTaskBarToolStripMenuItem .Checked == true)
{
showInTaskBarToolStripMenuItem.Checked = false;
this.ShowInTaskbar = false;
}
else
{
showInTaskBarToolStripMenuItem.Checked = true;
this.ShowInTaskbar = true;
}
}

private void upperCaseToolStripMenuItem_Click(object sender, EventArgs e)
{
txtMyText.Text = txtMyText.Text.ToUpper();
}

private void lowerCaseToolStripMenuItem_Click(object sender, EventArgs e)
{
txtMyText.Text = txtMyText.Text.ToLower();
}

private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
txtMyText.Clear();
}

MDI Applications

The windows applications are of two types.
1) SDI Applications (Single Document Interface)
2) MDI Applications (Multiple Document Interface)
The SDI and MDI applications contain multiple forms. But in SDI applications, each form will be executed as individual form; whereas in MDI applications, one form acts as Parent Form, and the remaining forms act as Child Forms.
You can observe the visibility of MDI applications in the picture.
ContextMenuStrip

Features of MDI Applications:

1) All the child forms are contained by the parent form, so that the parent form is also called as “Container form”.
2) Among several child forms, only one form acts as “active child form”.
3) Generally the parent form contains no UI design, it contains a menu.
4) Any child form can’t moved outside of it’s parent form.
5) In VB 6.0, only one form can be implemented as parent form in a project. But in C#.NET and VB.NET, you can define multiple parent forms within the same project.
6) In VB 6.0, the parent form can’t contain any type of controls. But in C#.NET and VB.NET, you can drag any controls.
7) The child form icon is not displayed in the windows taskbar.
8) If the parent form is moved, all the child forms will be moved.
9) Whenever the parent form is minimized, all the child forms will be minimized.
10) Whenever the parent form is maximized, all the child forms will be restored.
11) Whenever the child form is minimized, an icon will be created at the bottom area of the parent form.
12) Whenever the child form is maximized, the text of the parent form and child form will be concatenated.
13) Whenever you close the parent form, all the child forms will be closed automatically
14) The child form is able to access the reference of it’s parent form.
15) The parent form is able to access the references of it’s child forms.

Implementation of MDI Applications in C#

1) Convert the Form as Parent Form.
• To convert, set that form’s property “IsMDIContainer = True”.
2) Invoke the child form at run time from the parent form.
• Use the following code.
ChildFormClassName obj = new ChildFormClassName();
obj.MdiParent = this;
obj.Show();

Demo on MDI Applications

midi applications
Design
Form1
IsMDIContainer: true
Form1.cs
private void Form1_Load(object sender, EventArgs e)
{
Form2 f = new Form2();
f.MdiParent = this;
f.Show();
}
Form1.cs
midi applications
private void form2ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.MdiParent = this;
f.Show();
}
private void form3ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 f = new Form3();
f.MdiParent = this;
f.Show();
}
private void form4ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form4 f = new Form4();
f.MdiParent = this;
f.Show();
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.ActiveMdiChild != null)
{
this.ActiveMdiChild.Close();
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void pinkToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.ActiveMdiChild != null)
{
this.ActiveMdiChild.BackColor = Color.Pink;
}
}
private void cyanToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.ActiveMdiChild != null)
{
this.ActiveMdiChild.BackColor = Color.Cyan;
}
}
private void orangeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.ActiveMdiChild != null)
{
this.ActiveMdiChild.BackColor = Color.Orange;
}
}