Tuesday, October 27, 2009

Parent node image getting selected when click on others child nodes in the TreeView

Parent node image getting selected when click on others child nodes in the TreeView
 
While working with tree view control having some images, I faced following issue.
My treeview control has some text value and image in the parent nodes and corresponding child nodes. I bind treeview control with imagelist control to display image in the treeview. Image got binded properly and working fine, BUT when I clicks on the child nodes or some other nodes, very first parent node image get replaced with existing node image.
Solution: while binding the image into treeview nodes use "SelectedImageIndex" property along with "ImageIndex" property with same image index number.
 
Example:
parentNode.ImageIndex = 0
parentNode.SelectedImageIndex = 0
 
VB.NET Source Code
 
Below the vb.net source code to bind image with the treeview controls
 
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
'Load the Imagelist with desired images
Dim customImageList As New ImageList
customImageList.Images.Add("AMandatory", Bitmap.FromFile("C:\AMandatory.ico"))
customImageList.Images.Add("BDiscretionary", Bitmap.FromFile("C:\BDiscretionary.ico"))
customImageList.Images.Add("AAMandatory", Bitmap.FromFile("C:\AAMandatory.ico"))
customImageList.Images.Add("BBDiscretionary", Bitmap.FromFile("C:\BBDiscretionary.ico"))
 
'Bind ImageList with TreeView
TreeView1.ImageList = customImageList
 
'Create a ParentNode
Dim rootNodeMandatory As TreeNode = TreeView1.Nodes.Add("Mandatory Text")
'Bind first image from imagelist
rootNodeMandatory.ImageIndex = 0
rootNodeMandatory.SelectedImageIndex = 0
 
'Create the child Node
Dim childNodeMandatory As TreeNode
childNodeMandatory = rootNodeMandatory.Nodes.Add("Mandatory Child Text")
'Bind third image from imagelist
childNodeMandatory.ImageIndex = 2
childNodeMandatory.SelectedImageIndex = 2
 
'Create another ParentNode
Dim rootNodeDiscretionary As TreeNode = TreeView1.Nodes.Add("Discretionary Text")
'bind second image from imagelist
rootNodeDiscretionary.ImageIndex = 1
rootNodeDiscretionary.SelectedImageIndex = 1
 
'Create the child Node
Dim childNodeDiscretionary As TreeNode
childNodeDiscretionary = rootNodeDiscretionary.Nodes.Add("Discretionary Child Text")
'Bind fourth image from imagelist
childNodeDiscretionary.ImageIndex = 3
childNodeDiscretionary.SelectedImageIndex = 3
 
End Sub

1 comment:

Anonymous said...

Physical Properties Of Drug fartonast

order venlafaxine
Moreover, Effexor is meant to use for adult uses only, in general.
[url=http://www.outdoorwildernessinfo.com/]discount effexor online[/url]
So, it’s something that you should buy if you’re also finding yourself in trouble due to any of these problems.
http://www.outdoorwildernessinfo.com/ - buy cheap effexor
So it’s necessary for you to take a break of at least 2 weeks before you take this drug and quit the MAO inhibitor intake.


levitra price
Similar to other two medicines this also helps to increase flow of blood in penis.
[url=http://www.aur-phala.com/]buy levitra online[/url]
Levitra cures male impotence
Buy Levitra (Vardenafil) at reliable online pharmacy and save extra money! No prescription! No hidden fees!
Levitra is the most sought in after medicine for Erection Dysfunction (ED) problem.
http://www.aur-phala.com/ - buy levitra online
The minimum dosage is only once per a day.