[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.net TreeView image list populating all nodes problem

Posted on 2012-04-01
5
Medium Priority
?
1,971 Views
Last Modified: 2012-04-03
Hi

I am using the following code to populate a TreeView control in VB.net.
The result is shown on the image below.
For some reason, all the nodes are using the second image "TreeView" when
I don't want them to have any image. All I wanted to populate is cube and only
the bottom one. I don't know why it is in the parent as well

    Sub Load_Images()
        Dim myImageList As New ImageList()
        myImageList.Images.Add(My.Resources.cube)
        myImageList.Images.Add(My.Resources.TreeView)
        myImageList.Images.Add(My.Resources.TreeView2)

        ' Assign the ImageList to the TreeView.
        Me.TreeView_From.ImageList = myImageList

        Me.TreeView_From.Nodes(0).Nodes(2).ImageIndex = 0

    End Sub
1
0
Comment
Question by:Murray Brown
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37793844
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37795453
What are the image indices of the other nodes? Does it work if you set imageindex on one of the nodes to -1?
0
 

Author Comment

by:Murray Brown
ID: 37800428
Hi CodeCruiser

The image is populated with the following code that you saw in a different question. Perhaps that will shed some light on the indices
  Public Sub Populate_TreeView_From_SQL()

        Dim SQLExpression As String = _
            "SELECT TABLE_NAME, COLUMN_NAME FROM information_schema.columns ORDER BY TABLE_NAME,COLUMN_NAME"

        Dim Rootnode As TreeNode = Nothing
        Dim Mainnode As TreeNode = Nothing
        Dim Childnode As TreeNode = Nothing

        Dim MainName As String = String.Empty

        Dim oConnectionString As String = Me.RichTextBox1.Text
        Dim cn As New SqlConnection(oConnectionString)

        Dim adp As New SqlDataAdapter(SQLExpression, cn)

        Dim ds As New DataSet

        adp.Fill(ds, "SystemData")

        Me.TreeView_From.Nodes.Clear()
     
        Rootnode = Me.TreeView_From.Nodes.Add(key:="Root", text:="database name here", _
                                        imageIndex:=0, selectedImageIndex:=0)



        For Each row As DataRow In ds.Tables("SystemData").Rows

            If MainName <> row(0).ToString Then
                Mainnode = Rootnode.Nodes.Add(key:="Table", text:=row(0).ToString, _
                imageIndex:=1, selectedImageIndex:=1)
                MainName = row(0).ToString
            End If

            Childnode = Mainnode.Nodes.Add(key:="Column", text:=row(1).ToString, _
            imageIndex:=2, selectedImageIndex:=2)

        Next

        Me.TreeView_From.Nodes(0).EnsureVisible()
        Me.TreeView_From.ExpandAll()
        Me.TreeView_From.Scrollable = True

        ds.Dispose()
        ds = Nothing
        adp.Dispose()
        adp = Nothing
        cn.Dispose()
        cn = Nothing

    End Sub
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 37802105
Yes you are setting the image indices of all the nodes when creating these.

Change this

            Childnode = Mainnode.Nodes.Add(key:="Column", text:=row(1).ToString, _
            imageIndex:=2, selectedImageIndex:=2)


to

            Childnode = Mainnode.Nodes.Add(row(1).ToString)
0
 

Author Closing Comment

by:Murray Brown
ID: 37803169
thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question