Tree View - List OU's

G

Guest

I have very limited knowledge of VB and am trying to convert this to list
Just OU then when selected fill a text box with the full DN. I know this is a
big ask but can anyone help.

I know i need a filter to filter on Just OU.

The code i have thus far is:


Private Sub subRootNode()
Try
dBase = New DirectoryEntry("LDAP://" & DomainName())
Dim oRootNode As TreeNode = Me.treeviewOU.Nodes.Add(dBase.Path)
oRootNode.ImageIndex = 0
oRootNode.SelectedImageIndex = 0
oRootNode.Tag = dBase
oRootNode.Text = dBase.Name.Substring(3)
oRootNode.Nodes.Add("")
Catch ex As Exception
MessageBox.Show("Cannot create initial node: " & ex.ToString)
End
End Try
End Sub

Private Sub treeviewOU_BeforeExpand(ByVal sender As Object, ByVal e As
System.Windows.Forms.TreeViewCancelEventArgs) Handles treeviewOU.BeforeExpand
Windows.Forms.Cursor.Current = Cursors.WaitCursor
If e.Node.ImageIndex > 3 Then Exit Sub
Try
If e.Node.GetNodeCount(False) = 1 And e.Node.Nodes(0).Text = ""
Then
e.Node.Nodes(0).Remove()
subEnumerateChildren(e.Node)
End If
Catch ex As Exception
MessageBox.Show("Unable to expand " & e.Node.FullPath & ":" &
ex.ToString)
End Try
If e.Node.GetNodeCount(False) > 0 Then
e.Node.ImageIndex = 1
e.Node.SelectedImageIndex = 1
End If
Windows.Forms.Cursor.Current = Cursors.Default
End Sub

Private Sub subEnumerateChildren(ByVal oRootNode As TreeNode)
Dim deParent As DirectoryEntry = DirectCast(oRootNode.Tag,
DirectoryEntry)
For Each deChild As DirectoryEntry In deParent.Children
Dim oChildNode As TreeNode = oRootNode.Nodes.Add(deChild.Path)
Select Case deChild.SchemaClassName
Case "computer"
oChildNode.ImageIndex = 4
oChildNode.SelectedImageIndex = 4
Case "user"
oChildNode.ImageIndex = 5
oChildNode.SelectedImageIndex = 5
Case "group"
oChildNode.ImageIndex = 6
oChildNode.SelectedImageIndex = 6
Case "organizationalUnit"
oChildNode.ImageIndex = 0
oChildNode.SelectedImageIndex = 0
oChildNode.Nodes.Add("")
Case "container"
oChildNode.ImageIndex = 0
oChildNode.SelectedImageIndex = 0
oChildNode.Nodes.Add("")
Case "publicFolder"
oChildNode.ImageIndex = 3
oChildNode.SelectedImageIndex = 3
oChildNode.Nodes.Add("")
Case Else
oChildNode.ImageIndex = 2
oChildNode.SelectedImageIndex = 2
oChildNode.Nodes.Add("")
End Select
oChildNode.Tag = deChild
oChildNode.Text = deChild.Name.Substring(3).Replace("\", "")
Next
End Sub

Private Sub treeviewOU_BeforeCollapse(ByVal sender As Object, ByVal e As
System.Windows.Forms.TreeViewCancelEventArgs) Handles
treeviewOU.BeforeCollapse
e.Node.ImageIndex = 0
e.Node.SelectedImageIndex = 0
End Sub
 
G

Guest

I have very limited knowledge of VB and am trying to convert this to list
Just OU then when selected fill a text box with the full DN. I know this is a
big ask but can anyone help.
Private Sub treeviewOU()

TreeView1.Nodes.AddRange(New TreeNode() {OUS})

Dim ldap As New DirectoryEntry("LDAP://" +
Environment.GetEnvironmentVariable("USERDNSDOMAIN"))
Dim LDAPChildren As DirectoryEntries
LDAPChildren = ldap.Children
ParentNode = OUS

FillTreeView(ldap, LDAPChildren)
End Sub

Private Sub FillTreeView(ByVal LDAPEntry As DirectoryEntry, ByVal
LDAPEntries As DirectoryEntries)

Dim newNode As New TreeNode(GetName(LDAPEntry.Name))
newNode.Tag = LDAPEntry.Path
Try
ParentNode.Nodes.Add(newNode)
Catch ex As Exception
MsgBox(ex.ToString)
End Try

Try
For Each LDAPEntry In LDAPEntries
Select Case LDAPEntry.SchemaClassName.ToString
Case "organizationalUnit"
ParentNode = newNode
FillTreeView(LDAPEntry, LDAPEntry.Children)
Case "container"
ParentNode = newNode
FillTreeView(LDAPEntry, LDAPEntry.Children)
End Select
Next

Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Function GetName(ByVal strName As String) As String
Dim pos As Integer
pos = strName.IndexOf("=")
GetName = strName.Substring(pos + 1)
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top