Click here to Skip to main content
Licence CPOL
First Posted 14 Jun 2008
Views 20,522
Downloads 205
Bookmarked 23 times

Working with Multiple Images with Each TreeNode

By Mohd Arshad (Sam) | 14 Jun 2008
Working with Multiple Images with Each TreeNode
3 votes, 18.8%
1

2
1 vote, 6.3%
3
1 vote, 6.3%
4
11 votes, 68.8%
5
3.65/5 - 16 votes
μ 3.65, σa 2.82 [?]

Introduction

In general, we can use only one image with each TreeNode. There may be cases when we need multiple images, two, three, or more. We may also want to perform different operations on click of different images. Applying some trick, our single image can work as multiple images like I did in the example of this article. You just have to merge those images using any image editing software, i.e. Photoshop. This can be done by using ImageList in .NET, assign that to TreeView.

Example6

Using the Code

Identifying and performing operations on those images is as easy as adding images. Just work with TreeView’s NodeMouseClick event as below…

Private Sub trvMain_NodeMouseClick(ByVal sender As System.Object, _
                ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) _
						Handles trvMain.NodeMouseClick
     Dim x = e.Node.Bounds.X
     Dim y = e.Node.Bounds.Y
     Dim h = e.Node.Bounds.Y + e.Node.Bounds.Height
     If e.X > x - 18 And e.X < x - 3 Then
         If e.Y > y And e.Y < h Then
             If e.Node.Nodes.Count = 0 Then
                 MsgBox(e.Node.Text + " Form")
             End If
         End If
     ElseIf e.X > x - 35 And e.X < x - 18 Then
         If e.Y > y And e.Y < h Then
             MsgBox(e.Node.Text + " Document")
         End If
     End If
 End Sub

In the beginning of this subroutine, we have recognized the bounds of the current TreeNode. Then we have to check the location of mouse click, if the mouse was clicked on Document Image, then Else part of IF…Else statement will get true, and if mouse was clicked on Form Image then IF part of IF…Else statements will get true. In both cases, we can perform any operation, while I just show a message.

The next thing is if you don't want the same image for each TreeNode, i.e. you want to change images of TreeNodes which have children. Do it during runtime, when page loads, by changing the ImageIndex property of those nodes.

'Changing ImageIndex of TreeNodes which has Subtrees
	Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
						Handles Me.Load
         For Each node As TreeNode In trvMain.Nodes
             If node.Nodes.Count > 0 Then
                 node.ImageIndex = 1
                 node.SelectedImageIndex = 1
                 checkChild(node)
             End If
         Next
    End Sub


    'Recurring subroutine to check whether the child node has subtree.
    Private Sub checkChild(ByVal nod As TreeNode)
         For Each node As TreeNode In nod.Nodes
              If node.Nodes.Count > 0 Then
                   node.ImageIndex = 1
                   node.SelectedImageIndex = 1
                   checkChild(node)
              End If
         Next
    End Sub

Points of Interest

You can use any number of images, the sole thing you have to do is merge them using any image editing software. Also remember when you are checking for mouse click position that you must know the exact image size, so that you can easily apply that into if...else conditions.

Final Words

I hope you will find this article helpful. For more information, you can check out the demo program available in the downloads section or you can contact me at arshad@m-arshad.co.cc Good luck!

History

  • 14th June, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohd Arshad (Sam)

Software Developer (Senior)
Cherisys Technologies, WebStreet.in
India India

Member

Follow on Twitter Follow on Twitter

Software professional with demonstrated strength in windows-based and web-based software development. Have 4 years of experience with the full software development lifecycle including requirements, design, development, testing/QA, deployment, training & support. Have 1 year experience managing groups, planning and executing implementations. Practical working knowledge of all aspects of IT. Possess strong insight into practical business considerations.



www.cherisys.com
www.webstreet.in
www.codevdo.com
www.bizkut.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThis is really cool solution Pinmemberpamperghost3:00 3 Apr '10  
GeneralRe: This is really cool solution PinmemberMohd Arshad (Sam)5:24 3 Apr '10  
GeneralMy vote of 1 Pinmemberrob397890:42 24 Dec '08  
GeneralRe: My vote of 1 PinmemberMohd Arshad (Sam)14:55 31 Mar '10  
GeneralInefficient Pinmembermav.northwind21:51 14 Jun '08  
GeneralRe: Inefficient PinmemberMohd Arshad (Sam)22:14 14 Jun '08  
Thank You for your response. I'll surely try for it. I wish to know more details about that method from you. I'll be very thankful to you.
 
Sincerely yours,
Mohd Arshad
GeneralRe: Inefficient Pinmembermav.northwind19:22 17 Jun '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 14 Jun 2008
Article Copyright 2008 by Mohd Arshad (Sam)
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid