Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Cannot cast from ControlCollection to ControlCollection

Cannot cast from ControlCollection to ControlCollection

Scheduled Pinned Locked Moved Visual Basic
helpquestion
4 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Ed Hill _5_
    wrote on last edited by
    #1

    I'm trying to write a method that loops through a form and dumps the name of all controls and the data from each control into a string, it works fine until it gets to a control that its self has controls in it, like a tabcontrol. Is there any work around so I can get this to work in a single method? The getValue(childControl) function simply uses a typeof check and returns the data of that control as a string. Any help would be appreciated. Public Function reportApplicationState(ByVal controlList As Windows.Forms.Form.ControlCollection) As String Dim strIndent As String = "" Dim report As String = "" Dim childControl As Control For Each childControl In controlList If childControl.Controls.Count > 0 Then layoutIndent &= " " report &= childControl.Name & ControlChars.CrLf Dim nextChildControl As Control For Each nextChildControl In childControl.Controls reportApplicationState(nextChildControl.Controls) Next Else report &= layoutIndent & childControl.Name & getValue(childControl) & controlChars.CrLf End If Next Return report End Function

    E D 2 Replies Last reply
    0
    • E Ed Hill _5_

      I'm trying to write a method that loops through a form and dumps the name of all controls and the data from each control into a string, it works fine until it gets to a control that its self has controls in it, like a tabcontrol. Is there any work around so I can get this to work in a single method? The getValue(childControl) function simply uses a typeof check and returns the data of that control as a string. Any help would be appreciated. Public Function reportApplicationState(ByVal controlList As Windows.Forms.Form.ControlCollection) As String Dim strIndent As String = "" Dim report As String = "" Dim childControl As Control For Each childControl In controlList If childControl.Controls.Count > 0 Then layoutIndent &= " " report &= childControl.Name & ControlChars.CrLf Dim nextChildControl As Control For Each nextChildControl In childControl.Controls reportApplicationState(nextChildControl.Controls) Next Else report &= layoutIndent & childControl.Name & getValue(childControl) & controlChars.CrLf End If Next Return report End Function

      E Offline
      E Offline
      Ed Hill _5_
      wrote on last edited by
      #2

      Additional info: reportApplicationState(nextChildControl.Controls) - throws exception Exception thrown at runtime: System.InvalidCastException was unhandled Message="Unable to cast object of type 'ControlCollection' to type 'ControlCollection'." Source="detailedErrorReports" StackTrace: at detailedErrorReports.StateReport.reportApplicationState(ControlCollection controlList) in C:\Documents and Settings\Ed\My Documents\Visual Studio 2005\Projects\detailedErrorReports\detailedErrorReports\StateReport.vb:line 12 at detailedErrorReports.Form1.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Ed\My Documents\Visual Studio 2005\Projects\detailedErrorReports\detailedErrorReports\Form1.vb:line 5 sorry about the code sample indentation

      1 Reply Last reply
      0
      • E Ed Hill _5_

        I'm trying to write a method that loops through a form and dumps the name of all controls and the data from each control into a string, it works fine until it gets to a control that its self has controls in it, like a tabcontrol. Is there any work around so I can get this to work in a single method? The getValue(childControl) function simply uses a typeof check and returns the data of that control as a string. Any help would be appreciated. Public Function reportApplicationState(ByVal controlList As Windows.Forms.Form.ControlCollection) As String Dim strIndent As String = "" Dim report As String = "" Dim childControl As Control For Each childControl In controlList If childControl.Controls.Count > 0 Then layoutIndent &= " " report &= childControl.Name & ControlChars.CrLf Dim nextChildControl As Control For Each nextChildControl In childControl.Controls reportApplicationState(nextChildControl.Controls) Next Else report &= layoutIndent & childControl.Name & getValue(childControl) & controlChars.CrLf End If Next Return report End Function

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        I'd probably rewrite this so you don't have a "nextChildControl" section. One level should be enough. Speaking of level, I'd also change this method to take as parameters a ControlCollection AND an interger representing depth, so you can have a collection in a collection in a collection, ... Probably something like this:

        Public Function reportApplicationState(ByVal controlCollection As ControlCollection, Optional ByVal childDepth As Integer = 0) As String
        Dim layoutIndent As New String(" "c, childDepth)
        Dim report As String = String.Empty
        Dim childControl As Control

        For Each childControl In controlCollection
            If childControl.Controls.Count > 0 Then
                report &= childControl.Name & ":" & Environment.NewLine
                report &= reportApplicationState(childControl.Controls, childDepth + 1)
            Else
                report &= layoutIndent & childControl.Name & getValue(childControl) & Environment.NewLine
            End If
        Next
        
        Return report
        

        End Function

        This is by no means tested, but you should get the idea...

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        E 1 Reply Last reply
        0
        • D Dave Kreskowiak

          I'd probably rewrite this so you don't have a "nextChildControl" section. One level should be enough. Speaking of level, I'd also change this method to take as parameters a ControlCollection AND an interger representing depth, so you can have a collection in a collection in a collection, ... Probably something like this:

          Public Function reportApplicationState(ByVal controlCollection As ControlCollection, Optional ByVal childDepth As Integer = 0) As String
          Dim layoutIndent As New String(" "c, childDepth)
          Dim report As String = String.Empty
          Dim childControl As Control

          For Each childControl In controlCollection
              If childControl.Controls.Count > 0 Then
                  report &= childControl.Name & ":" & Environment.NewLine
                  report &= reportApplicationState(childControl.Controls, childDepth + 1)
              Else
                  report &= layoutIndent & childControl.Name & getValue(childControl) & Environment.NewLine
              End If
          Next
          
          Return report
          

          End Function

          This is by no means tested, but you should get the idea...

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          E Offline
          E Offline
          Ed Hill _5_
          wrote on last edited by
          #4

          Your right in saying only one level is needed, the inner loop was only added after i initially had problems. This works fine for all controls on the controlList passed in, but when it calls its self when it finds a control with child controls i get an error casting from the child controls to a control list even though they appear to be of the same type. Any ideas on why the cast fails would be appreciated.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups