Hi Everyone
hi was trying to call a different and Anonymous objects into my form
but i can't reach to the right method tyo do that

my matter here is falls under calling a tree view....
i mean any tree view !!!

i was trying to handle this code but it doesn't work

Dim tr As Control
        For Each tr In Me.Controls
            If TypeOf tr Is TreeView Then
                Do Until tr.Nodes.Count <= 1000
                    Dim x As Integer
                    Dim y As Integer
                    x = 0
                    y = 0
                    tr.Nodes(x).BackColor = Color.Navy
                    x += 1
                    tr.Nodes(y).BackColor = Color.White
                    y += 1
            End If

as i mean i tried to crate a loop into treeview nodes to change the colors but it doesn't work
and have a casting exception

so if you have some help
please tell me

thanks a lot


Edited by samehsenosi: n/a

8 Years
Discussion Span
Last Post by sknake

Your code doesn't make any sense: Do Until tr.Nodes.Count <= 1000 .
In the above line of code you're continuing a loop until the node count is less than 1000 ... but in the body of the loop you're not removing any nodes so that loop will continue forever if you have > 1000 nodes, and never excute if you have <= 1000.

Here is one way to iterate controls and find your treeview:

Public Class frmTreeView
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    For Each ctrl As Control In Me.Controls
      If TypeOf ctrl Is TreeView Then
        Dim tv As TreeView = CType(ctrl, TreeView)
        For i1 As Integer = 0 To 99
      End If
  End Sub
End Class

Edited by sknake: n/a


many thanks for your replay mr: sknake
but i was want for all to call any Treeview witch dropped into my form and make it changed automatically to my custom setting
like forecolor and backcolor and font
but i found some problem with the Nods or rows count to deal with

so i need a code to call this method that i want
if you have any idea please tell me

thanks again


Ah, I see. Are you also wanting to change the colors of child nodes? I think this might be an example of what you are trying to accomplish:

Private Shared Sub RecurseNode(ByRef tn As TreeNode, ByRef cnt As Integer)
    cnt += 1
    If ((cnt Mod 2) = 0) Then
      tn.BackColor = Color.Navy 'Even
      tn.BackColor = Color.White 'Odd
    End If
    For Each childNode As TreeNode In tn.Nodes
      RecurseNode(childNode, cnt)
  End Sub

  Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
    Dim cnt As Integer = 0
    For Each ctrl As Control In Me.Controls
      If TypeOf ctrl Is TreeView Then
        Dim tv As TreeView = CType(ctrl, TreeView)
        For Each tn In tv.Nodes
          RecurseNode(tn, cnt)
      End If
  End Sub

many thanks MR :sknake
this code is working with me so good

but i was wondering if i can do it with datagrid and it's rows and header cells
and datalist and its rows something like that

and more many thanks to you



Sure, give it a shot and post your code and we'll see what can be done.

Please mark this thread as solved if you have found an answer to your original question and good luck!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.