8.22 Capturing ToolBar Button Clicks

 <  Day Day Up  >  

8.22 Capturing ToolBar Button Clicks

You need to determine which button on a ToolBar control was clicked by the user .


Technique

You cannot create individual event handlers for each toolbar button. Therefore, create an event handler for the ToolBar object's ButtonClick method. There are two options you can use to determine which button was clicked. Each ToolBarButton object has an associated Tag property whose data type is System.Object . You can use this user-defined object however you see fit. For instance, by placing a string in the Tag property, you can create a switch statement that compares the Button.Tag property of the ToolBarButtonClickEventArgs object with the strings in the Tag property, as shown in Listing 8.7.

Listing 8.7 Using Tag for Custom Data
 private void toolBar1_ButtonClick(object sender,     System.Windows.Forms.ToolBarButtonClickEventArgs e) {     switch( e.Button.Tag.ToString() )     {         case( "New" ):         {             richTextBox1.Clear();             break;         }         case( "Open" ):         {             if( openFileDialog1.ShowDialog() == DialogResult.Cancel )                 return;             richTextBox1.Clear();             richTextBox1.LoadFile( openFileDialog1.FileName );             break;         }         case( "Cut" ):         {             richTextBox1.Cut();             break;         }         case( "Copy" ):         {             richTextBox1.Copy();             break;         }         case( "Paste" ):         {             richTextBox1.Paste();             break;         }         default:         {             break;         }     } } 

Another option is to use the IndexOf method of the Buttons collection of the ToolBar object, passing it the Button property of the event argument's object. This returns an index value that you can use to determine which button was clicked, as shown in Listing 8.8.

Listing 8.8 Determining the Clicked ToolBar Button
 private void toolBar1_ButtonClick(object sender,     System.Windows.Forms.ToolBarButtonClickEventArgs e) {     switch( toolBar1.Buttons.IndexOf(e.Button) )     {         case( 0 ):         {             richTextBox1.Clear();             break;         }         case( 1 ):         {             if( openFileDialog1.ShowDialog() == DialogResult.Cancel )                 return;             richTextBox1.Clear();             richTextBox1.LoadFile( openFileDialog1.FileName );             break;         }         case( 2 ):         {             richTextBox1.Cut();             break;         }         case( 3 ):         {             richTextBox1.Copy();             break;         }         case( 4 ):         {             richTextBox1.Paste();             break;         }         default:         {             break;         }     } } 

Comments

ToolBar events are fired from the toolbar itself and not any of its individual buttons. Any event handlers you create that are fired as a result of a toolbar button interaction must use logic to determine which button was manipulated. The ButtonClick event defined in the ToolBar control contains an event argument parameter that you can use for this determination.

The code in the "Technique" section shows two ways you can determine whether a toolbar button was clicked. One technique takes advantage of the Tag property of each toolbar button, and the other uses information about the index into the Button collection to make the determination. Although both are valid methods of doing the same thing, the Tag method has a distinct advantage. If you are creating an application that models the toolbar behavior of some other application in which buttons can dynamically be added or removed by a user during runtime for customization purposes, you are not always guaranteed that a toolbar button will have the same index value in the Buttons collection. The second method then associates the wrong commands with the wrong toolbar buttons. By using the Tag method, you are guaranteed that the logic for a single button is always called, regardless of where on the toolbar the button resides. Also, ensure that you create an event handler for the ButtonClick event to capture toolbar button clicks rather than the Click event, which fires when any portion of the ToolBar control is clicked.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net