I've recently wanted a CheckBox in a ToolStrip and while that control doesn't exist in the framework, I did some googling and made my own:
Code:
Imports System.Windows.Forms.Design
<ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip), DebuggerStepThrough()> _
Public Class ToolStripCheckBox
Inherits ToolStripControlHost
Public Sub New()
MyBase.New(New System.Windows.Forms.CheckBox())
ToolStripCheckBoxControl.BackColor = Color.Transparent
End Sub
Public ReadOnly Property ToolStripCheckBoxControl() As CheckBox
Get
Return TryCast(Control, CheckBox)
End Get
End Property
Public Property ToolStripCheckBoxEnabled() As Boolean
Get
Return ToolStripCheckBoxControl.Enabled
End Get
Set(ByVal value As Boolean)
ToolStripCheckBoxControl.Enabled = value
End Set
End Property
Public Property Checked() As Boolean
Get
Return ToolStripCheckBoxControl.Checked
End Get
Set(ByVal value As Boolean)
ToolStripCheckBoxControl.Checked = value
End Set
End Property
Protected Overrides Sub OnSubscribeControlEvents(ByVal c As Control)
MyBase.OnSubscribeControlEvents(c)
AddHandler DirectCast(c, CheckBox).CheckedChanged, AddressOf OnCheckedChanged
End Sub
Protected Overrides Sub OnUnsubscribeControlEvents(ByVal c As Control)
MyBase.OnUnsubscribeControlEvents(c)
RemoveHandler DirectCast(c, CheckBox).CheckedChanged, AddressOf OnCheckedChanged
End Sub
Public Event CheckedChanged As EventHandler
Private Sub OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent CheckedChanged(Me, e)
End Sub
End Class
Just add that code to your project , build it then simply click the AddItem DropDown arrow on a ToolStrip and at the bottom, there it is.
Here's a screenshot of it being used:
I've also included a class file as well as an example project for this.
Last edited by JuggaloBrotha; Jul 25th, 2012 at 08:10 AM.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
Yea i knew it was a coded as a class. I guess my issue is once i add it to a new class I cannot figure out how to get it into my ToolStrip.
I read about adding it to the construct of the toolstrip, but I have never done this before. I go into the Designer of the Form but it throughs any when attempting this. Sorry I have dome a lot of things with code before just never tried building a control like this before.
Last edited by The_Virus_TCH; Jul 24th, 2012 at 07:36 PM.
Yea i knew it was a coded as a class. I guess my issue is once i add it to a new class I cannot figure out how to get it into my ToolStrip. I read about adding it to the construct of the toolstrip, but I have never done this before. I go into the Designer of the Form but it throughs any when attempting this. SOrry I have dome a lot of things with code before just never tried building a control like this before.
The point I was making was that you can't put a class in a class.
There's nothing you need to do. Create the class, build the project, add the toolstrip to the form, and add the checkbox like you would any other native child control.
The point I was making was that you can't put a class in a class.
There's nothing you need to do. Create the class, build the project, add the toolstrip to the form, and add the checkbox like you would any other native child control.
Actually you can:
vb Code:
Public Class OutterClass
Private m_InnerClass As InnerClass
Public Sub New()
m_InnerClass = New InnerClass
End Sub
Private Class InnerClass
Public Sub New()
End Sub
End Clas
End Class
Originally Posted by The_Virus_TCH
This is the error i get
Constructor on type 'System.Windows.Forms.ToolStripControlHost' not found.
See post #1, I've included an example project, the control as a class file & a screenshot for ya.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
Constructor on type 'System.Windows.Forms.ToolStripControlHost' not found.
Did you ever figure out why this error happens? I can get the ToolStripCheckBox working on my form, and I can use <F7> to look at the code, but the error happens when I try to view the Designer (double-click the ToolStripCheckBox.vb class module or <Shift F7>).
Did you ever figure out why this error happens? I can get the ToolStripCheckBox working on my form, and I can use <F7> to look at the code, but the error happens when I try to view the Designer (double-click the ToolStripCheckBox.vb class module or <Shift F7>).
This isn't a control you can open in the visual designer as it has to be rendered in a ToolStripControlHost. Right click on it in the solution explorer and click "View Code" to see how it's made.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
Juggalo ... I'm having some issues with this control. I added the class to my project by copying & pasting your code from post #1 above. I add a ToolStrip to my form from the toolbox. I then add a CheckBox to the ToolStrip by picking it from the dropdown & it shows up in the ToolStrip as expected. I then run the project & the form shows on the screen with the CheckBox in the ToolStrip as expected, great. I now close the form & the CheckBox control disappears from the ToolStrip in the designer, along with the dropdown. When I run the project again the CheckBox still shows in the ToolStrip. But back in the designer I cannot see, select or delete the CheckBox or add other controls to the ToolStrip. If I delete the ToolStrip from the form & run the project again the ToolStrip still shows up along with the CheckBox on it. Any idea what's going on? I'd like to use this control, but certainly not with this behavior. Thanks for any help. BTW, I am using VS 2012, 4.0 framework & Windows 8.
Juggalo ... I'm having some issues with this control. I added the class to my project by copying & pasting your code from post #1 above. I add a ToolStrip to my form from the toolbox. I then add a CheckBox to the ToolStrip by picking it from the dropdown & it shows up in the ToolStrip as expected. I then run the project & the form shows on the screen with the CheckBox in the ToolStrip as expected, great. I now close the form & the CheckBox control disappears from the ToolStrip in the designer, along with the dropdown. When I run the project again the CheckBox still shows in the ToolStrip. But back in the designer I cannot see, select or delete the CheckBox or add other controls to the ToolStrip. If I delete the ToolStrip from the form & run the project again the ToolStrip still shows up along with the CheckBox on it. Any idea what's going on? I'd like to use this control, but certainly not with this behavior. Thanks for any help. BTW, I am using VS 2012, 4.0 framework & Windows 8.
Try closing the form in the designer, then reopening it and everything should appear to be normal.
I don't have Win8 or VS2012/2013, but using Win7 Ultimate x64 and VS 2010 Ultimate I am unable to reproduce your situation.
Last edited by JuggaloBrotha; Jan 22nd, 2014 at 10:45 AM.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
Juggalo ... I have revisited this post as I would like to use this control in a project, but I am still having the same issue I described in post #15. Closing the form & reopening it works, but everytime I run the project & then stop it, everything on the ToolStrip disappears again. It's a real pain to have to close & reopen the form designer everytime. Is there anything that can be done to eliminate this issue? I'm running VS2013/Windows 8 64bit. Thanks.
Can you zip & upload your project?
I still don't have Win8 or VS2012/2013, I'm running Win7 Ultimate x64 & VS2010, I'm not able to reproduce your issues at all.
Probably by the end of this year I'll have Win10 x64 & VS2015 to try it out with.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
Year 2024. I'll share here an improved modified version. Thanks to @JuggaloBrotha for sharing his original concept.
Notable changes:
Added support to use the CheckBox on a StatusStrip too.
Added properties: Checkstate and ThreeState
Added event: CheckstateChanged
Added relevant class attributes to each property (replicating what is defined in CheckBox class)
Properties are categorized and have descriptions for the property grid.
Simplified hosted CheckBox usage by shadowing ToolStripControlHost.Control property.
All the members of the source code are well XML documented.
Screenshots:
Source-code:
Note: You can set ToolboxItem and DesignTimeVisible class attributes to 'True' if you prefer.
ToolStripCheckBox .vb
Code:
#Region " Imports "
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Runtime.InteropServices
Imports System.Windows.Forms.Design
#End Region
#Region " ToolStripCheckBox "
''' <summary>
''' Represents a selectable <see cref="ToolStripItem"/> that when clicked, toggles a checkmark.
''' </summary>
''' <seealso cref="ToolStripControlHost"/>
<
ClassInterface(ClassInterfaceType.AutoDispatch),
ComVisible(True),
DebuggerStepThrough,
DefaultEvent(NameOf(ToolStripCheckBox.CheckedChanged)),
DefaultProperty(NameOf(ToolStripCheckBox.Text)),
Description("Represents a selectable ToolStripItem that when clicked, toggles a checkmark."),
Designer("System.Windows.Forms.Design.ToolStripItemDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
DesignerCategory(NameOf(DesignerCategoryAttribute.Generic)),
DesignTimeVisible(False),
DisplayName(NameOf(ToolStripCheckBox)),
Localizable(True),
ToolboxBitmap(GetType(CheckBox), "CheckBox.bmp"),
ToolboxItem(False),
ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow),
ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip Or ToolStripItemDesignerAvailability.StatusStrip)
>
Public Class ToolStripCheckBox : Inherits ToolStripControlHost
#Region " Properties "
''' <summary>
''' Gets the <see cref="CheckBox"/> control that is hosted by this <see cref="ToolStripCheckBox"/>.
''' </summary>
<
Browsable(True), EditorBrowsable(EditorBrowsableState.Advanced),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
Category("Hosted"), Description("The CheckBox control that is hosted by this control.")
>
Public Shadows ReadOnly Property Control As CheckBox
Get
Return DirectCast(MyBase.Control, CheckBox)
End Get
End Property
''' <summary>
''' Gets or sets a value indicating whether this <see cref="ToolStripCheckBox"/> is in the checked state.
''' </summary>
'''
''' <returns>
''' <see langword="True"/> if checked; otherwise, <see langword="False"/>.
''' </returns>
<
Bindable(True), SettingsBindable(True),
DefaultValue(False),
RefreshProperties(RefreshProperties.All),
Category("Appearance"), Description("Specifies whether this control is in the checked state.")
>
Public Property Checked As Boolean
Get
Return Me.Control.Checked
End Get
Set(value As Boolean)
Me.Control.Checked = value
End Set
End Property
''' <summary>
''' Gets or sets the checked state of this <see cref="ToolStripCheckBox"/>.
''' </summary>
'''
''' <returns>
''' One of the <see cref="System.Windows.Forms.CheckState"/> enumeration values.
''' <para></para>
''' The default value is <see cref="System.Windows.Forms.CheckState.Unchecked"/>.
''' </returns>
'''
''' <exception cref="System.ComponentModel.InvalidEnumArgumentException">
''' The value assigned is not one of the <see cref="System.Windows.Forms.CheckState"/> enumeration values.
''' </exception>
<
Bindable(True),
DefaultValue(CheckState.Unchecked),
RefreshProperties(RefreshProperties.All),
Category("Appearance"), Description("Specifies the checked state of this control.")
>
Public Property CheckState As CheckState
Get
Return Me.Control.CheckState
End Get
Set(value As CheckState)
Me.Control.CheckState = value
End Set
End Property
''' <summary>
''' Gets or sets a value indicating whether this <see cref="ToolStripCheckBox"/>
''' will allow three check states rather than two.
''' </summary>
'''
''' <remarks>
''' If the <see cref="ToolStripCheckBox.ThreeState"/> property is set to <see langword="False"/>,
''' the <see cref="ToolStripCheckBox.CheckState"/> property value can only be set to
''' the <see cref="System.Windows.Forms.CheckState.Indeterminate"/> value in code,
''' and not by user interaction doing click on the control.
''' </remarks>
'''
''' <returns>
''' <see langword="True"/> if this <see cref="ToolStripCheckBox"/>
''' is able to display three check states; otherwise, <see langword="False"/>.
''' <para></para>
''' The default value is <see langword="False"/>.
''' </returns>
<
DefaultValue(False),
Category("Behavior"), Description("Specifies whether this control will allow three check states rather than two.")
>
Public Property ThreeState As Boolean
Get
Return Me.Control.ThreeState
End Get
Set(value As Boolean)
Me.Control.ThreeState = value
End Set
End Property
#End Region
#Region " Events "
''' <summary>
''' Occurs whenever the <see cref="ToolStripCheckBox.Checked"/> property is changed.
''' </summary>
Public Event CheckedChanged As EventHandler
''' <summary>
''' Occurs whenever the <see cref="ToolStripCheckBox.CheckState"/> property is changed.
''' </summary>
Public Event CheckStateChanged As EventHandler
#End Region
#Region " Constructors "
''' <summary>
''' Initializes a new instance of the <see cref="ToolStripCheckBox"/> class.
''' </summary>
Public Sub New()
MyBase.New(New CheckBox())
Me.Control.BackColor = Color.Transparent
End Sub
#End Region
#Region " Event Invocators "
''' <summary>
''' Raises the <see cref="ToolStripCheckBox.CheckedChanged"/> event.
''' </summary>
'''
''' <param name="sender">
''' The source of the event.
''' </param>
'''
''' <param name="e">
''' The <see cref="EventArgs"/> instance containing the event data.
''' </param>
Private Sub OnCheckedChanged(sender As Object, e As EventArgs)
If Me.CheckedChangedEvent IsNot Nothing Then
RaiseEvent CheckedChanged(Me, e)
End If
End Sub
''' <summary>
''' Raises the <see cref="ToolStripCheckBox.CheckStateChanged"/> event.
''' </summary>
'''
''' <param name="sender">
''' The source of the event.
''' </param>
'''
''' <param name="e">
''' The <see cref="EventArgs"/> instance containing the event data.
''' </param>
Private Sub OnCheckStateChanged(sender As Object, e As EventArgs)
If Me.CheckStateChangedEvent IsNot Nothing Then
RaiseEvent CheckStateChanged(Me, e)
End If
End Sub
#End Region
#Region " Event Invocators (Overriden) "
''' <summary>
''' Subscribes events from the hosted control
''' </summary>
'''
''' <param name="control">
''' The control from which to subscribe events.
''' </param>
Protected Overrides Sub OnSubscribeControlEvents(control As Control)
MyBase.OnSubscribeControlEvents(control)
AddHandler DirectCast(control, CheckBox).CheckedChanged, AddressOf Me.OnCheckedChanged
End Sub
''' <summary>
''' Unsubscribes events from the hosted control
''' </summary>
'''
''' <param name="control">
''' The control from which to unsubscribe events.
''' </param>
Protected Overrides Sub OnUnsubscribeControlEvents(control As Control)
MyBase.OnUnsubscribeControlEvents(control)
RemoveHandler DirectCast(control, CheckBox).CheckedChanged, AddressOf Me.OnCheckedChanged
End Sub
''' <summary>
''' Raises the <see cref="Windows.Forms.Control.ParentChanged"/> event.
''' </summary>
'''
''' <param name="oldParent">
''' The original parent of the item.
''' </param>
'''
''' <param name="newParent">
''' The new parent of the item.
''' </param>
Protected Overrides Sub OnParentChanged(oldParent As ToolStrip, newParent As ToolStrip)
MyBase.OnParentChanged(oldParent, newParent)
End Sub
''' <summary>
''' Raises the <see cref="ToolStripItem.OwnerChanged"/> event.
''' </summary>
'''
''' <param name="e">
''' The <see cref="EventArgs"/> instance containing the event data.
''' </param>
Protected Overrides Sub OnOwnerChanged(e As EventArgs)
MyBase.OnOwnerChanged(e)
End Sub
#End Region
End Class
#End Region
The issue described in post #15 it is related to Windows Forms designer that is changing the parent and the owner at design time. I was not able to figure out a solution for it.
Last edited by pitoloko; Apr 11th, 2024 at 05:27 AM.