Mega Code Archive

 
Categories / C# Tutorial / GUI Windows Forms
 

Toolbar with ComboBox

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class ToolBarForm : System.Windows.Forms.Form {   private System.Windows.Forms.ToolBar toolBar1;   private System.Windows.Forms.ToolBarButton btnNew;   private System.Windows.Forms.ToolBarButton btnOpen;   private System.Windows.Forms.ToolBarButton btnSave;   private System.ComponentModel.IContainer components;   public ToolBarForm()   {     InitializeComponent();     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");     toolBar1.Controls.Add(cb);   }   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.btnNew = new System.Windows.Forms.ToolBarButton();     this.btnOpen = new System.Windows.Forms.ToolBarButton();     this.btnSave = new System.Windows.Forms.ToolBarButton();     this.SuspendLayout();     this.toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;     this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {                                           this.btnNew,                                           this.btnOpen,                                           this.btnSave});     this.toolBar1.DropDownArrows = true;     this.toolBar1.Name = "toolBar1";     this.toolBar1.ShowToolTips = true;     this.toolBar1.Size = new System.Drawing.Size(440, 41);     this.toolBar1.TabIndex = 1;     this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);     //      // btnNew     //      this.btnNew.ImageIndex = 0;     this.btnNew.Tag = "New";     this.btnNew.Text = "New";     this.btnNew.ToolTipText = "New Document";     //      // btnOpen     //      this.btnOpen.ImageIndex = 1;     this.btnOpen.Tag = "Open";     this.btnOpen.Text = "Open";     this.btnOpen.ToolTipText = "Open a document";     //      // btnSave     //      this.btnSave.ImageIndex = 3;     this.btnSave.Tag = "Save";     this.btnSave.Text = "Save";     this.btnSave.ToolTipText = "Save document";     //      // ToolBarForm     //      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);     this.ClientSize = new System.Drawing.Size(440, 126);     this.Controls.AddRange(new System.Windows.Forms.Control[] {                                     this.toolBar1});     this.IsMdiContainer = true;     this.ResumeLayout(false);   }   [STAThread]   static void Main()    {     Application.Run(new ToolBarForm());   }   private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)   {     switch ( e.Button.Tag.ToString() )     {       case "New":         Console.WriteLine("New");         break;       case "Open":         Console.WriteLine("Open");           break;       case "Save":         Console.WriteLine("Save");         break;     }   } }