Csharp/CSharp Tutorial/GUI Windows Forms/Toolbar
Содержание
- 1 Add Button action to ToolBar Button
- 2 Add Image to ToolBar Button
- 3 Add ToolTip to ToolBar Button
- 4 Create an ImageList and use it for ToolBar
- 5 Cut, Copy Paste/Text Box with Toolbar
- 6 Designed Toolbar
- 7 ToolBar Button Action
- 8 ToolBar Button border style: BorderStyle.Fixed3D
- 9 ToolBar Button Style
- 10 ToolBar Linked With Menu
- 11 Toolbar Styles
- 12 Toolbar with ComboBox
Add Button action to ToolBar Button
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class ToolBarButtonActioin : System.Windows.Forms.Form {
private ImageList toolBarIcons; private ToolBarButton tbExitButton; private ToolBarButton tbSaveButton; private ToolBar toolBar; private System.ruponentModel.Container components = null; public ToolBarButtonActioin() { this.ruponents = new System.ruponentModel.Container(); this.Size = new System.Drawing.Size(300,300);
toolBar = new ToolBar(); toolBarIcons = new ImageList(); tbSaveButton = new ToolBarButton(); tbExitButton = new ToolBarButton(); tbSaveButton.ImageIndex = 0; tbSaveButton.ToolTipText = "Save"; tbExitButton.ImageIndex = 1; tbExitButton.ToolTipText = "Exit"; toolBar.ImageList = toolBarIcons; toolBar.Size = new System.Drawing.Size(272, 28); toolBar.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; toolBar.ShowToolTips = true; toolBar.Buttons.AddRange(new ToolBarButton[] {tbSaveButton, tbExitButton}); toolBar.ButtonClick += new ToolBarButtonClickEventHandler(ToolBar_Clicked); toolBarIcons.ImageSize = new System.Drawing.Size(32, 32); toolBarIcons.Images.Add(new Icon("filesave.ico")); toolBarIcons.Images.Add(new Icon("fileexit.ico")); toolBarIcons.ColorDepth = ColorDepth.Depth16Bit; toolBarIcons.TransparentColor = System.Drawing.Color.Transparent; this.Controls.Add(toolBar); } [STAThread] static void Main() { Application.Run(new ToolBarButtonActioin()); } private void ToolBar_Clicked(object sender, ToolBarButtonClickEventArgs e) { MessageBox.Show(e.Button.ToolTipText); }
}</source>
Add Image to ToolBar Button
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class ImageListCreateAndAddToToolBar : System.Windows.Forms.Form {
private ImageList toolBarIcons; private ToolBarButton tbExitButton; private ToolBarButton tbSaveButton; private ToolBar toolBar; private System.ruponentModel.Container components = null; public ImageListCreateAndAddToToolBar() { this.ruponents = new System.ruponentModel.Container(); this.Size = new System.Drawing.Size(300,300); toolBar = new ToolBar(); toolBarIcons = new ImageList(); tbSaveButton = new ToolBarButton(); tbExitButton = new ToolBarButton(); tbSaveButton.ImageIndex = 0; tbExitButton.ImageIndex = 1; toolBar.ImageList = toolBarIcons; toolBar.Size = new System.Drawing.Size(272, 28); toolBar.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; toolBar.ShowToolTips = true; toolBar.Buttons.AddRange(new ToolBarButton[] {tbSaveButton, tbExitButton}); toolBar.ButtonClick += new ToolBarButtonClickEventHandler(ToolBar_Clicked); toolBarIcons.ImageSize = new System.Drawing.Size(32, 32); toolBarIcons.Images.Add(new Icon("filesave.ico")); toolBarIcons.Images.Add(new Icon("fileexit.ico")); toolBarIcons.ColorDepth = ColorDepth.Depth16Bit; toolBarIcons.TransparentColor = System.Drawing.Color.Transparent; this.Controls.Add(toolBar); } [STAThread] static void Main() { Application.Run(new ImageListCreateAndAddToToolBar()); } private void ToolBar_Clicked(object sender, ToolBarButtonClickEventArgs e) { MessageBox.Show(e.Button.ToolTipText); }
}</source>
Add ToolTip to ToolBar Button
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class ToolBarToolTip : System.Windows.Forms.Form {
private ImageList toolBarIcons; private ToolBarButton tbExitButton; private ToolBarButton tbSaveButton; private ToolBar toolBar; private System.ruponentModel.Container components = null; public ToolBarToolTip() { this.ruponents = new System.ruponentModel.Container(); this.Size = new System.Drawing.Size(300,300);
toolBar = new ToolBar(); toolBarIcons = new ImageList(); tbSaveButton = new ToolBarButton(); tbExitButton = new ToolBarButton(); tbSaveButton.ImageIndex = 0; tbSaveButton.ToolTipText = "Save"; tbExitButton.ImageIndex = 1; tbExitButton.ToolTipText = "Exit"; toolBar.ImageList = toolBarIcons; toolBar.Size = new System.Drawing.Size(272, 28); toolBar.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; toolBar.ShowToolTips = true; toolBar.Buttons.AddRange(new ToolBarButton[] {tbSaveButton, tbExitButton}); toolBar.ButtonClick += new ToolBarButtonClickEventHandler(ToolBar_Clicked); toolBarIcons.ImageSize = new System.Drawing.Size(32, 32); toolBarIcons.Images.Add(new Icon("filesave.ico")); toolBarIcons.Images.Add(new Icon("fileexit.ico")); toolBarIcons.ColorDepth = ColorDepth.Depth16Bit; toolBarIcons.TransparentColor = System.Drawing.Color.Transparent; this.Controls.Add(toolBar); } [STAThread] static void Main() { Application.Run(new ToolBarToolTip()); } private void ToolBar_Clicked(object sender, ToolBarButtonClickEventArgs e) { MessageBox.Show(e.Button.ToolTipText); }
}</source>
Create an ImageList and use it for ToolBar
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class ImageListCreateAndAddToToolBar : System.Windows.Forms.Form {
private ImageList toolBarIcons; private ToolBarButton tbExitButton; private ToolBarButton tbSaveButton; private ToolBar toolBar; private System.ruponentModel.Container components = null; public ImageListCreateAndAddToToolBar() { this.ruponents = new System.ruponentModel.Container(); this.Size = new System.Drawing.Size(300,300); toolBar = new ToolBar(); toolBarIcons = new ImageList(); tbSaveButton = new ToolBarButton(); tbExitButton = new ToolBarButton(); tbSaveButton.ImageIndex = 0; tbExitButton.ImageIndex = 1; toolBar.ImageList = toolBarIcons; toolBar.Size = new System.Drawing.Size(272, 28); toolBar.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; toolBar.ShowToolTips = true; toolBar.Buttons.AddRange(new ToolBarButton[] {tbSaveButton, tbExitButton}); toolBar.ButtonClick += new ToolBarButtonClickEventHandler(ToolBar_Clicked); toolBarIcons.ImageSize = new System.Drawing.Size(32, 32); toolBarIcons.Images.Add(new Icon("filesave.ico")); toolBarIcons.Images.Add(new Icon("fileexit.ico")); toolBarIcons.ColorDepth = ColorDepth.Depth16Bit; toolBarIcons.TransparentColor = System.Drawing.Color.Transparent; this.Controls.Add(toolBar); } [STAThread] static void Main() { Application.Run(new ImageListCreateAndAddToToolBar()); } private void ToolBar_Clicked(object sender, ToolBarButtonClickEventArgs e) { MessageBox.Show(e.Button.ToolTipText); }
}</source>
Cut, Copy Paste/Text Box with Toolbar
<source lang="csharp">using System; using System.Drawing; using System.Windows.Forms;
class TextBoxWithToolBar: Form {
TextBox txtbox = new TextBox(); MenuItem miEditCut, miEditCopy, miEditPaste; ToolBarButton tbbCut, tbbCopy, tbbPaste; public static void Main() { System.Threading.Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA; Application.Run(new TextBoxWithToolBar()); } public TextBoxWithToolBar() { txtbox.Parent = this; txtbox.Dock = DockStyle.Fill; txtbox.Multiline = true; txtbox.ScrollBars = ScrollBars.Both; txtbox.AcceptsTab = true; Bitmap bm = new Bitmap(GetType(), "TextBoxWithToolBar.bmp"); ImageList imglst = new ImageList(); imglst.Images.AddStrip(bm); imglst.TransparentColor = Color.Cyan; ToolBar tbar = new ToolBar(); tbar.Parent = this; tbar.ImageList = imglst; tbar.ShowToolTips = true; tbar.ButtonClick += new ToolBarButtonClickEventHandler(ToolBarOnClick); Menu = new MainMenu(); MenuItem mi = new MenuItem("&Edit"); mi.Popup += new EventHandler(MenuEditOnPopup); Menu.MenuItems.Add(mi); miEditCut = new MenuItem("Cu&t"); miEditCut.Click += new EventHandler(MenuEditCutOnClick); miEditCut.Shortcut = Shortcut.CtrlX; Menu.MenuItems[0].MenuItems.Add(miEditCut); tbbCut = new ToolBarButton(); tbbCut.ImageIndex = 4; tbbCut.ToolTipText = "Cut"; tbbCut.Tag = miEditCut; tbar.Buttons.Add(tbbCut); miEditCopy = new MenuItem("&Copy"); miEditCopy.Click += new EventHandler(MenuEditCopyOnClick); miEditCopy.Shortcut = Shortcut.CtrlC; Menu.MenuItems[0].MenuItems.Add(miEditCopy); tbbCopy = new ToolBarButton(); tbbCopy.ImageIndex = 5; tbbCopy.ToolTipText = "Copy"; tbbCopy.Tag = miEditCopy; tbar.Buttons.Add(tbbCopy); miEditPaste = new MenuItem("&Paste"); miEditPaste.Click += new EventHandler(MenuEditPasteOnClick); miEditPaste.Shortcut = Shortcut.CtrlV; Menu.MenuItems[0].MenuItems.Add(miEditPaste); tbbPaste = new ToolBarButton(); tbbPaste.ImageIndex = 6; tbbPaste.ToolTipText = "Paste"; tbbPaste.Tag = miEditPaste; tbar.Buttons.Add(tbbPaste); Timer timer = new Timer(); timer.Interval = 250; timer.Tick += new EventHandler(TimerOnTick); timer.Start(); } void MenuEditOnPopup(object obj, EventArgs ea) { miEditCut.Enabled = miEditCopy.Enabled = (txtbox.SelectionLength > 0); miEditPaste.Enabled = Clipboard.GetDataObject().GetDataPresent(typeof(string)); } void TimerOnTick(object obj, EventArgs ea) { tbbCut.Enabled = tbbCopy.Enabled = (txtbox.SelectionLength) > 0; tbbPaste.Enabled = Clipboard.GetDataObject().GetDataPresent(typeof(string)); } void ToolBarOnClick(object obj, ToolBarButtonClickEventArgs tbbcea) { ToolBarButton tbb = tbbcea.Button; MenuItem mi = (MenuItem) tbb.Tag; mi.PerformClick(); } void MenuEditCutOnClick(object obj, EventArgs ea) { txtbox.Cut(); } void MenuEditCopyOnClick(object obj, EventArgs ea) { txtbox.Copy(); } void MenuEditPasteOnClick(object obj, EventArgs ea) { txtbox.Paste(); }
}</source>
Designed Toolbar
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data;
public class DesignedToolbarForm : System.Windows.Forms.Form { private System.Windows.Forms.ToolBar tbDefault; private System.Windows.Forms.ImageList imgListToolbar; private System.Windows.Forms.ToolBarButton tbbNew; private System.Windows.Forms.ToolBarButton tbbOpen; private System.Windows.Forms.ToolBarButton tbbSave; private System.Windows.Forms.ToolBarButton tbbSeparator; private System.Windows.Forms.ToolBarButton tbbCut; private System.Windows.Forms.ToolBarButton tbbCopy; private System.Windows.Forms.ToolBarButton tbbPaste; private System.Windows.Forms.ToolBarButton tbbHelp; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MainMenu mnuDemo; public DesignedToolbarForm() { this.tbDefault = new System.Windows.Forms.ToolBar(); this.tbbNew = new System.Windows.Forms.ToolBarButton(); this.tbbOpen = new System.Windows.Forms.ToolBarButton(); this.tbbSave = new System.Windows.Forms.ToolBarButton(); this.tbbSeparator = new System.Windows.Forms.ToolBarButton(); this.tbbCut = new System.Windows.Forms.ToolBarButton(); this.tbbCopy = new System.Windows.Forms.ToolBarButton(); this.tbbPaste = new System.Windows.Forms.ToolBarButton(); this.tbbHelp = new System.Windows.Forms.ToolBarButton(); this.imgListToolbar = new System.Windows.Forms.ImageList(); this.mnuDemo = new System.Windows.Forms.MainMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.SuspendLayout(); // // tbDefault // this.tbDefault.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] { this.tbbNew, this.tbbOpen, this.tbbSave, this.tbbSeparator, this.tbbCut, this.tbbCopy, this.tbbPaste, this.tbbHelp}); this.tbDefault.DropDownArrows = true; this.tbDefault.ImageList = this.imgListToolbar; this.tbDefault.Name = "tbDefault"; this.tbDefault.ShowToolTips = true; this.tbDefault.Size = new System.Drawing.Size(360, 39); this.tbDefault.TabIndex = 0; this.tbDefault.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.OnButtonClick); // // tbbNew // this.tbbNew.ImageIndex = 0; this.tbbNew.Text = "New"; this.tbbNew.ToolTipText = "Create a new file"; // // tbbOpen // this.tbbOpen.ImageIndex = 1; this.tbbOpen.Text = "Open"; this.tbbOpen.ToolTipText = "Open an existing file"; // // tbbSave // this.tbbSave.ImageIndex = 2; this.tbbSave.Text = "Save"; this.tbbSave.ToolTipText = "Save active document"; // // tbbSeparator // this.tbbSeparator.Style = System.Windows.Forms.ToolBarButtonStyle.Separator; // // tbbCut // this.tbbCut.ImageIndex = 3; this.tbbCut.Text = "Cut"; this.tbbCut.ToolTipText = "Cut selection"; // // tbbCopy // this.tbbCopy.ImageIndex = 4; this.tbbCopy.Text = "Copy"; this.tbbCopy.ToolTipText = "Copy selection"; // // tbbPaste // this.tbbPaste.ImageIndex = 5; this.tbbPaste.Text = "Paste"; this.tbbPaste.ToolTipText = "Paste from clipboard"; // // tbbHelp // this.tbbHelp.ImageIndex = 6; this.tbbHelp.Text = "Help"; this.tbbHelp.ToolTipText = "Help"; // // imgListToolbar // this.imgListToolbar.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit; this.imgListToolbar.ImageSize = new System.Drawing.Size(16, 16); this.imgListToolbar.TransparentColor = System.Drawing.Color.Transparent; // // mnuDemo // this.mnuDemo.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.Text = "File"; // // DesignedToolbarForm // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(360, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.tbDefault}); this.Menu = this.mnuDemo; this.Name = "DesignedToolbarForm"; this.Text = "Designed Toolbar"; this.ResumeLayout(false); } static void Main() { Application.Run(new DesignedToolbarForm()); } private void OnButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { ToolBarButton tbCurrent = e.Button; MessageBox.Show("You clicked button " + tbCurrent.Text); } }</source>
ToolBar Button Action
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; 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.ruponentModel.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.ruponents = new System.ruponentModel.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; }
}</source>
ToolBar Button border style: BorderStyle.Fixed3D
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class ToolBarButtonBorderStyle : System.Windows.Forms.Form {
private ImageList toolBarIcons; private ToolBarButton tbExitButton; private ToolBarButton tbSaveButton; private ToolBar toolBar; private System.ruponentModel.Container components = null; public ToolBarButtonBorderStyle() { this.ruponents = new System.ruponentModel.Container(); this.Size = new System.Drawing.Size(300,300);
toolBar = new ToolBar(); toolBarIcons = new ImageList(); tbSaveButton = new ToolBarButton(); tbExitButton = new ToolBarButton(); tbSaveButton.ImageIndex = 0; tbExitButton.ImageIndex = 1; toolBar.ImageList = toolBarIcons; toolBar.Size = new System.Drawing.Size(272, 28); toolBar.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; toolBar.Buttons.AddRange(new ToolBarButton[] {tbSaveButton, tbExitButton}); toolBar.ButtonClick += new ToolBarButtonClickEventHandler(ToolBar_Clicked); toolBarIcons.ImageSize = new System.Drawing.Size(32, 32); toolBarIcons.Images.Add(new Icon("filesave.ico")); toolBarIcons.Images.Add(new Icon("fileexit.ico")); toolBarIcons.ColorDepth = ColorDepth.Depth16Bit; toolBarIcons.TransparentColor = System.Drawing.Color.Transparent; this.Controls.Add(toolBar); } [STAThread] static void Main() { Application.Run(new ToolBarButtonBorderStyle()); } private void ToolBar_Clicked(object sender, ToolBarButtonClickEventArgs e) { MessageBox.Show(e.Button.ToolTipText); }
}</source>
ToolBar Button Style
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data;
public class TBBStylesForm : System.Windows.Forms.Form { private System.Windows.Forms.ToolBar tbMain; private System.Windows.Forms.ImageList imgListDefault; private System.Windows.Forms.ContextMenu ddmDemo; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem menuItem3; private System.ruponentModel.IContainer components; public TBBStylesForm() { InitializeComponent(); string[] astrTBB = {"Cut", "Copy", "Paste", "", "Messages", "", "Help"}; ToolBarButtonStyle[] atbbStyles = {ToolBarButtonStyle.PushButton, ToolBarButtonStyle.PushButton, ToolBarButtonStyle.PushButton, ToolBarButtonStyle.Separator, ToolBarButtonStyle.ToggleButton, ToolBarButtonStyle.Separator, ToolBarButtonStyle.DropDownButton }; int[] anImageIndex = { 0, 1, 2, 0, 4, 0, 3 }; for (int i=0; i < astrTBB.Length; i++) { ToolBarButton tbb = new ToolBarButton(); tbb.ImageIndex = anImageIndex[i]; tbb.Style = atbbStyles[i]; tbb.ToolTipText = astrTBB[i]; if (tbb.Style == ToolBarButtonStyle.DropDownButton) { tbb.DropDownMenu = ddmDemo; } tbMain.Buttons.Add(tbb); } } private void InitializeComponent() { this.ruponents = new System.ruponentModel.Container(); System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(TBBStylesForm)); this.tbMain = new System.Windows.Forms.ToolBar(); this.imgListDefault = new System.Windows.Forms.ImageList(this.ruponents); this.ddmDemo = new System.Windows.Forms.ContextMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.SuspendLayout(); // // tbMain // this.tbMain.DropDownArrows = true; this.tbMain.ImageList = this.imgListDefault; this.tbMain.Name = "tbMain"; this.tbMain.ShowToolTips = true; this.tbMain.Size = new System.Drawing.Size(292, 39); this.tbMain.TabIndex = 0; this.tbMain.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.OnButtonClick); // // imgListDefault // this.imgListDefault.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit; this.imgListDefault.ImageSize = new System.Drawing.Size(16, 16); this.imgListDefault.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgListDefault.ImageStream"))); this.imgListDefault.TransparentColor = System.Drawing.Color.Transparent; // // ddmDemo // this.ddmDemo.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1, this.menuItem2, this.menuItem3}); this.ddmDemo.Popup += new System.EventHandler(this.ddmDemo_Popup); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.Text = "SDK Help"; // // menuItem2 // this.menuItem2.Index = 1; this.menuItem2.Text = "Visual Studio .NET Help"; // // menuItem3 // this.menuItem3.Index = 2; this.menuItem3.Text = "About"; // // TBBStylesForm // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.tbMain}); this.Name = "TBBStylesForm"; this.Text = "ToolBarButtonStyles Sample"; this.Load += new System.EventHandler(this.TBBStylesForm_Load); this.ResumeLayout(false); } static void Main() { Application.Run(new TBBStylesForm()); } private void OnButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { bool bShowMessages = tbMain.Buttons[4].Pushed; if (true == bShowMessages) { MessageBox.Show("Button " + e.Button.ToolTipText + " was clicked"); } } private void TBBStylesForm_Load(object sender, System.EventArgs e) { } private void ddmDemo_Popup(object sender, System.EventArgs e) { } }</source>
ToolBar Linked With Menu
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class ToolBarLinkedWithMenu : 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 = { "YourFile.bmp", "YourFile.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.ruponentModel.Container components = null; public ToolBarLinkedWithMenu() { 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; // 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; String[] arFiles = { "1.ico", "2.ico", "3.ico", "4.ico" }; 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 ); } private void InitializeComponent() { this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.mnuFile = new System.Windows.Forms.MenuItem(); this.mnuNew = new System.Windows.Forms.MenuItem(); this.mnuFileOpen = new System.Windows.Forms.MenuItem(); this.mnuFileClose = new System.Windows.Forms.MenuItem(); this.mnuFileSave = new System.Windows.Forms.MenuItem(); this.mnuFileSaveAs = new System.Windows.Forms.MenuItem(); this.mnuEdit = new System.Windows.Forms.MenuItem(); this.mnuEditCopy = new System.Windows.Forms.MenuItem(); this.mnuEditPaste = new System.Windows.Forms.MenuItem(); this.mnuOptions = new System.Windows.Forms.MenuItem(); this.mnuOption1 = new System.Windows.Forms.MenuItem(); this.mnuOption2 = new System.Windows.Forms.MenuItem(); this.mnuOption3 = new System.Windows.Forms.MenuItem(); this.mnuRadioOptions = new System.Windows.Forms.MenuItem(); this.mnuROption1 = new System.Windows.Forms.MenuItem(); this.mnuROption2 = new System.Windows.Forms.MenuItem(); this.mnuROption3 = new System.Windows.Forms.MenuItem(); this.mnuWindow = new System.Windows.Forms.MenuItem(); this.mnuMenu1 = new System.Windows.Forms.MenuItem(); this.mnuMenu11 = new System.Windows.Forms.MenuItem(); this.mnuMenu12 = new System.Windows.Forms.MenuItem(); this.mnuMenu13 = new System.Windows.Forms.MenuItem(); this.mnuMenu14 = new System.Windows.Forms.MenuItem(); this.mnuMerge = new System.Windows.Forms.MenuItem(); this.mnuMenu2 = new System.Windows.Forms.MenuItem(); this.mnuMenu21 = new System.Windows.Forms.MenuItem(); this.mnuMenu22 = new System.Windows.Forms.MenuItem(); this.mnuMenu23 = new System.Windows.Forms.MenuItem(); this.mnuMenu24 = new System.Windows.Forms.MenuItem(); this.mnuSpecial = new System.Windows.Forms.MenuItem(); this.mnuODVote = new System.Windows.Forms.MenuItem(); this.mnuODShazam = new System.Windows.Forms.MenuItem(); // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuFile, this.mnuEdit, this.mnuOptions, this.mnuRadioOptions, this.mnuSpecial, this.mnuWindow, this.mnuMenu1, this.mnuMenu2}); // // mnuFile // this.mnuFile.Index = 0; this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuNew, this.mnuFileOpen, this.mnuFileClose, this.mnuFileSave, this.mnuFileSaveAs}); this.mnuFile.Text = "File"; // // mnuNew // this.mnuNew.Index = 0; this.mnuNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN; this.mnuNew.Text = "&New"; // // mnuFileOpen // this.mnuFileOpen.Index = 1; this.mnuFileOpen.Text = "Open"; // // mnuFileClose // this.mnuFileClose.Index = 2; this.mnuFileClose.Text = "Close"; // // mnuFileSave // this.mnuFileSave.Index = 3; this.mnuFileSave.Text = "Save"; // // mnuFileSaveAs // this.mnuFileSaveAs.Index = 4; this.mnuFileSaveAs.Text = "Save&As"; // // mnuEdit // this.mnuEdit.Index = 1; this.mnuEdit.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuEditCopy, this.mnuEditPaste}); this.mnuEdit.Text = "Edit"; // // mnuEditCopy // this.mnuEditCopy.Index = 0; this.mnuEditCopy.Text = "&Copy"; // // mnuEditPaste // this.mnuEditPaste.Index = 1; this.mnuEditPaste.Text = "Paste"; // // mnuOptions // this.mnuOptions.Index = 2; this.mnuOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuOption1, this.mnuOption2, this.mnuOption3}); this.mnuOptions.Text = "Options"; // // mnuOption1 // this.mnuOption1.Index = 0; this.mnuOption1.Text = "Option1"; // // mnuOption2 // this.mnuOption2.Index = 1; this.mnuOption2.Text = "Option2"; // // mnuOption3 // this.mnuOption3.Index = 2; this.mnuOption3.Text = "Option3"; // // mnuRadioOptions // this.mnuRadioOptions.Index = 3; this.mnuRadioOptions.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuROption1, this.mnuROption2, this.mnuROption3}); this.mnuRadioOptions.Text = "Radio Options"; // // mnuROption1 // this.mnuROption1.Index = 0; this.mnuROption1.RadioCheck = true; this.mnuROption1.Text = "Radio Option 1"; // // mnuROption2 // this.mnuROption2.Index = 1; this.mnuROption2.RadioCheck = true; this.mnuROption2.Text = "Radio Option 2"; // // mnuROption3 // this.mnuROption3.Index = 2; this.mnuROption3.RadioCheck = true; this.mnuROption3.Text = "Radio Option 3"; // // mnuWindow // this.mnuWindow.Index = 5; this.mnuWindow.MdiList = true; this.mnuWindow.MergeOrder = 99; this.mnuWindow.Text = "&Window"; // // mnuMenu1 // this.mnuMenu1.Index = 6; this.mnuMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuMenu11, this.mnuMenu12, this.mnuMenu13, this.mnuMenu14, this.mnuMerge}); this.mnuMenu1.Text = "Menu 1"; // // mnuMenu11 // this.mnuMenu11.Index = 0; this.mnuMenu11.MergeOrder = 1; this.mnuMenu11.Text = "Menu 1.1"; // // mnuMenu12 // this.mnuMenu12.Index = 1; this.mnuMenu12.MergeOrder = 2; this.mnuMenu12.Text = "Menu 1.2"; // // mnuMenu13 // this.mnuMenu13.Index = 2; this.mnuMenu13.MergeOrder = 3; this.mnuMenu13.Text = "Menu 1.3"; // // mnuMenu14 // this.mnuMenu14.Index = 3; this.mnuMenu14.MergeOrder = 4; this.mnuMenu14.Text = "Menu 1.4"; // // mnuMerge // this.mnuMerge.Index = 4; this.mnuMerge.MergeOrder = 99; this.mnuMerge.Text = "Merge!"; // // mnuMenu2 // this.mnuMenu2.Index = 7; this.mnuMenu2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuMenu21, this.mnuMenu22, this.mnuMenu23, this.mnuMenu24}); this.mnuMenu2.Text = "Menu 2"; // // mnuMenu21 // this.mnuMenu21.Index = 0; this.mnuMenu21.MergeOrder = 1; this.mnuMenu21.Text = "Menu 2.1"; // // mnuMenu22 // this.mnuMenu22.Index = 1; this.mnuMenu22.MergeOrder = 2; this.mnuMenu22.MergeType = System.Windows.Forms.MenuMerge.Replace; this.mnuMenu22.Text = "Menu 2.2"; // // mnuMenu23 // this.mnuMenu23.Index = 2; this.mnuMenu23.MergeOrder = 3; this.mnuMenu23.MergeType = System.Windows.Forms.MenuMerge.Remove; this.mnuMenu23.Text = "Menu 2.3"; // // mnuMenu24 // this.mnuMenu24.Index = 3; this.mnuMenu24.MergeOrder = 5; this.mnuMenu24.Text = "Menu 2.4"; // // mnuSpecial // this.mnuSpecial.Index = 4; this.mnuSpecial.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.mnuODVote, this.mnuODShazam}); this.mnuSpecial.Text = "Special"; // // mnuODVote // this.mnuODVote.Index = 0; this.mnuODVote.OwnerDraw = true; this.mnuODVote.Text = "Vote"; // // mnuODShazam // this.mnuODShazam.Index = 1; this.mnuODShazam.OwnerDraw = true; this.mnuODShazam.Text = "Shazam"; // // ToolBarLinkedWithMenu // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(440, 126); this.IsMdiContainer = true; this.Menu = this.mainMenu1; this.Name = "ToolBarLinkedWithMenu"; this.Text = "ToolBarLinkedWithMenu"; } [STAThread] static void Main() { Application.Run(new ToolBarLinkedWithMenu()); } private void toolBar1_ButtonClick( object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { ToolBarButton btn = e.Button; MenuItem mi = (MenuItem) btn.Tag; mi.PerformClick(); }
}</source>
Toolbar Styles
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data;
public class ToolbarStylesForm : System.Windows.Forms.Form { private System.Windows.Forms.ToolBar tbMain; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem mnuAppearanceNormal; private System.Windows.Forms.MenuItem mnuAppearanceFlat; private System.Windows.Forms.MenuItem mnuTextAlignUnderneath; private System.Windows.Forms.MenuItem mnuTextAlignRight; private System.Windows.Forms.MenuItem mnuDivider; string[] astrTBarButtons = { "New", "Open", "Save", "Cut", "Copy", "Paste", "Print", "Help"}; public ToolbarStylesForm() { InitializeComponent(); tbMain.ImageList = new ImageList(); Bitmap bmpImageStrip = new Bitmap(GetType(), "Toolbar.bmp"); bmpImageStrip.MakeTransparent(Color.FromArgb(0xff, 0x00, 0xff)); tbMain.ImageList.ImageSize = new Size(16, 15); tbMain.ImageList.Images.AddStrip(bmpImageStrip); // Create the toolbar buttons for (int i=0; i < astrTBarButtons.Length; i++) { ToolBarButton tbb = new ToolBarButton(); tbb.ImageIndex = i; tbb.ToolTipText = astrTBarButtons[i]; tbMain.Buttons.Add(tbb); } } private void InitializeComponent() { this.tbMain = new System.Windows.Forms.ToolBar(); this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.mnuAppearanceNormal = new System.Windows.Forms.MenuItem(); this.mnuAppearanceFlat = new System.Windows.Forms.MenuItem(); this.mnuTextAlignUnderneath = new System.Windows.Forms.MenuItem(); this.mnuTextAlignRight = new System.Windows.Forms.MenuItem(); this.mnuDivider = new System.Windows.Forms.MenuItem(); this.SuspendLayout(); // // tbMain // this.tbMain.DropDownArrows = true; this.tbMain.Name = "tbMain"; this.tbMain.ShowToolTips = true; this.tbMain.Size = new System.Drawing.Size(292, 39); this.tbMain.TabIndex = 0; // // 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.mnuAppearanceNormal, this.mnuAppearanceFlat, this.mnuDivider, this.mnuTextAlignUnderneath, this.mnuTextAlignRight}); this.menuItem1.Text = "Styles"; // // mnuAppearanceNormal // this.mnuAppearanceNormal.Index = 0; this.mnuAppearanceNormal.Text = "Normal"; this.mnuAppearanceNormal.Click += new System.EventHandler(this.OnAppearanceNormal); // // mnuAppearanceFlat // this.mnuAppearanceFlat.Index = 1; this.mnuAppearanceFlat.Text = "Flat"; this.mnuAppearanceFlat.Click += new System.EventHandler(this.OnAppearanceFlat); // // mnuTextAlignUnderneath // this.mnuTextAlignUnderneath.Index = 3; this.mnuTextAlignUnderneath.Text = "Text underneath"; this.mnuTextAlignUnderneath.Click += new System.EventHandler(this.OnTextAlignUnderneath); // // mnuTextAlignRight // this.mnuTextAlignRight.Index = 4; this.mnuTextAlignRight.Text = "Text right"; this.mnuTextAlignRight.Click += new System.EventHandler(this.OnTextAlignRight); // // mnuDivider // this.mnuDivider.Index = 2; this.mnuDivider.Text = "Divider"; this.mnuDivider.Click += new System.EventHandler(this.OnDivider); // // ToolbarStylesForm // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.tbMain}); this.Menu = this.mainMenu1; this.Name = "ToolbarStylesForm"; this.Text = "Toolbar Styles"; this.ResumeLayout(false); } static void Main() { Application.Run(new ToolbarStylesForm()); } private void OnAppearanceNormal(object sender, System.EventArgs e) { tbMain.Appearance = ToolBarAppearance.Normal; } private void OnAppearanceFlat(object sender, System.EventArgs e) { tbMain.Appearance = ToolBarAppearance.Flat; } private void OnDivider(object sender, System.EventArgs e) { tbMain.Divider = !tbMain.Divider; } private void OnTextAlignUnderneath(object sender, System.EventArgs e) { AddTextToToolBarButtons(); tbMain.TextAlign = ToolBarTextAlign.Underneath; } private void OnTextAlignRight(object sender, System.EventArgs e) { AddTextToToolBarButtons(); tbMain.TextAlign = ToolBarTextAlign.Right; } private void AddTextToToolBarButtons() { for (int i=0; i < astrTBarButtons.Length; i++) { tbMain.Buttons[i].Text = astrTBarButtons[i]; } } private void RemoveTextFromToolBarButtons() { for (int i=0; i < astrTBarButtons.Length; i++) { tbMain.Buttons[i].Text = ""; } tbMain.Refresh(); } }</source>
Toolbar with ComboBox
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; 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.ruponentModel.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.ruponents = new System.ruponentModel.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; } }
}</source>