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.