mtelligent

View Original

Fun with MDI, Toolstrips and Context Menus

These are supplementatal notes I created to help me prepare for Exam 70-553. I have been taking sample exams to prepare and these are intended to help better my understanding in the areas I found I was weakest.

Being that I am primarily a web developer working mostly with ASP.Net one of my weaker areas deal with Windows Form Development.

I created a test application to do a deeper dive into MDI Forms, Toolstrips and Context Menus.

I created a Windows Form Project and quickly created three forms: OuterForm, InnerForm, OtherForm. (I wasn't trying to impress anyone with naming, this was just to delve into code)

Before I start to demonstrate some of the test code I created, a brief background on MDI. MDI stands for Multiple Document Interface. The point is to be able to have a parent window control more than one child window. This is common in applications you use every day. In visual studio you have more than one code file open and you tab between them or you can use the Window Menu to select the document you want to work on.

So to deepen my understanding of these topics I wanted to created a Form that allows me to create two types of other forms which would be housed within it. The first thing I did was set the "IsMDIContainer" Proprty of OuterForm to true. Immediately you will notice the form change from the light gray design surface to an darker gray container.

I wanted to use a menu to give users the option to create instances of OuterForm1 and OuterForm2 so I  dragged a MenuStrip control on to the form. I figured that was enough drag and drop coding so I switched to code view and added the following code to the constructor.


//File Menu
ToolStripMenuItem fileItem = new ToolStripMenuItem();
fileItem.Text = "&File";

ToolStripMenuItem newItem = new ToolStripMenuItem();
newItem.Text = "&New";
newItem.Click += delegate
{
    InnerForm myForm = new InnerForm();
    myForm.MdiParent = this;
    myForm.Show();
};

ToolStripMenuItem otherItem = new ToolStripMenuItem();
otherItem.Text = "Testing";
otherItem.Click += delegate
{
    OtherForm myForm = new OtherForm();
    myForm.MdiParent = this;
    myForm.Show();
};


//Create an Array so We can use the AddRange method
ToolStripMenuItem[] items = new ToolStripMenuItem[2];
items[0] = newItem;
items[1] = otherItem;

//Add the two items to the File Menu
fileItem.DropDown.Items.AddRange(items);

Add to the Top Level MenuStrip
this.menuStrip1.Items.Add(fileItem);

This code adds a file menu and new options that use anonymous delegates to create a new instance of one of the other forms. It sets the new instance MDIParent to the current form so it is displayed within the current Form.

So now when I run it, I can create instance of the child forms over and over. But I wanted to be able to create another menu item that lists all the open forms and allows me to easily switch between forms, like the Window Menu does in Visual Studio or Word.

To accomplish this I added the following code.

//Create a new Top Level Menu to reference the open windows
ToolStripMenuItem windows = new ToolStripMenuItem();
windows.Text = "Open Windows";

//assign the mdiwindowslistitem property to the toolstripmenuitem
menuStrip1.MdiWindowListItem = windows;

//add it back to the menu
this.menuStrip1.Items.Add(windows);

I was surprised how easy it was. I didn't have to do any iteration or anything.

Next I wanted to fool around with Context Menus, these are the menus you get when your right click a label or button. So I added a label to InnerForm and changed the Text to "Right Click Me". I then dragged a Context Menu on to the form and set the ContextMenu Property of the label to the ContextMenu I just created. First I just wanted to add a menu Item to show a dialog box.

//Add a simple menu item for the Right Click Menu
ToolStripMenuItem item = new ToolStripMenuItem();
item.Text = "Testing";
item.Click += delegate
{
    MessageBox.Show("Hello World");
};

this.contextMenuStrip1.Items.Add(item);

This worked simply enough. But now I wanted to add a text box to the right click menu, or any control for that matter. For this we needed to use a ToolStripControlHost.

//Create a TextBox to add to the right click menu
TextBox t = new TextBox();
t.Text = "This is Text in a Textbox";

//Create a ToolStripControlHost to house the TextBox
ToolStripControlHost myHost = new ToolStripControlHost(t);

//Add to the ContextMenu
this.contextMenuStrip1.Items.Add(myHost);

With this kind of code I could add any type of control including user controls to a right click menu.

After writing this code I feel better prepared to answer questions on these topics on the exam. I hope it helps you too.