By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,945 Members | 1,692 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,945 IT Pros & Developers. It's quick & easy.

Treeview in .Net 2003

P: n/a
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.

Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.M" <nospam> wrote in message
news:OI**************@TK2MSFTNGP15.phx.gbl...
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.

Nov 21 '05 #2

P: n/a
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.

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:cj*******************@news.demon.co.uk...
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.M" <nospam> wrote in message
news:OI**************@TK2MSFTNGP15.phx.gbl...
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.


Nov 21 '05 #3

P: n/a
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.M" <nospam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:cj*******************@news.demon.co.uk...
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.M" <nospam> wrote in message
news:OI**************@TK2MSFTNGP15.phx.gbl...
> 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.
>
>
>



Nov 21 '05 #4

P: n/a
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
"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:cj*******************@news.demon.co.uk...
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.M" <nospam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:cj*******************@news.demon.co.uk...
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.M" <nospam> wrote in message
news:OI**************@TK2MSFTNGP15.phx.gbl...
> 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.
>
>
>



Nov 21 '05 #5

P: n/a
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.M" <nospam> wrote in message
news:eT**************@TK2MSFTNGP15.phx.gbl...
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
"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:cj*******************@news.demon.co.uk...
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.M" <nospam> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> 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.
>
>
>
> "Robin Tucker" <id*************************@reallyidont.com> wrote in
> message news:cj*******************@news.demon.co.uk...
>> 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.M" <nospam> wrote in message
>> news:OI**************@TK2MSFTNGP15.phx.gbl...
>> > 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.
>> >
>> >
>> >
>>
>>
>
>



Nov 21 '05 #6

P: n/a
L.M
I mean an equivalent to :
TreeView1.DragIcon = TreeView1.SelectedItem.CreateDragImage
from VB6 but in VB.Net

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:cj*******************@news.demon.co.uk...
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

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.