Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi !

 

Another really stupid thing to do with VB6 just turn into a nightmare with the .NET version...

 

I'm just trying to loop throu all the controls of my Form, butnow, the Controls collection just give you access to top level controls, which means you have to drill down to get all the controls in GroupBox.

 

I've seen some thread (in this forum) that suggests that I can use a recursive method, but I just can't make it work properly.

 

Here's the code :

Protected Overridable Sub AddTextBoxHandler(ByRef colControls As ControlCollection)
       Dim ctrlTemp As Control

       For Each ctrlTemp In colControls
           If TypeOf ctrlTemp Is GroupBox Then
               Try
                   AddTextBoxHandler(DirectCast(grpTemp, GroupBox).Controls)
               Catch e As Exception
                   MessageBox.Show(e.Message & vbCrLf & ctrlTemp.Name & " --- " & grpTemp.GetType.Name)
               End Try

           End If
           If TypeOf ctrlTemp Is TextBox Then AddHandler ctrlTemp.TextChanged, AddressOf CellTextChanged
       Next ctrlTemp

   End Sub

 

As soon as I'm trying to call it back with the Controls collection of the GroupBox, I get an "Invalid cast" error. But the type is Windows.Forms.ControlCollection. Does anyone have an idea on what I'm doing wrong here ?

Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone!

--Homer Simpson

  • *Experts*
Posted

You can go through child controls of a control like this:

dim ctrl as Control
Dim ctrl2 as Control
For Each ctrl in Me.Control
    If typeOf ctrl Is GroupBox Then 'if its a groupbox
          For Each ctrl2 In ctrl.Controls 'go through all controls in a groupbox
                'do something to each control
          Next
    End If
Next

Posted
I totally agree with you on that one. As a matter of fact, that's the solution I tried first, but I then have to limit the number of drill down, because I would create only a certain number of temp "ctrl". That's basically why I'm searching for a more flexible solution, because you can have a lot of GroupBoxes embedded in one another, like russian nesting dolls...

Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone!

--Homer Simpson

Posted

I just found this exemple on another forum. This is about the same as yours, but this one manages Levels, so it can dig in all controls of a Form, no matter what it is.

 

'global variables, may not have to be
Dim allControls As StringBuilder
Dim level As Integer


  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      allControls = New StringBuilder

      allControls.Append(Me.Name & vbCrLf)
      level = 0
      listControls(Me)

      MessageBox.Show(allControls.ToString)

  End Sub

  Private Sub listControls(ByVal ctrl As Control)

      Dim c As Control
      Dim i As Integer
      Dim j As Integer

      For i = 0 To ctrl.Controls.Count - 1
          level += 1
          For j = 1 To level
              allControls.Append(vbTab)
          Next
          allControls.Append(ctrl.Controls(i).Name() & vbCrLf)
          listControls(ctrl.Controls(i))
          level -= 1
      Next
  End Sub 

 

Hope this helps other people in the future !

 

Thanks guys

Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone!

--Homer Simpson

Posted

Well, looking to it closely, the Levels are just there for display. No real use. But it shows a differnt way of looping thru the controls with the Count property instaead of For Each Next.

 

Anyway, it works !

Now go on, boy, and pay attention. Because if you do, someday, you may achieve something that we Simpsons have dreamed about for generations: You may outsmart someone!

--Homer Simpson

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...