Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I determine which button in a ToolBar is clicked?

When a button is clicked on a ToolBar, the click is sent to the ToolBar not the button. In the ToolBar.ButtonClick event handler, you need add code to check the Button property of the ToolBarButtonClickEventArgs passed to the event handler to determine which button was clicked.

[C#]

using System.Windows.Forms;

private void toolBar1_ButtonClick( object sender, ToolBarButtonClickEventArgs e )
{
  if ( e.Button == toolBarButton1 )
    MessageBox.Show( "One" );
  else if ( e.Button == toolBarButton2 )
    MessageBox.Show( "Two" );
  else if ( e.Button == toolBarButton3 )
    MessageBox.Show( "Three" );
  else
    MessageBox.Show( "Unknown" );
}

[Visual Basic]

Imports System
Imports System.Windows.Forms

Private Sub ToolBar1_ButtonClick(ByVal sender As Object, _
    ByVal e As ToolBarButtonClickEventArgs) _
    Handles ToolBar1.ButtonClick
  If e.Button Is ToolBarButton1 Then
    MessageBox.Show("One")
  ElseIf e.Button Is ToolBarButton2 Then
    MessageBox.Show("Two")
  ElseIf e.Button Is ToolBarButton3 Then
    MessageBox.Show("Three")
  Else
    MessageBox.Show("Unknown")
  End If
End Sub

George Shepherd, Syncfusion, and Stuart Celarier, Fern Creek



Page view counter