0

When I do something like:

For Each item As HScrollBar In Me.Controls 'ERROR: Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.Windows.Forms.HScrollBar'.
item.Visible = False
Next

I get an error because I have a control on a windows forms that is not the item which is a hscrollbar.

1 Answers1

2

Yeah, that's not correct code. Me.Controls contains controls, not just scrollbars. Fix:

    For Each item As Control In Me.Controls
        If TypeOf item Is HScrollBar Then
            '' etc..
        End If
    Next

Or the cleaner Linq version:

    For Each item As HScrollBar In Me.Controls.OfType(Of HScrollBar)()
        '' etc..
    Next
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536