Browse DevX
Sign up for e-mail newsletters from DevX


Master Toolbars and Menus with the New ToolStrip Control : Page 5

For years, developers have complained about the difficulties involved in creating professional toolbars, menus, and status bars, notably the lack of flexibility and extensibility in the controls. The flexible and extensible new ToolStrip control in the .NET 2.0 framework promises to solve those problems by changing the underlying model.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Building Context Menus
In the very first code snippet in this article, clicking the New menu opened a new form, which you haven't seen yet. You'll implement the ToolStrip, ContextMenuStrip and StatusStrip here.

Create the new Form and rename it to FirstForm. Ensure that the code sets the new form's MDIParent property before calling its Show method. I've repeated the first code snippet below, for clarity.

ToolStripMenuItem fileitem = new ToolStripMenuItem(); fileitem.Text = "&File"; ToolStripMenuItem newitem = new ToolStripMenuItem(); newitem.Text = "&New"; newitem.Click += delegate { FirstForm f = new FirstForm(); f.MdiParent = this; f.Show(); }; fileitem.DropDownItems.Add(newitem );

Drag and drop a ToolStrip onto the FirstForm form and rename it to tlStrip. Also drag a ContextMenuStrip and rename it to cntxtMenu.

Implement the Department and Sub-department items the same way as you did with the MenuStrip in the earlier examples, but this time, associate them with the ContextMenuStrip instead. The downloadable sample code contains the complete implementation. Note that there's no difference between the MenuStrip and ContextMenuStrip implementation in terms of using the event model or the object model. This flexibility in the ToolStrip architecture helps you build reusable components/controls. Although this sample itself isn't generically reusable, it would be very easy to create an application configuration file that could implement this functionality.

Figure 5: Child Form ToolStrip and ContextMenuStrip: The figure shows how the completed ToolStrip and ContextMenuStrip controls look in the child form.
The ToolStrip implementation is very simple and uses a set of images in an ImageList. Figure 5 shows how the completed ToolStrip looks. Here's the code.

ToolStripButton lblQuest = new ToolStripButton(); lblQuest.Image = imgList.Images[1]; lblQuest.Text = "&Question"; lblQuest.Enabled = true; tlStrip.Items.Add(lblQuest); ToolStripButton lblExcla = new ToolStripButton(); lblExcla.Image = imgList.Images[2]; lblExcla.Text = "&Exclamation"; lblExcla.Enabled = true; tlStrip.Items.Add(lblExcla); ToolStripButton lblInfo = new ToolStripButton(); lblInfo.Image = imgList.Images[3]; lblInfo.Text = "&Information"; lblInfo.Enabled = true; tlStrip.Items.Add(lblInfo ); tlStrip.Items.Add(drp); tlStrip.Items.Add(mnudeptCombo); tlStrip.Raft = RaftingSides.Right;

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date