Treeview in .Net 2003

L

L.M

Hello,

I knew how to use the treeview under VB6. After migrating to .NET, well, I'm
lost.
I try to add a new node, either to the same level or as a child to a
selected node in the treeview.
However, either it only add it to the root level or it only add it on level
below, doesn't matter what I select. And in some case, I just get an
exception.

Does anyone have a short example of how to add a new node (same level/child)
to an existing treeview based on a selected node ?

I'm pretty sure I mess up with the index, but I'm really lost.

Here is what I do to add same level:

Sub InsertNode(ByRef TreeviewRef As System.Windows.Forms.TreeView, ByVal
ImgIndex As Integer, ByVal SelImgIndex As Integer, ByVal NText As String,
ByVal NIndex As Integer, ByVal NTag As String)
' Add a node to a treeview. node will be added before the selection index.
' In : treeview, imgindex,selimgindex,ntext,index,tag
' Out : -
' Ret : -
' Call ex: InsertNode(MainTreeView, 0, 0, "Root",675,"tag")
Dim oNode As New System.Windows.Forms.TreeNode
oNode.ImageIndex = ImgIndex
oNode.SelectedImageIndex = SelImgIndex
oNode.Text = NText
oNode.Tag = NTag
TreeviewRef.Nodes.Insert(NIndex, oNode)


And what I do for child level:

Sub AddChildrenNode(ByRef TreeviewRef As System.Windows.Forms.TreeView,
ByVal NIndex As Integer, ByVal NTag As String)
' Add a children a node to a treeview under the current selection.
' In : treeview, index, tag
' Out : -
' Ret : -
' Call ex: AddChildrenNode(MainTreeView, 675, "xxx")

Dim oNode As New System.Windows.Forms.TreeNode
oNode.ImageIndex = 0
oNode.SelectedImageIndex = 0
oNode.Text = "toto"
oNode.Tag = NTag
TreeviewRef.Nodes(NIndex).Nodes.Add(oNode)
TreeviewRef.Nodes(NIndex).Expand()
End Sub


Thanks.
 
R

Robin Tucker

Adding a new node to the selected node is done something like this:

' Create the new node

Dim myNewNode as new TreeNode
....
....

' Get the currently selected node

Dim theSelectedNode as TreeNode = myTree.SelectedNode

If not theSelectedNode is nothing then

theSelectedNode.Nodes.Add ( myNewNode)

Else

' Error, attempting to add a new node when no node has been selected.

End If
 
L

L.M

Hello,

That make it, thanks a lot. It seems that the Index in the .Net treeview is
based on each node collection and not on the whole treeview content....
Might be a good thing as soon as you get used to it....

Next step is the drap and drop, it works fine. I just have the mouse pointer
instead of the node text+icon as in VB6.
 
R

Robin Tucker

Item drag and drag drop.....


Private Sub TreeView_ItemDrag(ByVal sender As Object, ByVal e As
System.Windows.Forms.ItemDragEventArgs) Handles TreeView.ItemDrag

If e.Item Is Nothing Then
Exit Sub
End If

' Start the drag operation

DoDragDrop(...whatever I'm dragging...maybe a tree node (ie.
SelectedNode), DragDropEffects.Move Or DragDropEffects.Copy)

End Sub

Private Sub TreeView_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles TreeView.DragDrop

Dim mousePos As Point

mousePos = TreeView.PointToClient(New Point(e.X, e.Y))

Dim nodeOver As TreeNode = TreeView.GetNodeAt(mousePos)

If nodeOver Is Nothing Then

' not dropping on a tree node...

Else

' dropping onto "node over"

End If

End Sub
 
L

L.M

Thanks, that's what I had, in a less nice way, but the same. However, is
there a way to get the icone/text under the mouse as it was in VB6 (or in
windows explorer when you drap/drop file/folder) ?
The funtion I was using in VB6 doesn't seems to exist anymore in .Net.

L.M
 
R

Robin Tucker

Not sure what you mean by Icon/Text under the mouse. When dragging from
explorer, you will have a String() array (I expect) in the Data property.

ie. :

' Determine if the drag drop is one of our supported drop formats

If e.Data.GetDataPresent(DataFormats.FileDrop) Then

' Get the files...

Dim theFiles As String() =
DirectCast(e.Data.GetData(DataFormats.FileDrop), String())

.....

End If

End If
 
L

L.M

I mean an equivalent to :
TreeView1.DragIcon = TreeView1.SelectedItem.CreateDragImage
from VB6 but in VB.Net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top