| ToolStrip Checkboxes with ToolTips in C# |
|
|
| Monday, 27 October 2008 11:44 | |
|
You can create a ToolStrip CheckBox control by simply extending ToolStripControlHost, but in order to get it to show ToolTip help like all the standard ToolStrip controls, you need to jump through the hoop of adding a MouseHover event handler to bubble up the event. In this example, I create a FlowLayoutPanel to hold the new CheckBox so I can add more widgets to the same ToolStrip control later if necessary -- like an image to display a validation error or some other indicator. I create the ToolStripCheckBox by first calling the base contructor (ToolStripControlHost) with a new FlowLayoutPanel. Then I can set some panel properties and add the CheckBox control. Finally, I need to add an event handler for the CheckBox's MouseHover event, since this is the event that will cause the ToolTip to display. The event handler simply calls the ToolStripCheckBox's OnMouseHover() method, effectively making it appear that the ToolStripControlHost is raising the event. Show tool tips for this control, and you should see them when you mouse over the CheckBox. public partial class ToolStripCheckBox
: ToolStripControlHost
{
private FlowLayoutPanel panel;
private CheckBox chk = new CheckBox();
public ToolStripCheckBox()
: base ( new FlowLayoutPanel() )
{
panel = (FlowLayoutPanel)base.Control;
panel.BackColor = Color.Transparent;
panel.Controls.Add( chk );
chk.MouseHover += new EventHandler( chk_MouseHover );
}
void chk_MouseHover( object sender, EventArgs e )
{
this.OnMouseHover( e );
}
}Update 12/30/08: Vladimir Ilic suggests the following to make the ToolStripCheckBox available through the designer and to show the text: (Thanks, Vladimir!) ToolStripItemDesignerAvailability is in System.Windows.Forms.Design. [ToolStripItemDesignerAvailability(
ToolStripItemDesignerAvailability.ToolStrip )]
[ToolboxBitmap( "..\..\Images\CheckBox.bmp" )]
public class ToolStripCheckBox
: ToolStripControlHost
{
...
protected override void OnTextChanged( EventArgs e )
{
base.OnTextChanged( e );
chk.Text = this.Text;
}
}
|
|
| Last Updated ( Tuesday, 30 December 2008 02:53 ) |

