|
-
May 17th, 2010, 03:12 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Disabling multiple controls.
I have the need to disable multiple controls based on specific circumstances.
I thought about going at it like so.
Code:
Public Shared Sub Disable(ByVal ParamArray ControlsToDisable As Control())
Try
For Each c In ControlsToDisable
c.Enabled = False
Next
Catch ex As Exception
Err(ex)
End Try
End Sub
Thing is, this doesn't work for ToolStrip items. Is there a way to do this without checking the type of each control passed to the function?
Prefix has no suffix, but suffix has a prefix.
-
May 17th, 2010, 04:15 AM
#2
Re: Disabling multiple controls.
Since the Control and ToolStripItem classes don't inherit the same base class that also implements the Enabled property, I think you will have to make a separate method for ToolStripItems.
If, for example, both Control and ToolStripItem implemented some IEnable interface (or whatever) which enforces them to implement an Enabled property, then you could do this
Code:
Public Shared Sub Disable(ByVal ParamArray ControlsToDisable As IEnable())
Try
For Each c As IEnable In ControlsToDisable
c.Enabled = False
Next
Catch ex As Exception
Err(ex)
End Try
End Sub
This does not work, because there is no such interface as far as I know. The Control class and the ToolStripItem class both implement their Enabled properties separately, not as part of a base class or interface.
What you can do is something more ugly such as
Code:
Public Shared Sub Disable(ByVal ParamArray ControlsToDisable As Object())
Try
For Each o As Object In ControlsToDisable
If TypeOf o Is Control Then
DirectCast(o, Control).Enabled = False
ElseIf TypeOf o Is ToolStripItem Then
DirectCast(o, ToolStripItem).Enabled = False
End If
Next
Catch ex As Exception
Err(ex)
End Try
End Sub
But that is exactly doing what you didn't want: checking the type of every control. Since there are only two controls though (I can't think of any other class that you can enable which is not a control or ToolStripItem?), it's not too much of a problem.
-
May 17th, 2010, 04:55 AM
#3
Re: Disabling multiple controls.
The other way (not recommended) is to accept OBJECT as function argument while OPTION STRICT is OFF.
Code:
'In the declerations section
Option Strict Off
'-----------------
Public Shared Sub Disable(ByVal ParamArray ControlsToDisable As Object())
Try
For Each c In ControlsToDisable
c.Enabled = False
Next
Catch ex As Exception
Err(ex)
End Try
End Sub
-
May 17th, 2010, 04:57 AM
#4
Re: Disabling multiple controls.
Or use Overloaded methods (recommended)
Code:
Public Overloads Shared Sub Disable(ByVal ParamArray ControlsToDisable As Control())
Try
For Each c In ControlsToDisable
c.Enabled = False
Next
Catch ex As Exception
Err(ex)
End Try
End Sub
Public Overloads Shared Sub Disable(ByVal ParamArray ControlsToDisable As ToolStripItem())
Try
For Each c In ControlsToDisable
c.Enabled = False
Next
Catch ex As Exception
Err(ex)
End Try
End Sub
-
May 17th, 2010, 02:52 PM
#5
Thread Starter
Hyperactive Member
Re: Disabling multiple controls.
I ended up doing what Pradeep did, except I didn't declare Overloads.
It's silly that they have different base classes. Thanks for the help.
Prefix has no suffix, but suffix has a prefix.
-
May 17th, 2010, 03:16 PM
#6
Re: Disabling multiple controls.
It is silly.
Another option would be to build two collections, one to hold each type, then create a method that took either one as an argument. You would still have to check the type, but you would be checking the type of the collection, not the type of each individual control.
Having said that, I liked Pradeeps overloaded methods best of all the suggestions. There are many ways to skin a cat, but most of them leave you with a ratty pelt and a skinned cat, neither of which is desirable.
My usual boring signature: Nothing
 
-
May 17th, 2010, 03:22 PM
#7
Re: Disabling multiple controls.
Another option is to make use of Generics...
Code:
Public Shared Sub Disable(Of T)(ByVal ParamArray ControlsToDisable As T())
Try
For Each c As T In ControlsToDisable
c.Enabled = False
Next
Catch ex As Exception
Err(ex)
End Try
End Sub
To call:
Code:
dim mytoolStripItem as ToolStripItem()
' Assign to the array
Disable(of ToolStripItem) (myToolStripItems())
dim myControls as Control()
' Assign to the array
Disable(of Control) (myControls())
Disclaimer - untested, but that should work.
-tg
-
May 17th, 2010, 05:05 PM
#8
Re: Disabling multiple controls.
 Originally Posted by techgnome
Another option is to make use of Generics...
Code:
Public Shared Sub Disable(Of T)(ByVal ParamArray ControlsToDisable As T())
Try
For Each c As T In ControlsToDisable
c.Enabled = False
Next
Catch ex As Exception
Err(ex)
End Try
End Sub
To call:
Code:
dim mytoolStripItem as ToolStripItem()
' Assign to the array
Disable(of ToolStripItem) (myToolStripItems())
dim myControls as Control()
' Assign to the array
Disable(of Control) (myControls())
Disclaimer - untested, but that should work.
-tg
That won't work. How does the compiler know that 'T' has an Enabled property? You'd have to give T a constraint so it must implement some interface (I think the syntax is "MethodName(Of T As Interface)" in VB, but not sure about that), but that leaves you with the same problem: there is no common interface or control that both a ToolStripItem and Control implement/inherit (except Component, which does not have the Enabled property).
Perhaps it works with option strict off, but then you could just as well pass as object.
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
|