TreeView Trouble

Bam

Member
Joined
Oct 19, 2005
Messages
15
Location
Ireland
Programming Experience
Beginner
Hi,

Was wondering if anyone could enlighten me on this on;

I have a TreeView Control, The Control has 5 nodes and each node has 2 child nodes. Beside the TreeView Control I have a TextBox Control. What I want to do is everytime i click on a each node or child node i want the certain text to show in the TextBox.

I Figured that to do this would be a case of using a simple IF - ELSE statment to find the node and insert the piece of text...

But after the Following line of code im stuck

PrivateSub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

See I cant seem to find out which part of this statment holds which node is selected and since i don't know that i cant implement a IF - ELSE loop.

Any suggestions?

By the way the stament above is what is generated when you double click the TreeView control.
 
Hey,

You can use e.Node to work out which node has been selected. e.Node.Text will return the text property of the node or e.Node.Tag will return it's tag.

Hope this helps.
 
BadgerByte said:
Hey,

You can use e.Node to work out which node has been selected. e.Node.Text will return the text property of the node or e.Node.Tag will return it's tag.

Hope this helps.

I see,

So would i need to assign e.Node.Tag value to a variable to use in a IF - Else eg:

e.Node.Tag = PretendVariable

IF PretendVariable = 1 Then

Insert Code

End IF

And If this is true would it be a case of 0123456.... For the node Values?
 
Ok thats all well and good if i want to set the values, but i cant seem to go the other way ie: Get the value from the tag after a certian node is selected then use the value in the IF loop!

I can put up the code if you like.
 
Hey,

I figured this should work if you set the tags beforehand, I may have got the wrong end of the stick here, or I may be just plain wrong, this is off the top of my head. Post back with your code if it doesn't and I'll have a proper look.

VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] TreeView1_AfterSelect([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.TreeViewEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] TreeView1.AfterSelect

If e.Node.Tag = 0 then
'Do Something
End If
if e.Node.Tag = 1 then
'Do Something else
End If
End Sub
[/SIZE]
 
Yeah thats like what my code is like, but you metioned setting the tags first. I was looking for the tags of the computer as such. For instance i thought that the nodes would have a "label" and you would call that "label" everytime you clicked on a certian node. I take it this is not the way treeview works. Forgive all the questions the Tome i have seems to just skip treeview altogther!
 
No probs Bam,

I too found it kind of weird using treeviews, but there are quite a few strange ways of identifying them. I like to change the nodes Tag property (if you don't, as you've discovered this doesn't contain anything) and then use the code as above. A lot of people use the e.Node.Text to check. so if you had a node who's text was "Tree Node 1" then you could say If e.Node.Text = "Tree Node 1". But as I said I prefer the tag mathod, no particular reason.

Hope this clears things up.
 
Ha Ha it all makes sense!!!!

I can't belive it was that easy!

Thanks man
 
Back
Top