Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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

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.


advertisement
Going Beyond Simple Menus
The next example uses a ToolStripLabel element. You use this element to display read-only normal text, an image, or a hyperlink.

/*********Add Separator******************/ ToolStripSeparator sep = new ToolStripSeparator(); hrMenu.DropDownItems.Add(sep); /**************************************/ /********Add hyperlinks*************/ ToolStripLabel lblMore = new ToolStripLabel(); lblMore.IsLink = true; lblMore.LinkBehavior = LinkBehavior.AlwaysUnderline; lblMore.LinkColor = Color.DarkViolet; lblMore.VisitedLinkColor = Color.LightBlue; lblMore.Text = "Click for more.."; lblMore.Click += delegate { MessageBox.Show("You selected link", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); }; hrMenu.DropDownItems.Add(lblMore); mnuStrip.Items.Add(hrMenu); /******************************************/

The preceding code snippet illustrates some important properties. The IsLink property ensures that the label type is rendered as a link. LinkBehavior lets you configure the label's UI using self-describing values such as AlwaysUnderLine and HoverUnderline. You can also set the default LinkColor and VisitedLinkColor properties.

The final menu item in the sample code adds a ToolStripTextBox control with specific TextAlignment and CharacterCasing property settings. Although not shown in the sample code, you may also want to look at the AutoCompleteSource property, as it can help you implement smart controls.

A Raft of New Usability Features
At the point, you have a fully functional MenuStrip that illustrates the new .NET 2.0 UI ToolStrip features; but to provide a truly usable toolbar, you need to implement Rafting. Rafting is the process of sliding a toolbar horizontally or vertically to place it in a particular location relative to other toolbars that share the same screen real estate. The following example uses a ContextMenuStrip to handle the rafting of the completed ToolStrip. Drag and drop a ContextMenuStrip and rename it to stripctxtMenu. Then add four ToolStripButton controls to the ContextMenuStrip as shown in the following code snippet.

void SetContextMenuStrip() { ToolStripButton btnTop=new ToolStripButton(); btnTop.Text = "&Top"; btnTop.Click +=delegate {mnuStrip.Raft = RaftingSides.Top ; }; ToolStripButton btnBottom = new ToolStripButton(); btnBottom.Text = "&Bottom"; btnBottom.Click += delegate {mnuStrip.Raft = RaftingSides.Bottom; }; ToolStripButton btnLeft = new ToolStripButton(); btnLeft.Text = "&Left"; btnLeft.Click += delegate {mnuStrip.Raft = RaftingSides.Left; }; ToolStripButton btnRight = new ToolStripButton(); btnRight.Text = "&Right"; btnRight.Click += delegate {mnuStrip.Raft = RaftingSides.Right; }; stripctxtMenu.Items.Add(btnTop); stripctxtMenu.Items.Add(btnLeft); stripctxtMenu.Items.Add(btnRight); stripctxtMenu.Items.Add(btnBottom); mnuStrip.ContextMenuStrip = stripctxtMenu; }

Notice that the anonymous methods associated with each of the button's Click event use the RaftingSides enumeration to set the ToolStrip menu's Raft property. The last line of the snippet associates the MenuStrip's ContextMenuStrip property with the new ContextMenuStrip. The new ContextMenuStrip controls how the MenuStrip rafts. To test it, right click on the MenuStrip and select the appropriate Raft setting, then notice the way you can move the ToolStrip.



Comment and Contribute

 

 

 

 

 


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

 

 

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