Results 1 to 8 of 8

Thread: [RESOLVED] Disabling multiple controls.

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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.

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    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.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Disabling multiple controls.

    Quote Originally Posted by techgnome View Post
    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
  •  



Click Here to Expand Forum to Full Width