While wizards and property windows make creating toolbars easy, they do limit you in some interesting, if subtle ways. In the previous code, you set the Button's Tag property to a string (e.g., "New"), and in the event handler, you switched on that string:
private void toolBar1_ButtonClick( object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { switch ( e.Button.Tag.ToString( ) ) { case "New": mnuNew.PerformClick( ); break; case "Open": mnuFileOpen.PerformClick( ); break; case "Save": mnuFileSave.PerformClick( ); break; } }
A tag, however, can be any type of object. It would be nice to put a reference to the MenuItem itself into the tag; this would greatly simplify the event handler:
private void toolBar1_ButtonClick( object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { ToolBarButton btn = e.Button; MenuItem mi = (MenuItem) btn.Tag; mi.PerformClick( ); }
Creating the toolbar by hand is not much more difficult than creating it with Visual Studio .NET, as illustrated in Example 18-7 in C# and in Example 18-8 in VB.NET. In these examples, the modified code from the previous examples is highlighted. An analysis follows the code listings.
Example 18-7. Creating toolbars by hand in C# (ToolBarsByHandCS)
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace MenusVSCS05 { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem mnuNew; private System.Windows.Forms.MenuItem mnuFile; private System.Windows.Forms.MenuItem mnuFileOpen; private System.Windows.Forms.MenuItem mnuFileClose; private System.Windows.Forms.MenuItem mnuFileSave; private System.Windows.Forms.MenuItem mnuFileSaveAs; private System.Windows.Forms.MenuItem mnuEdit; private System.Windows.Forms.MenuItem mnuEditCopy; private System.Windows.Forms.MenuItem mnuEditPaste; private System.Windows.Forms.MenuItem mnuOption1; private System.Windows.Forms.MenuItem mnuOption2; private System.Windows.Forms.MenuItem mnuOption3; private System.Windows.Forms.MenuItem mnuROption1; private System.Windows.Forms.MenuItem mnuROption2; private System.Windows.Forms.MenuItem mnuROption3; private System.Windows.Forms.MenuItem mnuWindow; private System.Windows.Forms.MenuItem mnuOptions; private System.Windows.Forms.MenuItem mnuRadioOptions; private System.Windows.Forms.MenuItem mnuMenu1; private System.Windows.Forms.MenuItem mnuMenu11; private System.Windows.Forms.MenuItem mnuMenu12; private System.Windows.Forms.MenuItem mnuMenu13; private System.Windows.Forms.MenuItem mnuMenu14; private System.Windows.Forms.MenuItem mnuMenu2; private System.Windows.Forms.MenuItem mnuMenu21; private System.Windows.Forms.MenuItem mnuMenu22; private System.Windows.Forms.MenuItem mnuMenu23; private System.Windows.Forms.MenuItem mnuMenu24; private System.Windows.Forms.MenuItem mnuMerge; private System.Windows.Forms.MenuItem mnuODShazam; private string[ ] files ={@"c: empvote.bmp", @"c: empshazam.bmp"}; private System.Windows.Forms.MenuItem mnuODVote; private System.Windows.Forms.MenuItem mnuSpecial; private System.Windows.Forms.ToolBar toolBar1; private System.Windows.Forms.ImageList imgListFileButtons; private System.ComponentModel.Container components = null; public Form1( ) { InitializeComponent( ); CreateImageList( ); InitializeToolbar( ); } private void InitializeToolbar( ) { toolBar1 = new ToolBar( ); toolBar1.ImageList = imgListFileButtons; ToolBarButton btnNew = new ToolBarButton( ); btnNew.Tag = mnuNew; btnNew.Enabled = true; btnNew.ImageIndex = 0; // new file btnNew.Pushed = false; btnNew.Style = ToolBarButtonStyle.PushButton; btnNew.Text= "New"; btnNew.ToolTipText = "New document"; btnNew.Visible = true; toolBar1.Buttons.Add(btnNew); ToolBarButton btnOpen = new ToolBarButton( ); btnOpen.Tag = mnuFileOpen; btnOpen.Enabled = true; btnOpen.ImageIndex = 1; // open file btnOpen.Pushed = false; btnOpen.Style = ToolBarButtonStyle.PushButton; btnOpen.Text = "Open"; btnOpen.ToolTipText = "Open a document"; btnOpen.Visible = true; toolBar1.Buttons.Add(btnOpen); ToolBarButton btnSave = new ToolBarButton( ); btnSave.Tag = mnuFileSave; btnSave.Enabled = true; btnSave.ImageIndex = 3; // save file btnSave.Pushed = false; btnSave.Style = ToolBarButtonStyle.PushButton; btnSave.Text = "Save"; btnSave.ToolTipText = "Save document"; btnSave.Visible = true; toolBar1.Buttons.Add(btnSave); ComboBox cb = new ComboBox( ); cb.Left = 150; cb.Top = 5; cb.Items.Add("Alabama"); cb.Items.Add("Alaska"); cb.Items.Add("Arizona"); cb.Items.Add("Arkansas"); ToolTip tip = new ToolTip( ); tip.AutomaticDelay = 500; // sets the next three automatically // tip.AutoPopDelay = 10 times AutomaticDelay // tip.InitialDelay = AutomaticDelay //tip.ReshowDelay = 1/5 AutomaticDelay tip.ShowAlways = true; // display even if control is disabled tip.SetToolTip(cb,"Pick a state"); toolBar1.Controls.Add(cb); toolBar1.Parent = this; toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; toolBar1.DropDownArrows = true; toolBar1.Name = "toolBar1"; toolBar1.ShowToolTips = true; toolBar1.Size = new System.Drawing.Size(440, 41); toolBar1.TabIndex = 1; toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(toolBar1_ButtonClick); } private void CreateImageList( ) { imgListFileButtons = new ImageList( ); Image img; // Use an array to add filenames to the ImageList String[ ] arFiles = { @"C:Program FilesMicrosoft Visual Studio .NET 2003Common7GraphicsitmapsOffCtlBrLargeColorNew.bmp", @"C:Program FilesMicrosoft Visual Studio .NET 2003Common7GraphicsitmapsOffCtlBrLargeColorOpen.bmp", @"C:Program FilesMicrosoft Visual Studio .NET 2003Common7GraphicsitmapsOffCtlBrLargeColorCut.bmp", @"C:Program FilesMicrosoft Visual Studio .NET 2003Common7GraphicsitmapsOffCtlBrLargeColorSave.bmp" }; for (int i = 0; i < arFiles.Length; i++) { img = Image.FromFile(arFiles[i]); imgListFileButtons.Images.Add(img); } } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose( ); } } base.Dispose( disposing ); } #region Windows Form Designer generated code #endregion [STAThread] static void Main( ) { Application.Run(new Form1( )); } private void mnuNew_Click(object sender, System.EventArgs e) { MDIChild newMDIChild = new MDIChild( ); newMDIChild.MdiParent = this; newMDIChild.Show( ); } private void mnuFileOpen_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked File Open", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void mnuFileClose_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked File Close", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void Option_Click(object sender, System.EventArgs e) { MenuItem item = sender as MenuItem; if ( item != null ) { item.Checked = ! item.Checked; } } private void RadioOption_Click(object sender, System.EventArgs e) { MenuItem item = sender as MenuItem; if ( item != null ) { Menu parent = item.Parent; foreach ( MenuItem mi in parent.MenuItems ) mi.Checked = false; item.Checked = true; } } private void mnuEditCopy_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked Edit Copy", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void mnuEditPaste_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked Edit Paste", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void mnuFileSave_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked Save", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void mnuFileSaveAs_Click(object sender, System.EventArgs e) { MessageBox.Show ("You clicked SaveAs", "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } private void mnuMerge_Click(object sender, System.EventArgs e) { MenuItem item = sender as MenuItem; if ( item != null ) { item.Parent.MergeMenu(mnuMenu2); } } private void OnDrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { Image img = Image.FromFile(files[e.Index]); Rectangle r = e.Bounds; Pen pen = new Pen(e.BackColor,2); r.Inflate(-6,-6); e.Graphics.DrawRectangle(pen,r); e.Graphics.DrawImage(img,r); } private void OnMeasureItem( object sender, System.Windows.Forms.MeasureItemEventArgs e) { Image img = Image.FromFile(files[e.Index]); e.ItemHeight = img.Height; e.ItemWidth = img.Width; } private void mnuODDraw_Click(object sender, System.EventArgs e) { MenuItem item = sender as MenuItem; if ( item != null ) { string choice = item.Text; MessageBox.Show ("You clicked " + choice, "Menu Event Tester", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } private void toolBar1_ButtonClick( object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { ToolBarButton btn = e.Button; MenuItem mi = (MenuItem) btn.Tag; mi.PerformClick( ); } } }
Example 18-8. Creating toolbars by hand in VB.NET (ToolBarsByHandVB)
Public Class Form1 Inherits System.Windows.Forms.Form Private files( ) As String = {"c: empvote.bmp", "c: empshazam.bmp"} Friend WithEvents toolBar1 As ToolBar Private imgListFileButtons As ImageList #Region " Windows Form Designer generated code " Public Sub New( ) MyBase.New( ) 'This call is required by the Windows Form Designer. InitializeComponent( ) CreateImageList( ) InitializeToolbar( ) End Sub #End Region Private Sub InitializeToolbar( ) toolBar1 = New ToolBar( ) toolBar1.ImageList = imgListFileButtons Dim btnNew As New ToolBarButton( ) btnNew.Tag = mnuNew btnNew.Enabled = True btnNew.ImageIndex = 0 ' new file btnNew.Pushed = False btnNew.Style = ToolBarButtonStyle.PushButton btnNew.Text = "New" btnNew.ToolTipText = "New document" btnNew.Visible = True toolBar1.Buttons.Add(btnNew) Dim btnOpen As New ToolBarButton( ) btnOpen.Tag = mnuFileOpen btnOpen.Enabled = True btnOpen.ImageIndex = 1 ' open file btnOpen.Pushed = False btnOpen.Style = ToolBarButtonStyle.PushButton btnOpen.Text = "Open" btnOpen.ToolTipText = "Open a document" btnOpen.Visible = True toolBar1.Buttons.Add(btnOpen) Dim btnSave As New ToolBarButton( ) btnSave.Tag = mnuFileSave btnSave.Enabled = True btnSave.ImageIndex = 3 ' save file btnSave.Pushed = False btnSave.Style = ToolBarButtonStyle.PushButton btnSave.Text = "Save" btnSave.ToolTipText = "Save document" btnSave.Visible = True toolBar1.Buttons.Add(btnSave) Dim cb As New ComboBox( ) cb.Left = 150 cb.Top = 5 cb.Items.Add("Alabama") cb.Items.Add("Alaska") cb.Items.Add("Arizona") cb.Items.Add("Arkansas") Dim tip As New ToolTip( ) tip.AutomaticDelay = 500 ' sets the next three automatically ' tip.AutoPopDelay = 10 times AutomaticDelay ' tip.InitialDelay = AutomaticDelay 'tip.ReshowDelay = 1/5 AutomaticDelay tip.ShowAlways = True ' display even if control is disabled tip.SetToolTip(cb, "Pick a state") toolBar1.Controls.Add(cb) toolBar1.Parent = Me toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D toolBar1.DropDownArrows = True toolBar1.Name = "toolBar1" toolBar1.ShowToolTips = True toolBar1.Size = New System.Drawing.Size(440, 41) toolBar1.TabIndex = 1 End Sub Private Sub CreateImageList( ) imgListFileButtons = New ImageList( ) Dim img As Image Dim arFiles( ) As String = {"C:Program FilesMicrosoft Visual Studio .NET 2003Common7Graphicsitmaps OffCtlBrLargeColorNew.bmp", _ "C:Program FilesMicrosoft Visual Studio .NET 2003Common7Graphicsitmaps OffCtlBrLargeColorOpen.bmp", _ "C:Program FilesMicrosoft Visual Studio .NET 2003Common7Graphicsitmaps OffCtlBrLargeColorCut.bmp", _ "C:Program FilesMicrosoft Visual Studio .NET 2003Common7Graphicsitmaps OffCtlBrLargeColorSave.bmp"} Dim i As Integer For i = 0 To arFiles.Length - 1 img = Image.FromFile(arFiles(i)) imgListFileButtons.Images.Add(img) Next End Sub Private Sub mnuNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles mnuNew.Click Dim newMDIChild As New MDIChild( ) newMDIChild.MdiParent = Me newMDIChild.Show( ) End Sub Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles mnuFileOpen.Click MessageBox.Show("You clicked file open!", _ "Menu event tester", _ MessageBoxButtons.OK, _ MessageBoxIcon.Information) End Sub Private Sub mnuFileClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles mnuFileClose.Click MessageBox.Show("You clicked file close!", "Menu event tester", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub Private Sub mnuEditCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles mnuEditCopy.Click MessageBox.Show("You clicked Edit copy!", "Menu event tester", MessageBoxButtons.OK, MessageBoxIcon.1-fm2xml) End Sub Private Sub mnuEditPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles mnuEditPaste.Click MessageBox.Show("You clicked Edit paste!", "Menu event tester", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub Private Sub Option_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles mnuOption1.Click, mnuOption2.Click, mnuOption3.Click Dim item As MenuItem = CType(sender, MenuItem) item.Checked = Not item.Checked End Sub Private Sub RadioOption_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles mnuRadioOption1.Click, mnuRadioOption2.Click, mnuRadioOption3.Click Dim item As MenuItem = CType(sender, MenuItem) Dim parent As Menu = item.Parent Dim tempMi As MenuItem For Each tempMi In parent.MenuItems tempMi.Checked = False Next item.Checked = True End Sub Private Sub mnuMerge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim item As MenuItem = CType(sender, MenuItem) item.Parent.MergeMenu(mnuMenu2) End Sub Private Sub mnuODVote_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) _ Handles mnuODVote.DrawItem, mnuODShazam.DrawItem Dim img As Image img = Image.FromFile(files(e.Index)) Dim r As Rectangle r = e.Bounds Dim p As Pen = New Pen(e.BackColor, 2) r.Inflate(-6, -6) e.Graphics.DrawRectangle(p, r) e.Graphics.DrawImage(img, r) End Sub Private Sub mnuODVote_MeasureItem( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.MeasureItemEventArgs) _ Handles mnuODVote.MeasureItem, mnuODShazam.MeasureItem Dim img As Image img = Image.FromFile(files(e.Index)) e.ItemHeight = img.Height e.ItemWidth = img.Width End Sub Private Sub mnuODDraw_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles mnuODVote.Click, mnuODShazam.Click Dim item As MenuItem = CType(sender, MenuItem) Dim choice As String = item.Text MessageBox.Show("You clicked " & choice, _ "Menu Event Tester", MessageBoxButtons.OK, _ MessageBoxIcon.Asterisk) End Sub Private Sub toolBar1_ButtonClick( _ ByVal sender As Object, _ ByVal e As ToolBarButtonClickEventArgs) _ Handles toolBar1.ButtonClick Dim btn As ToolBarButton = e.Button Dim mi As MenuItem = CType(btn.Tag, MenuItem) mi.PerformClick( ) End Sub End Class
|
18.4.1 Creating the Toolbar by Hand: Analysis
You begin by creating two private member variables of the Form class: a toolbar, and an ImageList to attach to that toolbar:
private System.Windows.Forms.ToolBar toolBar1; private System.Windows.Forms.ImageList imgListFileButtons;
(Hidden inside the Windows Form Designer generated code, along with other member variables):
Friend WithEvents imgListFileButtons As System.Windows.Forms.ImageList Friend WithEvents ToolBar1 As System.Windows.Forms.ToolBar
You add two method calls to the constructor: one to create the ImageList and the other to initialize the ToolBar:
public Form1( ) { InitializeComponent( ); CreateImageList( ); InitializeToolbar( ); }
Public Sub New( ) MyBase.New( ) InitializeComponent( ) CreateImageList( ) InitializeToolbar( ) End Sub
Creating the ImageList is straightforward. Initialize a new empty image list and create a local variable to hold an Image object.
imgListFileButtons = new ImageList( ); Image img;
Then create an array to hold the four filenames for the bitmaps supplied by Microsoft:
String[ ] arFiles = { @"C:Program FilesMicrosoft Visual Studio .NET 2003Common7 GraphicsitmapsOffCtlBrLargeColorNew.bmp", @"C:Program FilesMicrosoft Visual Studio .NET 2003Common7GraphicsitmapsOffCtlBrLargeColorOpen.bmp", @"C:Program FilesMicrosoft Visual Studio .NET 2003Common7GraphicsitmapsOffCtlBrLargeColor Cut.bmp", @"C:Program FilesMicrosoft Visual Studio .NET 2003Common7Graphicsitmaps OffCtlBrLargeColorSave.bmp" };
Dim arFiles( ) As String = {"C:Program FilesMicrosoft Visual Studio .NET 2003Common7 GraphicsitmapsOffCtlBrLargeColorNew.bmp", _ "C:Program FilesMicrosoft Visual Studio .NET 2003Common7GraphicsitmapsOffCtlBr LargeColorOpen.bmp", _ "C:Program FilesMicrosoft Visual Studio .NET 2003Common7GraphicsitmapsOffCtlBr LargeColorCut.bmp", _ "C:Program FilesMicrosoft Visual Studio .NET 2003Common7GraphicsitmapsOffCtlBr LargeColorSave.bmp"}
Finally, create a reference to an image from each of the filenames, and add that image to the ImageList's collection of images:
for (int i = 0; i < arFiles.Length; i++) { img = Image.FromFile(arFiles[i]); imgListFileButtons.Images.Add(img); }
Initializing the toolbar is more tedious, but not terribly difficult. Begin by instantiating the toolbar and setting its ImageList property to the ImageList you just created:
toolBar1 = new ToolBar( ); toolBar1.ImageList = imgListFileButtons;
Next, create a button and set its properties:
ToolBarButton btnNew = new ToolBarButton( ); btnNew.Tag = mnuNew; btnNew.Enabled = true; btnNew.ImageIndex = 0; // new file btnNew.Pushed = false; btnNew.Style = ToolBarButtonStyle.PushButton; btnNew.Text= "New"; btnNew.ToolTipText = "New document"; btnNew.Visible = true;
Notice that the ImageIndex property is set to the appropriate index into the ImageList that you assigned to the ToolBar'sImageList property. Also notice that in this example you are not setting the Tag property to a string, but you are setting the Tag property to a reference to a MenuItem.
Once all properties are set, you can add the button to the ToolBar's Buttons collection:
toolBar1.Buttons.Add(btnNew);
Repeat these steps for the next two buttons, changing only which MenuItem and ImageIndex you must use, along with the appropriate changes to the Text and the ToolTip text.
ToolBarButton btnOpen = new ToolBarButton( ); btnOpen.Tag = mnuFileOpen; btnOpen.Enabled = true; btnOpen.ImageIndex = 1; // open file btnOpen.Pushed = false; btnOpen.Style = ToolBarButtonStyle.PushButton; btnOpen.Text = "Open"; btnOpen.ToolTipText = "Open a document"; btnOpen.Visible = true; toolBar1.Buttons.Add(btnOpen); ToolBarButton btnSave = new ToolBarButton( ); btnSave.Tag = mnuFileSave; btnSave.Enabled = true; btnSave.ImageIndex = 3; // save file btnSave.Pushed = false; btnSave.Style = ToolBarButtonStyle.PushButton; btnSave.Text = "Save"; btnSave.ToolTipText = "Save document"; btnSave.Visible = true; toolBar1.Buttons.Add(btnSave);
18.4.1.1 Adding ToolTips and setting their properties
You are now ready to add the Combo box. This is unchanged from the previous examples, except this time you'll add a ToolTip to the Combo box as well:
ComboBox cb = new ComboBox( ); cb.Left = 150; cb.Top = 5; cb.Items.Add("Alabama"); cb.Items.Add("Alaska"); cb.Items.Add("Arizona"); cb.Items.Add("Arkansas"); ToolTip tip = new ToolTip( );
The ToolTip object itself has many useful properties. You can set the InitialDelay (how long the system waits before displaying the tip 500 milliseconds by default), the AutoPopDelay (how long the tip is displayed), and the ReshowDelay (how long before the tip is redisplayed when you return to the button). Alternatively, you can just set the AutomaticDelay, and the other delays are set for you. The AutoPopDelay is set to 10 times the AutomaticDelay, while the ReshowDelay is set to 1/5 of the AutomaticDelay, and the InitialDelay is set to the same value as the AutomaticDelay.
tip.AutomaticDelay = 500; // sets the next three automatically (milliseconds) // tip.AutoPopDelay = 10 times AutomaticDelay (5 seconds) // tip.InitialDelay = AutomaticDelay (1/2 second) //tip.ReshowDelay = 1/5 AutomaticDelay (1/10 second)
Setting the ShowAlways property to true instructs the tip to be displayed whether or not the control is disabled.
tip.ShowAlways = true; // display even if control is disabled
Finally, you are ready to attach the ToolTip to the Combo box. Surprisingly, you do so by calling the SetToolTip method on the ToolTip itself, passing in two argumentsthe control to set the ToolTip on and the text to display:
tip.SetToolTip(cb,"Pick a state");
You are now ready to add the Combo box to the ToolBar:
toolBar1.Controls.Add(cb);
18.4.1.2 Setting properties of the Toolbar
Once the controls have been added to the ToolBar, you can set properties of the Toolbar itself. Begin by setting the Parent control of the Toolbar to the form:
toolBar1.Parent = this;
toolBar1.Parent = Me
You are now ready to set the border style, how DropDownArrows will be handled, and so forth:
toolBar1.Parent = this; toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; toolBar1.DropDownArrows = true; toolBar1.Name = "toolBar1"; toolBar1.ShowToolTips = true; toolBar1.Size = new System.Drawing.Size(440, 41); toolBar1.TabIndex = 1;
Finally, if you are doing this in C#, you must set up the event handler:
toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler( toolBar1_ButtonClick);
18.4.1.3 Handling ToolBar button clicks
Now that you've stashed the appropriate MenuItem into the Button's Tag property, your event handler is much simpler. First extract the Button itself from the ToolBarButtonClickEventArgs argument:
private void toolBar1_ButtonClick( object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { ToolBarButton btn = e.Button;
Private Sub toolBar1_ButtonClick( _ ByVal sender As Object, _ ByVal e As ToolBarButtonClickEventArgs) _ Handles toolBar1.ButtonClick Dim btn As ToolBarButton = e.Button
With the Button in hand, extract the Tag, which you are free to cast to a MenuItem:
MenuItem mi = (MenuItem) btn.Tag;
Dim mi As MenuItem = CType(btn.Tag, MenuItem)
You can now tell that MenuItem to act as if it were clicked:
mi.PerformClick( );
Windows Forms and the .NET Framework
Getting Started
Visual Studio .NET
Events
Windows Forms
Dialog Boxes
Controls: The Base Class
Mouse Interaction
Text and Fonts
Drawing and GDI+
Labels and Buttons
Text Controls
Other Basic Controls
TreeView and ListView
List Controls
Date and Time Controls
Custom Controls
Menus and Bars
ADO.NET
Updating ADO.NET
Exceptions and Debugging
Configuration and Deployment