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 3

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
Extending the Menu
Next, assume that you want to implement a menu where the dropdown items get their data from a backend database. In the sample code, check out the DataStore class, which constructs a Dataset with two DataTables. The first table holds a list of Departments and the second a list of Sub-departments.

The ToolStripDropDownButton class is a button that you can associate with an element of type ToolStripDropDown. When a user clicks the button the control pops up the ToolStripDropDown items associated with it. So, to use the class to drop down the list of Departments from the table in the DataSet, you first declare a ToolStripDropDownButton. Then, create a ToolStripDropDownMenu (derived from ToolStripDropDown), and add a ToolStripButton item for each record found in the Department table as shown below.

 
Figure 3. ToolStripDropDownButton Implementation. The "Departments" ToolStripDropDownButton displays a list of department names drawn from a database. The figure shows the effect of applying the various ToolStripTextDirection variations.

ToolStripMenuItem hrMenu = new ToolStripMenuItem(); hrMenu.Text = "&Human Resources"; /****Get the dropdown and attach buttons*************/ ToolStripDropDownButton deptButton = new ToolStripDropDownButton(); ToolStripDropDownMenu dept = new ToolStripDropDownMenu(); dept.DefaultDropDownDirection = ToolStripDropDownDirection.AboveRight; DataTable deptTable = DataStore.GetDepartment(); foreach (DataRow record in deptTable.Rows ) { ToolStripButton item = new ToolStripButton(); item.Tag = record; item.Text = record["DepartmentName"].ToString(); item.TextAlign = ContentAlignment.BottomCenter; item.TextDirection = ToolStripTextDirection.Horizontal; item.Width = 200; dept.Items.Add(item); } dept.Width = 200; dept.Height = 40; deptButton.RightToLeft = RightToLeft.Yes ; deptButton.DropDown = dept; deptButton.Enabled = true; deptButton.Height = 40; deptButton.Width = 200; deptButton.Text = "&Departments"; hrMenu.DropDownItems.Add(deptButton );

You can customize each of these ToolStripItems as required. The TextDirection property is interesting; try setting it to the values Vertical90 and Vertical270 to see how it affects the text orientation. After adding all the ToolStripButton elements to the ToolStripDropDown set the ToolStripDropDown's DropDown property to associate the DropDown elements with the ToolStripDropDownButton. Figure 3 shows how this looks at runtime.

You can embed a ComboBox in a tool strip using the ToolStripComboBox control. The following implementation is simple and similar to using a standard ComboBox. Note that ToolStripComboBox is a specialized implementation of ToolStripControlHost, exposing a hosted ComboBox. You can set the ComboBox DropDownStyle, its BackColor, and ForeColor, etc. The sample adds Sub-department names to the Items collection of the ComboBox. Figure 4 shows the code running in a Windows Form.

 
Figure 4: The ToolStripComboBox Implementation. Here's how the ToolStripComboBox looks at runtime.

/********Drop down box menucontrol*************/ ToolStripComboBox cboSubDept = new ToolStripComboBox(); cboSubDept.DropDownHeight = 30; cboSubDept.DropDownWidth = 200; cboSubDept.DropDownStyle = ComboBoxStyle.DropDownList; cboSubDept.ComboBox.BackColor = Color.DarkViolet ; cboSubDept.ComboBox.ForeColor = Color.White ; cboSubDept.ComboBox.ItemHeight = 30; DataTable subDeptTable = DataStore.GetSubDepartment(); foreach (DataRow record in subDeptTable.Rows) { cboSubDept.ComboBox.Items.Add( record["SubDepartmentName"].ToString()); } cboSubDept.ComboBox.SelectedIndex = 1; cboSubDept.ComboBox.SelectedValueChanged += delegate { MessageBox.Show("You selected " + cboSubDept.SelectedItem.ToString(), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); }; cboSubDept.ComboBox.SelectedValue = 1; hrMenu.DropDownItems.Add(cboSubDept);



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