Results 1 to 13 of 13

Thread: Enable / Disable all buttons in a ToolStrip

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    52

    Enable / Disable all buttons in a ToolStrip

    Hi!

    I have a toolStrip with several buttons. Each button opens a new form.

    With some forms I disable just its button, but with others I disable three buttons.

    After I close any form, I re-enable the button.

    As it isn't always the same amount of buttons, I'd like to do a routine to re-enable the buttons after I close any form.

    I've tried with:

    Code:
            For Each Ctrl As Control In Me.Controls
                Ctrl.GetType()
                If (Ctrl Is ToolStripButton) Then
                       Ctrl.enabled = true
                End If
            Next
    and

    Code:
          For Each StripButton As ToolStripButton In MDIParent1.Controls
                If Controls.Equals(StripButton) Then
                    StripButton.Enabled = True
                End If
            Next
    But any of them work...

    could anyone please, give me a hand with this?

    Thanks in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Enable / Disable all buttons in a ToolStrip

    That would be because tool strip buttons are not controls in any Controls collection. They can be found in the Items collection of the ToolStrip.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    52

    Re: Enable / Disable all buttons in a ToolStrip

    Thank you for your reply.

    Then, what should I write in my code?

    Because I wrote

    Code:
           For Each StripButton As ToolStripButton In MDIParent1.ToolStrip.Items
                If Controls.Equals(StripButton) Then
                    StripButton.Enabled = True
                End If
            Next
    and it still doesn't work...

  4. #4
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Enable / Disable all buttons in a ToolStrip

    How is Controls defined?
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    52

    Re: Enable / Disable all buttons in a ToolStrip

    The problem appears right on the For stament.

    It says
    Code:
    "Cannot convert type 'System.Windows.Forms.ToolStripSeparator' to type 'System.Windows.Forms.ToolStripButton'"

  6. #6
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Enable / Disable all buttons in a ToolStrip

    In that case you need to ignore any StripButton that is a "ToolStripSeparator".
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    52

    Re: Enable / Disable all buttons in a ToolStrip

    How can I ignore the toolStripSeparator in that code?

    Do I have to delete that separator from my ToolStrip Design?

  8. #8
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Enable / Disable all buttons in a ToolStrip

    No. Just add an If statement before your If that checks to see if the object you have a reference to is a "ToolStripSeparator", if it isn't then continue. You may have to use GetType or there may be a property you can check.

    Conversly you may be able to check if the object is a "ToolStripButton" and if so then continue on with your if.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  9. #9

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    52

    Re: Enable / Disable all buttons in a ToolStrip

    The problem is that I can't get to the If statement.

    Now, I tried with just this code:

    vb Code:
    1. For Each StripButton As ToolStripButton In MDIParent1.ToolStrip.Items
    2.  
    3.         Next

    and even with that I get a mistake.

    I think I should have a different variable type in the for each statement...am I right? I mean: To declare

    Code:
    For each aux_var as 'whatever_type' in MDIParent1.ToolStrip.Items
    and there, get the type of aux_var...

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2010
    Posts
    52

    Re: Enable / Disable all buttons in a ToolStrip

    Ok!

    Forget about it. I found the problem. I should have written this:

    vb Code:
    1. For Each StripButton As ToolStripItem In MDIParent1.ToolStrip.Items
    2.             If StripButton.Equals(StripButton) Then
    3.                 StripButton.Enabled = True
    4.             End If
    5.         Next

    Now it works.

    Thank you very much for your help and your time!!

  11. #11
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: Enable / Disable all buttons in a ToolStrip

    You're welcome. Glad you figured it out!!
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  12. #12
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Enable / Disable all buttons in a ToolStrip

    Quote Originally Posted by bakero View Post
    Ok!

    Forget about it. I found the problem. I should have written this:

    vb Code:
    1. For Each StripButton As ToolStripItem In MDIParent1.ToolStrip.Items
    2.             If StripButton.Equals(StripButton) Then
    3.                 StripButton.Enabled = True
    4.             End If
    5.         Next

    Now it works.

    Thank you very much for your help and your time!!
    You might want to read up on the TypeOf operator, last I know a variable (StripButton) will always equal itself. Luckily for you a ToolStripSeparator looks the same on in it's Enabled state as it does in it's disabled state & they aren't clickable (no Click event) anyways. But if you have other items in there that you don't want to become enabled/disabled, you'll want to check for just items that are of the type ToolStripButton:
    Code:
          For Each StripButton As ToolStripItem In MDIParent1.ToolStrip.Items
                If TypeOf StripButton Is ToolStripButton Then
                    'Only enable ToolStripButton's
                    StripButton.Enabled = True
                End If
            Next
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Enable / Disable all buttons in a ToolStrip

    Assuming .NET 3.5 or later (please specify in future):
    vb.net Code:
    1. For Each button In MDIParent1.ToolStrip.Items.OfType(Of ToolStripButton)()
    2.     button.Enabled = True
    3. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width