This documentation is archived and is not being maintained.

ToolBarButton.Tag Property

Gets or sets the object that contains data about the toolbar button.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public Object Tag { get; set; }

Property Value

Type: System.Object
An Object that contains data about the toolbar button. The default is null.

Retrieves or assigns the data currently associated with the toolbar button. Any Object derived type can be assigned to this property. If this property is being set through the Windows Forms designer, only text can be assigned.

The following code example demonstrates how to use the Pushed, PartialPush, and Tag properties. To run the example, paste the following code in a form containing a RichTextBox control called RichTextBox1. Call the InitializeToolBar method in the form's constructor or Load method.

	// Declare ToolBar1. 
	internal System.Windows.Forms.ToolBar ToolBar1;

	// Initialize ToolBar1 with Bold(B), Italic(I), and  
	// Underline(U) buttons. 
	private void InitializeToolBar()
		ToolBar1 = new ToolBar();

		// Set the appearance to Flat.
		ToolBar1.Appearance = ToolBarAppearance.Flat;

		// Set the toolbar to dock at the bottom of the form.
		ToolBar1.Dock = DockStyle.Bottom;

		// Set the toolbar font to 14 points and bold.
		ToolBar1.Font = new Font(FontFamily.GenericSansSerif,
			14, FontStyle.Bold);

		// Declare fontstyle array with the three font styles.
		FontStyle[] fonts = new FontStyle[]{FontStyle.Bold, 
			FontStyle.Italic, FontStyle.Underline};
		int count;

		// Create a button for each value in the array, setting its  
		// text to the first letter of the style and its  
		// button's tag property. 
		for(count=0; count<fonts.Length; count++)
			ToolBarButton fontButton = 
				new ToolBarButton(fonts[count].ToString().Substring(0, 1));
			fontButton.Style = ToolBarButtonStyle.ToggleButton;
			fontButton.Tag = fonts[count];
		this.ToolBar1.ButtonClick += 
			new ToolBarButtonClickEventHandler(ToolBar1_ButtonClick);

	// Declare FontStyle object, which defaults to the Regular 
	// FontStyle.
	FontStyle style = new FontStyle();

	private void ToolBar1_ButtonClick(object sender, 
		System.Windows.Forms.ToolBarButtonClickEventArgs e)

		// If a button is pushed, use a bitwise Or combination  
		// of the style variable and the button tag, to set style to  
		// the correct FontStyle. Set the button's PartialPush  
		// property to true for a Windows XP-like appearance. 
		if (e.Button.Pushed)
			e.Button.PartialPush = true;
			style = style |(FontStyle) e.Button.Tag;

			// If the button was not pushed, use a bitwise XOR  
			// combination to turn off that style  
			// and set the PartialPush property to false.
			e.Button.PartialPush = false;
			style = style ^ (FontStyle) e.Button.Tag;

		// Set the font using the existing RichTextBox font and the new 
		// style.
		RichTextBox1.Font = new Font(RichTextBox1.Font, style);


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, Windows CE, Windows Mobile for Pocket PC

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, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0