tree view

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Director - Minvent

    #1

    tree view

    Hi,

    I am trying to work out the easiest way to add some things to a treeview.

    I have got a list of the priviliged names for each of the computers e.g.

    LDAP://DomainName/CN=ComputerName ,OU=OrgUnit1,OU =OrgUnit2,
    DC=DCRecord1,DC =DCRecord2

    I am iterating through a list of these names to split them into strings for
    the domain name and common name and an arraylist of strings for the
    organisational units.

    That bit works fine no problems, The problem I have is that on a one by one
    basis I need to add them onto the correct node of the treeview taking account
    the current structure of domains and OU's in the tree view and not
    reproducing nodes that are already present.

    Hope that makes sense!

    Thanks M
    --
    If at first you don't succeed... Hide the evidence that you tried!
  • Niclas

    #2
    Re: tree view

    Hi,

    I started off a project to build my own "AD explorer", and think your are
    after something similar. The code below build a treeview representation of
    AD. See if this gives you some help

    Regards

    Niclas

    Imports System.Director yServices
    Public Class Form1

    Inherits System.Windows. Forms.Form
    Private sRootDSE As String
    #Region " Windows Form Designer generated code "

    Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeCompo nent()

    'Add any initialization after the InitializeCompo nent() call
    sRootDSE = GetRootDSE()
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    If Not (components Is Nothing) Then
    components.Disp ose()
    End If
    End If
    MyBase.Dispose( disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.Componen tModel.IContain er

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Friend WithEvents TreeView1 As System.Windows. Forms.TreeView
    Friend WithEvents ImageList1 As System.Windows. Forms.ImageList
    <System.Diagnos tics.DebuggerSt epThrough()> Private Sub
    InitializeCompo nent()
    Me.components = New System.Componen tModel.Containe r
    Dim resources As System.Resource s.ResourceManag er = New
    System.Resource s.ResourceManag er(GetType(Form 1))
    Me.TreeView1 = New System.Windows. Forms.TreeView
    Me.ImageList1 = New System.Windows. Forms.ImageList (Me.components)
    Me.SuspendLayou t()
    '
    'TreeView1
    '
    Me.TreeView1.Im ageList = Me.ImageList1
    Me.TreeView1.Lo cation = New System.Drawing. Point(32, 24)
    Me.TreeView1.Na me = "TreeView1"
    Me.TreeView1.Si ze = New System.Drawing. Size(240, 248)
    Me.TreeView1.Ta bIndex = 0
    '
    'ImageList1
    '
    Me.ImageList1.I mageSize = New System.Drawing. Size(16, 16)
    Me.ImageList1.I mageStream =
    CType(resources .GetObject("Ima geList1.ImageSt ream"),
    System.Windows. Forms.ImageList Streamer)
    Me.ImageList1.T ransparentColor = System.Drawing. Color.Transpare nt
    '
    'Form1
    '
    Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
    Me.ClientSize = New System.Drawing. Size(552, 302)
    Me.Controls.Add (Me.TreeView1)
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout (False)

    End Sub

    #End Region


    Sub GetOU()


    Dim root As New DirectoryEntry( "LDAP://" & sRootDSE)


    Dim searcher As New DirectorySearch er(root)


    Dim s As SearchResult
    Dim nodearr() As TreeNode
    searcher.Search Scope = SearchScope.One Level
    Dim I As Integer
    Dim cp As IComparer
    cp = New NodeComparer

    ReDim nodearr(searche r.FindAll.Count - 1)

    For Each s In searcher.FindAl l
    Dim no As New
    TreeNode(s.GetD irectoryEntry.P roperties("Name ").Value.ToStri ng,
    GetObjectClass( s.GetDirectoryE ntry.Properties ("Distinguished Name").Value.To String),
    GetObjectClass( s.GetDirectoryE ntry.Properties ("Distinguished Name").Value.To String))
    no.Tag =
    s.GetDirectoryE ntry.Properties ("Distinguished Name").Value.To String
    nodearr(I) = no

    I += 1
    Next
    Array.Sort(node arr, cp)

    Dim k As TreeNode


    TreeView1.Nodes .AddRange(nodea rr)



    End Sub

    Private Sub Form1_Load(ByVa l sender As Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    GetOU()

    End Sub

    Private Sub TreeView1_Doubl eClick(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles TreeView1.Doubl eClick

    If TreeView1.Selec tedNode.GetNode Count(True) < 1 Then
    Dim root As New DirectoryEntry( "LDAP://" &
    TreeView1.Selec tedNode.Tag.ToS tring)
    Dim searcher As New DirectorySearch er(root)
    Dim s As SearchResult
    Dim nodearr As TreeNode()
    searcher.Search Scope = SearchScope.One Level
    Dim I As Integer
    Dim cp As IComparer
    cp = New NodeComparer

    ReDim nodearr(searche r.FindAll.Count - 1)

    For Each s In searcher.FindAl l
    Dim no As New
    TreeNode(s.GetD irectoryEntry.P roperties("Name ").Value.ToStri ng,
    GetObjectClass( s.GetDirectoryE ntry.Properties ("Distinguished Name").Value.To String),
    GetObjectClass( s.GetDirectoryE ntry.Properties ("Distinguished Name").Value.To String))
    no.Tag =
    s.GetDirectoryE ntry.Properties ("Distinguished Name").Value.To String
    nodearr(I) = no
    I += 1
    Next
    Array.Sort(node arr, cp)
    TreeView1.Selec tedNode.Nodes.A ddRange(nodearr )
    TreeView1.Selec tedNode.Expand( )
    End If



    End Sub

    Private Function GetRootDSE() As String

    Dim RootEntry As New DirectoryEntry( "LDAP://RootDSE")
    Return RootEntry.Prope rties("DefaultN amingContext"). Value.ToString
    'Add error handling
    End Function

    Private Function GetObjectClass( ByVal DN As String) As Integer
    Dim m As New DirectoryEntry( "LDAP://" & DN)


    Dim r As New DirectoryEntry( m.NativeObject. schema.ToString )

    Select Case r.Name
    Case "organizational Unit"
    Return 0
    Case "computer"
    Return 1
    Case "user"
    Return 2
    Case Else
    Return 0
    End Select
    End Function



    End Class
    Class NodeComparer
    Implements IComparer 'Implement the IComparer Interface
    Overloads Function Compare(ByVal x As Object, ByVal y As Object) As
    Integer Implements IComparer.Compa re
    Return CType(x, TreeNode).Image Index - CType(y, TreeNode).Image Index

    End Function
    End Class


    "Director - Minvent" <DirectorMinven t@discussions.m icrosoft.com> wrote in
    message news:D11491E1-E113-4B7C-9EB8-D5920F77B180@mi crosoft.com...[color=blue]
    > Hi,
    >
    > I am trying to work out the easiest way to add some things to a treeview.
    >
    > I have got a list of the priviliged names for each of the computers e.g.
    >
    > LDAP://DomainName/CN=ComputerName ,OU=OrgUnit1,OU =OrgUnit2,
    > DC=DCRecord1,DC =DCRecord2
    >
    > I am iterating through a list of these names to split them into strings
    > for
    > the domain name and common name and an arraylist of strings for the
    > organisational units.
    >
    > That bit works fine no problems, The problem I have is that on a one by
    > one
    > basis I need to add them onto the correct node of the treeview taking
    > account
    > the current structure of domains and OU's in the tree view and not
    > reproducing nodes that are already present.
    >
    > Hope that makes sense!
    >
    > Thanks M
    > --
    > If at first you don't succeed... Hide the evidence that you tried![/color]


    Comment

    Working...