getting the right index of a child node.
Hi,
Im trying to use a treeview control to navigate through records on a different form, setting the position of the bindingsource on that form to the index of the selected node.
Now, the nodes I select are child nodes, so I need to get the index 5 if I select the first childnode under a parent and there are 4 other childnodes under different parents. As it is, I get index 1.
Do you know how I can solve this problem?
Fuga.
Re: getting the right index of a child node.
don't use the index.... try setting the key value and use tat.
-tg
Re: getting the right index of a child node.
Thanks for your reply.
The key value is set when you add the treenode, right? I can´t seem to access it. Here´s the code:
Code:
For Each tvrw In tvds.Tables(0).Rows
tvnode = New TreeNode
tvnode.Tag = tvrw.Item("Fid")
tvnode.Text = tvrw.Item("rubrik").ToString
tvnode.ToolTipText = tvrw.Item("info").ToString
Faktors.TreeView1.Nodes(0).Nodes.Add(tvnode)
If tvnode.Tag.ToString = Evaluate.TextBox7.Text Then
Faktors.TreeView1.SelectedNode = tvnode
End If
Next
I also tried using the name property but that didn´t work properly.
How do you think I should do it?
Thanks again.
Fuga.
Re: getting the right index of a child node.
I've done similar before using the .Tag of the node, then finding the node with the appropriate tag, so it doesnt matter when its shifted around.
But if you can get a key method working as recommended (which i wasnt aware of) that would save searching the nodes.
Re: getting the right index of a child node.
Thanks!
Yes I am using the tag, but for a different purpose.
The thing is, the posts are recorded in one way in the db, so I use the tag to find them there. But they are displayed in a different way in the form so I thought I should use the key for that.
I suppose I could use the tag and find the corresponding position in the bindingsource, move to that position etc, but I was hoping for a quicker way.
Fuga.
Re: getting the right index of a child node.
Well a tag is of type "Object" menaing you could put anything in there, like an array to hold 2 values.
But as for the key value, I found this msdn entry about it http://support.microsoft.com/kb/311318 it seems to explain it in depth
Re: getting the right index of a child node.
Thanks! That really helps a lot.
Quote:
Well a tag is of type "Object" menaing you could put anything in there, like an array to hold 2 values.
Why is it that you always make a fool of yourself when you´re a beginner?:blush: :p
Great that I learned this, it´s going to help me in my project.
Fuga.
Re: getting the right index of a child node.
Hmm...
I´ve been trying to use the tag as an array, but I can´t get it to work.
I know how to declare the array and to assign values, but I don´t know how to turn the tag into an array.
I´m doing something wrong, aren´t I?
Fuga.
Re: getting the right index of a child node.
I've seen other people bump, so I try it too.
I haven´t been able to figure out how to assign an array to the tag property of a treenode. Can´t find it at msdn either.
Any help will be very appreciated.
Fuga.
Re: getting the right index of a child node.
Hi again,
I thought you were going to use the keys solution from msdn?
Anwyay, a Tag is a type "Object" so it can be ANYTHING, so theres no reason why it can't be an array, there's several ways to go about it.
here is the 1 liner way to make an array, test this for yourself (im just using a buttons tag)
Code:
Button1.Tag = New String() {"hi", "hello"}
MsgBox(Button1.Tag(0))
MsgBox(Button1.Tag(1))
a slower way, depending on your circumstances you may like better
Code:
Dim a As New List(Of String)
a.Add("hi")
a.Add("hello")
Button1.Tag = a.ToArray
Hope that helps.
Re: getting the right index of a child node.
Thanks Phill64!
No, once you told me that a tag could hold an array, I thought that would be a good way to go about it. I thought the key method looked more complex, and since I barely know what I´m doing half the time I thought I'll try the array stuff.:)
However, I think I´ve managed to put an array of values into the tag, but I´ve run into a different problem, which I posted in another thread.
http://www.vbforums.com/showthread.php?t=498811
Fuga.