Hi guys
I need your help a bit.
I have designed a tree view control and the user can select multiple options.
Now the problem lies in:
"When the user clicks on show button a msgbox appears showing all the items selected"....This is what I want to do. but is clueless on how to do.
Please help me out ASAP
Thank You in advance

What does tv1.SelectedItem.Key returns ?

Hi
tv1.selecteditem.key returns nothing but a blank msgbox.
I had already tried it.
Is there any other option?

Hi
common man....isn't there any one who can help me out?

You will have to enumerate through the nodes to find out which ones have been selected and if they have then retrieve the information you want from them...

Option Explicit

Private Sub Form_Load()
Dim N As Node
Set N = TV.Nodes.Add(, , "Root1", "Root1")
Set N = TV.Nodes.Add(, , "Root2", "Root2")
Set N = TV.Nodes.Add(, , "Root3", "Root3")
Set N = TV.Nodes.Add(, , "Root4", "Root4")

End Sub

Private Sub Command1_Click()
Dim N As Node, ForLoopCounter As Integer

For ForLoopCounter = 1 To TV.Nodes.Count
  If TV.Nodes(ForLoopCounter).Selected = True Then
    MsgBox TV.Nodes(ForLoopCounter).Key
  End If
Next ForLoopCounter

For Each N In TV.Nodes
  If N.Checked = True Then
    MsgBox N.Text
  End If
Next N
End Sub

Now, as you can see, if you are using check boxes (as I set mine up to have), the checked property is different than the selected propert and as you can see, I have shown you two different ways in which to enumerate through the items in a treeview control...

Good Luck

Hi, Ankush!

So, if you don't explicit a value for the key when you load your TreeView control, it will return empty if you test.

Take a look:

'TreeView1.Nodes.Add Relative, Relationship, Key, Text, Image, ImageSelected
  TreeView1.Nodes.Add , , , "Texto1"
  TreeView1.Nodes.Add , , "K01", "Texto2"

If you put the code MsgBox TreeView1.SelectedItem.Key in a button, if user select the first option and click the button, the dialog box shows empty; if the user select the second option and click button again, the dialog box will show "K01".
It means that you aren't filling the key property in your treeview control.


So, I'm thinking in your problem and maybe the key property would help you to code what you want.
If you use the primary key field in this property, you easily select the match option in any other control(s). Don't forget to use a letter at first in the key property - for example, if your primary key is an integer value, you should put "K" and then the primary key value, like TreeView1.Nodes.Add , , "K" & oDados.Fields("YOURPRIMARYKEY").Value, oDados.Fields("TheFieldYouWantUserToSee").Value ...


And, for my information, what property you change to made TreeView multiselectable? In my tests, I can't find this functionality, until I changed to turn checkboxes on...


Regards!

Sidnei


Hi
tv1.selecteditem.key returns nothing but a blank msgbox.
I had already tried it.
Is there any other option?

Originally quote by sidnei:
And, for my information, what property you change to made TreeView multiselectable? In my tests, I can't find this functionality, until I changed to turn checkboxes on...

This is the coding I have done for multiselection:

Private Sub tvwNodeSelect(Optional Node As Node)
Dim i As Long
Dim selNode As Node
Dim tvwnodecol As Collection
If blCtrlSel Then
    If Node.BackColor = vbHighlight Then
        If tvwnodecol.Count > 1 Then
            Node.BackColor = vbWindowBackground
            Node.ForeColor = vbWindowText
            Node.Selected = False
            tvwnodecol.Remove Node.Key
        End If
        Exit Sub
    End If
End If

If Not Node Is Nothing Then
    Node.BackColor = vbRed
    Node.ForeColor = vbBlack
MsgBox (Node.Text)
End If
End Sub

Hi vb5prgrmr
I think I was not clear in my text.
By the way thanks a lot for your help
What I want is the user first makes the selection and then all the selections he has made appear in a text box.
Thanks in advance

Well with the code I have provided, you should be able to figure it out...

Good Luck

Hi vb5prgrmr
Thanks
I will go through it again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.