Export (0) Print
Expand All
2 out of 3 rated this helpful - Rate this topic

ToolStripButton Class

Represents a selectable ToolStripItem that can contain text and images.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
[ToolStripItemDesignerAvailabilityAttribute(ToolStripItemDesignerAvailability.ToolStrip)]
public class ToolStripButton : ToolStripItem

Use ToolStripButton to create a toolbar button that supports both text and images. Use the ToolStripItem.ImageAlign and ToolStripItem.TextAlign properties to get or set the positioning of ToolStripButton images and text.

You can display a ToolStripButton with various border styles, and you can use it to represent and activate operational states. You can also define it to have the focus by default.

Although ToolStripButton replaces and extends the ToolBarButton control of previous versions, ToolBarButton is retained for both backward compatibility and future use.

The following code example shows two ToolStripButton controls with both image and text on a ToolStrip. Clicking the New ToolStripButton displays a message box.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsApplication11
{
	public class Form1 : Form
	{
		private ToolStripButton toolStripButton1;
		private ToolStripButton toolStripButton2;
		private ToolStrip toolStrip1;
	
		public Form1()
		{
			InitializeComponent();
		}
		[STAThread]
		static void Main()
		{
			Application.EnableVisualStyles();
			Application.Run(new Form1());
		}

		private void InitializeComponent()
		{
			this.toolStrip1 = new System.Windows.Forms.ToolStrip();
			this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
			this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
			this.toolStrip1.SuspendLayout();
			this.SuspendLayout();
			//  
			// toolStrip1 
			//  
			this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripButton1,
            this.toolStripButton2});
			this.toolStrip1.Location = new System.Drawing.Point(0, 0);
			this.toolStrip1.Name = "toolStrip1";
			this.toolStrip1.TabIndex = 0;
			this.toolStrip1.Text = "toolStrip1";
			//  
			// toolStripButton1 
			// 
			this.toolStripButton1.Image = Bitmap.FromFile("c:\\NewItem.bmp");
			this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText;
			this.toolStripButton1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.toolStripButton1.Name = "toolStripButton1";
			this.toolStripButton1.Text = "&New";
			this.toolStripButton1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click);
			//  
			// toolStripButton2 
			//  
			this.toolStripButton2.Image = Bitmap.FromFile("c:\\OpenItem.bmp");
			this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText;
			this.toolStripButton2.Name = "toolStripButton2";
			this.toolStripButton2.Text = "&Open";
			this.toolStripButton2.Click += new System.EventHandler(this.toolStripButton2_Click);
			//  
			// Form1 
			//  
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Controls.Add(this.toolStrip1);
			this.Name = "Form1";
			this.toolStrip1.ResumeLayout(false);
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			MessageBox.Show("You have mail.");
		}

		private void toolStripButton2_Click(object sender, EventArgs e)
		{
			// Add the response to the Click event here.
		}
	}
}
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.