Results 1 to 12 of 12

Thread: ToolStripCheckBox

  1. #1
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 05
    Location
    Lansing, MI; USA
    Posts
    3,787

    ToolStripCheckBox

    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:
    Name:  ToolStripCheckBoxExample.png
Views: 440
Size:  75.5 KB

    I've also included a class file as well as an example project for this.
    Attached Files Attached Files
    Last edited by JuggaloBrotha; Jul 25th, 2012 at 08:10 AM.
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.



    CodeBank: All Threads • Colors ComboBox • Fading & Gradient Form • MoveItemListBox/MoveItemListView • MultilineListBox • MenuButton • ToolStripCheckBox • Start with Windows

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 09
    Location
    USA
    Posts
    3,823

    Re: ToolStripCheckBox

    Nice! I'm going to test it out as soon as I get a chance. This definitely seems useful
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 05
    Location
    Lansing, MI; USA
    Posts
    3,787

    Re: ToolStripCheckBox

    I modified the code so the internal checkbox's backcolor is transparent, allows the toolstrip's gradient backcolor to show through the text
    Currently using: VS 2010 Ultimate on Win7 Ultimate x64.



    CodeBank: All Threads • Colors ComboBox • Fading & Gradient Form • MoveItemListBox/MoveItemListView • MultilineListBox • MenuButton • ToolStripCheckBox • Start with Windows

  4. #4
    New Member
    Join Date
    Jul 12
    Posts
    4

    Re: ToolStripCheckBox

    I am messing with a control like this for the first time. This code provided where would you put this? In a new Class or an existing class?

  5. #5
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 09
    Location
    USA
    Posts
    3,823

    Re: ToolStripCheckBox

    Quote Originally Posted by The_Virus_TCH View Post
    I am messing with a control like this for the first time. This code provided where would you put this? In a new Class or an existing class?
    It's a control. So you can either create a control and paste in this code or just create a new class.

    If you look at the code, it's a class. So you can't really put it in another class:

    VB.NET Code:
    1. Public Class ToolStripCheckBox
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  6. #6
    New Member
    Join Date
    Jul 12
    Posts
    4

    Re: ToolStripCheckBox

    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.

  7. #7
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 09
    Location
    USA
    Posts
    3,823

    Re: ToolStripCheckBox

    Quote Originally Posted by The_Virus_TCH View Post
    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.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8
    New Member
    Join Date
    Jul 12
    Posts
    4

    Re: ToolStripCheckBox

    This is the error i get

    Constructor on type 'System.Windows.Forms.ToolStripControlHost' not found.

  9. #9
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 09
    Location
    USA
    Posts
    3,823

    Re: ToolStripCheckBox

    Quote Originally Posted by The_Virus_TCH View Post
    This is the error i get

    Constructor on type 'System.Windows.Forms.ToolStripControlHost' not found.
    C'mon, man. You're not even trying. I'm not going to do this for you. Hover over the error.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  10. #10
    New Member
    Join Date
    Jul 12
    Posts
    4

    Re: ToolStripCheckBox

    I am a MORON sorry about that was trying to work from home and my 4 year old had me milti-tasking. Thanks for your help.

  11. #11
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 05
    Location
    Lansing, MI; USA
    Posts
    3,787

    Re: ToolStripCheckBox

    Quote Originally Posted by weirddemon View Post
    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:
    1. Public Class OutterClass
    2.  
    3.   Private m_InnerClass As InnerClass
    4.  
    5.   Public Sub New()
    6.     m_InnerClass = New InnerClass
    7.   End Sub
    8.  
    9.   Private Class InnerClass
    10.     Public Sub New()
    11.     End Sub
    12.   End Clas
    13.  
    14. End Class

    Quote Originally Posted by The_Virus_TCH View Post
    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 2010 Ultimate on Win7 Ultimate x64.



    CodeBank: All Threads • Colors ComboBox • Fading & Gradient Form • MoveItemListBox/MoveItemListView • MultilineListBox • MenuButton • ToolStripCheckBox • Start with Windows

  12. #12
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 09
    Location
    USA
    Posts
    3,823

    Re: ToolStripCheckBox

    Quote Originally Posted by JuggaloBrotha View Post
    Actually you can:
    vb Code:
    1. Public Class OutterClass
    2.  
    3.   Private m_InnerClass As InnerClass
    4.  
    5.   Public Sub New()
    6.     m_InnerClass = New InnerClass
    7.   End Sub
    8.  
    9.   Private Class InnerClass
    10.     Public Sub New()
    11.     End Sub
    12.   End Clas
    13.  
    14. End Class

    See post #1, I've included an example project, the control as a class file & a screenshot for ya.
    Yeah. I don't know what I was thinking. I have classes within classes on several of my projects.

    Maybe the context of the control confused me.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •