|
-
Oct 29th, 2010, 03:06 AM
#1
Thread Starter
Member
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.
-
Oct 29th, 2010, 06:06 AM
#2
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.
-
Oct 29th, 2010, 06:51 AM
#3
Thread Starter
Member
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...
-
Oct 29th, 2010, 07:13 AM
#4
Re: Enable / Disable all buttons in a ToolStrip
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
-
Oct 29th, 2010, 07:30 AM
#5
Thread Starter
Member
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'"
-
Oct 29th, 2010, 07:32 AM
#6
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
-
Oct 29th, 2010, 07:48 AM
#7
Thread Starter
Member
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?
-
Oct 29th, 2010, 07:52 AM
#8
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
-
Oct 29th, 2010, 08:06 AM
#9
Thread Starter
Member
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:
For Each StripButton As ToolStripButton In MDIParent1.ToolStrip.Items 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...
-
Oct 29th, 2010, 08:10 AM
#10
Thread Starter
Member
Re: Enable / Disable all buttons in a ToolStrip
Ok!
Forget about it. I found the problem. I should have written this:
vb Code:
For Each StripButton As ToolStripItem In MDIParent1.ToolStrip.Items If StripButton.Equals(StripButton) Then StripButton.Enabled = True End If Next
Now it works.
Thank you very much for your help and your time!!
-
Oct 29th, 2010, 08:12 AM
#11
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
-
Oct 29th, 2010, 08:39 AM
#12
Re: Enable / Disable all buttons in a ToolStrip
 Originally Posted by bakero
Ok!
Forget about it. I found the problem. I should have written this:
vb Code:
For Each StripButton As ToolStripItem In MDIParent1.ToolStrip.Items
If StripButton.Equals(StripButton) Then
StripButton.Enabled = True
End If
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
-
Oct 29th, 2010, 08:19 PM
#13
Re: Enable / Disable all buttons in a ToolStrip
Assuming .NET 3.5 or later (please specify in future):
vb.net Code:
For Each button In MDIParent1.ToolStrip.Items.OfType(Of ToolStripButton)() button.Enabled = True Next
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|