Altering how a treeview displays data
I have an Active Directory browser written in vb.net 2008 that allows an end user to visually browse AD. Currently it displays the full DN for each child and what I want is to display just the name of the OU. Something tells me I would use the Tag() function but after playing with it, I just couldn't figure it out. All I removed was our internal domain name.
Would someone like to review what I have now and offer some ideas on what I would change to alter how the tree is viewed while retaining funuctionality?
Thanks!
CODE:
Code:
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.DirectoryServices
Imports System.DirectoryServices.ActiveDirectory
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports ErrorControl
Namespace DirectoryServicesBrowser
Partial Public Class DirectoryServicesBrowserDialog
Inherits Form
#Region "fields"
Private connectionPrefix As String
Private m_objectName As String
Private m_objectPath As String
Private m_objectGUID As String
Private userName As String
Private password As String
Private WithEvents MAIN As frmMAIN
Private _Err As String = Nothing
#End Region
#Region "properties"
Public ReadOnly Property ObjectName() As String
Get
Return m_objectName
End Get
End Property
Public ReadOnly Property ObjectPath() As String
Get
Return m_objectPath
End Get
End Property
Public ReadOnly Property ObjectGUID() As String
Get
Return m_objectGUID
End Get
End Property
#End Region
Public Sub New(ByVal userName As String, ByVal password As String)
UnhandledExceptionManager.AddHandler()
InitializeComponent()
Me.userName = Nothing
Me.password = Nothing
connectionPrefix = "LDAP://"
End Sub
#Region "Active Directory Methods - Demonstration"
Private Sub populateChildren(ByVal oParent As TreeNode)
For Each child As String In EnumerateOU(oParent.Text)
Dim oNode As New TreeNode()
oNode.Text = child
oNode.SelectedImageIndex = 1
oParent.Nodes.Add(oNode)
Next
End Sub
Private Sub populateObjectDetails(ByVal objectLdapPath As String)
Dim directoryObject As New DirectoryEntry(connectionPrefix + objectLdapPath, userName, password)
m_objectName = directoryObject.Name
m_objectPath = Replace(directoryObject.Path, "LDAP://***.*****.com/", "")
m_objectGUID = directoryObject.Guid.ToString()
End Sub
Private Function EnumerateOU(ByVal OuDn As String) As ArrayList
Dim alObjects As New ArrayList()
Try
Dim directoryObject As New DirectoryEntry(connectionPrefix + OuDn, userName, password)
For Each child As DirectoryEntry In directoryObject.Children
Dim childPath As String = child.Path.ToString()
'MsgBox(child.Name.ToString)
alObjects.Add(childPath.Remove(0, 7))
'remove the LDAP prefix from the path
child.Close()
child.Dispose()
Next
directoryObject.Close()
directoryObject.Dispose()
Catch e As DirectoryServicesCOMException
_Err = "An Error Occurred: " & e.Message.ToString()
End Try
Return alObjects
End Function
Private Function EnumerateDomains() As ArrayList
Dim alDomains As New ArrayList()
Dim currentForest As Forest = Forest.GetCurrentForest()
Dim myDomains As DomainCollection = currentForest.Domains
For Each objDomain As Domain In myDomains
alDomains.Add(objDomain.Name)
Next
Return alDomains
End Function
#End Region
#Region "events"
Private Sub DirectoryServicesBrowserDialog_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim oNode As New TreeNode()
oNode.SelectedImageIndex = 0
For Each domain As String In EnumerateDomains()
treeView1.Nodes.Add(domain)
Next
End Sub
Private Sub treeView1_AfterSelect(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles treeView1.AfterSelect
populateChildren(e.Node.Tag)
End Sub
Private Sub btnSelect_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSelect.Click
populateObjectDetails(treeView1.SelectedNode.Text.ToString())
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
End Sub
#End Region
End Class
End Namespace
DESIGNER CODE:
Code:
Namespace DirectoryServicesBrowser
* * Partial Class DirectoryServicesBrowserDialog
* * * * ''' <summary>
* * * * ''' Required designer variable.
* * * * ''' </summary>
* * * * Private components As System.ComponentModel.IContainer = Nothing
* * * *
* * * * ''' <summary>
* * * * ''' Clean up any resources being used.
* * * * ''' </summary>
* * * * ''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
* * * * Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
* * * * * * If disposing AndAlso (components IsNot Nothing) Then
* * * * * * * * components.Dispose()
* * * * * * End If
* * * * * * MyBase.Dispose(disposing)
* * * * End Sub
* * * *
* * * * #Region "Windows Form Designer generated code"
* * * *
* * * * ''' <summary>
* * * * ''' Required method for Designer support - do not modify
* * * * ''' the contents of this method with the code editor.
* * * * ''' </summary>
* * * * Private Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(DirectoryServicesBrowserDialog))
Me.treeView1 = New System.Windows.Forms.TreeView
Me.btnSelect = New System.Windows.Forms.Button
Me.btnCancel = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'treeView1
'
Me.treeView1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.treeView1.Location = New System.Drawing.Point(3, 3)
Me.treeView1.Name = "treeView1"
Me.treeView1.Size = New System.Drawing.Size(560, 237)
Me.treeView1.TabIndex = 0
'
'btnSelect
'
Me.btnSelect.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnSelect.Location = New System.Drawing.Point(398, 246)
Me.btnSelect.Name = "btnSelect"
Me.btnSelect.Size = New System.Drawing.Size(75, 23)
Me.btnSelect.TabIndex = 1
Me.btnSelect.Text = "Select"
Me.btnSelect.UseVisualStyleBackColor = True
'
'btnCancel
'
Me.btnCancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.btnCancel.Location = New System.Drawing.Point(479, 246)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.Size = New System.Drawing.Size(75, 23)
Me.btnCancel.TabIndex = 2
Me.btnCancel.Text = "Cancel"
Me.btnCancel.UseVisualStyleBackColor = True
'
'DirectoryServicesBrowserDialog
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(566, 271)
Me.Controls.Add(Me.btnCancel)
Me.Controls.Add(Me.btnSelect)
Me.Controls.Add(Me.treeView1)
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Name = "DirectoryServicesBrowserDialog"
Me.Text = "DirectoryServicesBrowserDialog"
Me.ResumeLayout(False)
End Sub
* * * *
* * * * #End Region
* * * *
Private WithEvents treeView1 As System.Windows.Forms.TreeView
Private WithEvents btnSelect As System.Windows.Forms.Button
* * * * Private btnCancel As System.Windows.Forms.Button
* * End Class
End Namespace
Re: Altering how a treeview displays data
Re: Altering how a treeview displays data
Are you still looking for help with this? I actually wrote my own little active directory browser (see here: http://www.vbforums.com/showthread.php?p=3432374) not long ago so might be able to help out. I wont bother if you have already got a solution though :)
Re: Altering how a treeview displays data
I downloaded your examples and I'll definitely take a look at them. Thanks for your contribution to the general coder populace. : )
Re: Altering how a treeview displays data
No worries, if you need a hand just getting the OU then let me know and I'm sure I can put something together :)
Re: Altering how a treeview displays data
Off the top, I like the work you've done and the final appearance. But the load/wait time is incredible. Have you researched the possibility of only populating objects when needed instead of enumerating everything? That alone would improve its usefulness by leaps and bounds.
Great job otherwise! In the meantime, I'll look at the code to see if it has what I need.
Re: Altering how a treeview displays data
Yeah its actually easier to do it that way that you mentioned but when I originally made that control I was helping someone out on here that needed it to load all at once rather than just load each OU dynamically when expanded, so thats the way I did it.
I might add an option to choose which load method to use in the next version, as well as a property that returns just the parent OU of each Node