ToolBar Button Action : Toolbar « GUI Windows Forms « C# / CSharp Tutorial






ToolBar Button Action
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class ToolBarButtonAction : System.Windows.Forms.Form
{
  private System.Windows.Forms.ToolBar toolBar1;
  private System.Windows.Forms.ToolBarButton toolBarButton1;
  private System.Windows.Forms.ToolBarButton toolBarButton2;
  private System.Windows.Forms.ToolBarButton toolBarButton3;
  private System.Windows.Forms.ImageList imageList1;
  private System.Windows.Forms.MainMenu mainMenu1;
  private System.Windows.Forms.MenuItem menuItem1;
  private System.Windows.Forms.MenuItem Open;
  private System.Windows.Forms.MenuItem Test;
  private System.Windows.Forms.MenuItem Exit;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.TextBox textBox1;
  private System.ComponentModel.IContainer components;

  public ToolBarButtonAction()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }

  private void InitializeComponent()
  {
    this.components = new System.ComponentModel.Container();
    this.toolBar1 = new System.Windows.Forms.ToolBar();
    this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
    this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
    this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
    this.mainMenu1 = new System.Windows.Forms.MainMenu();
    this.menuItem1 = new System.Windows.Forms.MenuItem();
    this.Open = new System.Windows.Forms.MenuItem();
    this.Test = new System.Windows.Forms.MenuItem();
    this.Exit = new System.Windows.Forms.MenuItem();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // toolBar1
    // 
    this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
                                          this.toolBarButton1,
                                          this.toolBarButton2,
                                          this.toolBarButton3});
    this.toolBar1.DropDownArrows = true;
    this.toolBar1.ImageList = this.imageList1;
    this.toolBar1.Name = "toolBar1";
    this.toolBar1.ShowToolTips = true;
    this.toolBar1.Size = new System.Drawing.Size(292, 39);
    this.toolBar1.TabIndex = 0;
    this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
    // 
    // toolBarButton1
    // 
    this.toolBarButton1.ImageIndex = 0;
    this.toolBarButton1.Text = "Open";
    this.toolBarButton1.ToolTipText = "Opens a file";
    // 
    // toolBarButton2
    // 
    this.toolBarButton2.ImageIndex = 1;
    this.toolBarButton2.Text = "Test";
    this.toolBarButton2.ToolTipText = "Test";
    // 
    // toolBarButton3
    // 
    this.toolBarButton3.ImageIndex = 2;
    this.toolBarButton3.Text = "Exit";
    this.toolBarButton3.ToolTipText = "Close Program";

    // 
    // mainMenu1
    // 
    this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                          this.menuItem1});
    // 
    // menuItem1
    // 
    this.menuItem1.Index = 0;
    this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                          this.Open,
                                          this.Test,
                                          this.Exit});
    this.menuItem1.Text = "Main Menu Options";
    // 
    // Open
    // 
    this.Open.Index = 0;
    this.Open.Text = "Open";
    this.Open.Click += new System.EventHandler(this.OpenMenuItemClick);
    // 
    // Test
    // 
    this.Test.Index = 1;
    this.Test.Text = "Test";
    this.Test.Click += new System.EventHandler(this.TestMenuItemClick);
    // 
    // Exit
    // 
    this.Exit.Index = 2;
    this.Exit.Text = "Exit";
    this.Exit.Click += new System.EventHandler(this.ExitMenuItemClick);
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(8, 64);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(128, 32);
    this.button1.TabIndex = 1;
    this.button1.Text = "Open File Dialog";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(160, 72);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(120, 32);
    this.button2.TabIndex = 2;
    this.button2.Text = "FontDialog";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // button3
    // 
    this.button3.Location = new System.Drawing.Point(40, 120);
    this.button3.Name = "button3";
    this.button3.Size = new System.Drawing.Size(136, 32);
    this.button3.TabIndex = 3;
    this.button3.Text = "ColorDialog";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(88, 184);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(168, 20);
    this.textBox1.TabIndex = 4;
    this.textBox1.Text = "textBox1";
    // 
    // ToolBarButtonAction
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                    this.textBox1,
                                    this.button3,
                                    this.button2,
                                    this.button1,
                                    this.toolBar1});
    this.Menu = this.mainMenu1;
    this.Name = "ToolBarButtonAction";
    this.Text = "ToolBarButtonAction";
    this.Load += new System.EventHandler(this.ToolBarButtonAction_Load);
    this.ResumeLayout(false);

  }

  [STAThread]
  static void Main() 
  {
    Application.Run(new ToolBarButtonAction());
  }

  private void ToolBarButtonAction_Load(object sender, System.EventArgs e)
  {
  
  }

  private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
  {
    if ( e.Button == toolBarButton1 )
    {
      MessageBox.Show( "Open Button Clicked ");
    }
    if ( e.Button == toolBarButton2 )
    {
      MessageBox.Show( "Test Button Clicked ");
    }
    if ( e.Button == toolBarButton3 )
    {
      MessageBox.Show( "Exit Button Clicked ");
    } 

  }

  private void TestMenuItemClick(object sender, System.EventArgs e)
  {
    MessageBox.Show( "Test Menu ItemClicked ");
  }

  private void OpenMenuItemClick(object sender, System.EventArgs e)
  {
    MessageBox.Show( "Open Menu ItemClicked ");
  }

  private void ExitMenuItemClick(object sender, System.EventArgs e)
  {
    MessageBox.Show( "Exit Menu ItemClicked ");
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
    OpenFileDialog fdlg = new OpenFileDialog(); 
    fdlg.Title = "C# Corner Open File Dialog" ; 
    fdlg.InitialDirectory = @"c:\" ; 
    fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*" ; 
    fdlg.FilterIndex = 2 ; 
    fdlg.RestoreDirectory = true ; 
    if(fdlg.ShowDialog() == DialogResult.OK) 
    { 
      textBox1.Text = fdlg.FileName ; 
    }
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
    FontDialog fntDlg = new FontDialog(); 
    fntDlg.ShowColor = true; 
    if(fntDlg.ShowDialog() != DialogResult.Cancel ) 
    { 
      textBox1.Font = fntDlg.Font ; 
      textBox1.ForeColor = fntDlg.Color; 
    }
  }

  private void button3_Click(object sender, System.EventArgs e)
  {
    ColorDialog colorDlg = new ColorDialog();
    colorDlg.ShowDialog();
    textBox1.BackColor = colorDlg.Color;
    button1.BackColor = colorDlg.Color;
    button3.BackColor = colorDlg.Color;

  }
}








23.38.Toolbar
23.38.1.Toolbar with ComboBoxToolbar with ComboBox
23.38.2.ToolBar Linked With MenuToolBar Linked With Menu
23.38.3.ToolBar Button ActionToolBar Button Action
23.38.4.Add Button action to ToolBar ButtonAdd Button action to ToolBar Button
23.38.5.Add ToolTip to ToolBar ButtonAdd ToolTip to ToolBar Button
23.38.6.Create an ImageList and use it for ToolBarCreate an ImageList and use it for ToolBar
23.38.7.Add Image to ToolBar ButtonAdd Image to ToolBar Button
23.38.8.ToolBar Button border style: BorderStyle.Fixed3DToolBar Button border style: BorderStyle.Fixed3D
23.38.9.Cut, Copy Paste/Text Box with Toolbar
23.38.10.Designed Toolbar
23.38.11.ToolBar Button Style
23.38.12.Toolbar Styles