how to add node to treeview

  • Thread starter Thread starter Co
  • Start date Start date
C

Co

Hi All,

I want to be able to add a node to a treeview on the fly.
When I rightclick on a node in a treeview I want to add a new subnode
underneath the node I clicked.
I use following code to do so:

Private Sub TMenuClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.ToolStripItemClickedEventArgs)

'Point to menu item clicked in the Treeview
Select Case e.ClickedItem.Text
Case "Add Folder"
Dim sNewFolder = "Nieuwe Map"
Dim node = TreeView.SelectedNode
Dim nodeID = node.row("ID")
TreeView.Nodes.Add(sNewFolder)
TreeView.Refresh()
Case "Edit Folder" : MsgBox("Edit")
Case "Delete Folder" : MsgBox("Delete")
End Select

End Sub

What it does is create a new root node instead of underneath the
clicked node.
Also I want to be able to change the name from Nieuwe Map into
something else.
Like in windows explorer when you create a new Folder it stays
selected and you can change the name.

Marco
 
Co said:
Hi All,

I want to be able to add a node to a treeview on the fly.
When I rightclick on a node in a treeview I want to add a new subnode
underneath the node I clicked.
I use following code to do so:

Private Sub TMenuClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.ToolStripItemClickedEventArgs)

'Point to menu item clicked in the Treeview
Select Case e.ClickedItem.Text
Case "Add Folder"
Dim sNewFolder = "Nieuwe Map"
Dim node = TreeView.SelectedNode
Dim nodeID = node.row("ID")
TreeView.Nodes.Add(sNewFolder)
TreeView.Refresh()
Case "Edit Folder" : MsgBox("Edit")
Case "Delete Folder" : MsgBox("Delete")
End Select

End Sub

What it does is create a new root node instead of underneath the
clicked node.
Also I want to be able to change the name from Nieuwe Map into
something else.
Like in windows explorer when you create a new Folder it stays
selected and you can change the name.

Marco

You are close. You have the node you want to add the new node to in the
line:

Dim node = TreeView.SelectedNode (you may want to ensure that a value comes
back rather than nothing).

So change:

TreeView.Nodes.Add(sNewFolder)

to

node.Nodes.Add(sNewFolder)

One other thing. It is really bad pratice to name object with the name of
the object type. While it may not seem like a problem, don't look at the
code for a week and then try to figure out what is happening. Also many
object have shared properties or methods and if you use the object type name
as the object name it will become very confusing.

Hope this helps
LS
 
You are close.  You have the node you want to add the new node to in the
line:

Dim node = TreeView.SelectedNode (you may want to ensure that a value comes
back rather than nothing).

So change:

TreeView.Nodes.Add(sNewFolder)

to

node.Nodes.Add(sNewFolder)

One other thing.  It is really bad pratice to name object with the nameof
the object type.  While it may not seem like a problem, don't look at the
code for a week and then try to figure out what is happening.  Also many
object have shared properties or methods and if you use the object type name
as the object name it will become very confusing.

Hope this helps
LS

Thanks for that.
Do you know if there is a way to change the name of the new node from
"New Folder" to
something meaningful after it has been created?

Marco
 
You are close. You have the node you want to add the new node to in the
line:

Dim node = TreeView.SelectedNode (you may want to ensure that a value
comes
back rather than nothing).

So change:

TreeView.Nodes.Add(sNewFolder)

to

node.Nodes.Add(sNewFolder)

One other thing. It is really bad pratice to name object with the name of
the object type. While it may not seem like a problem, don't look at the
code for a week and then try to figure out what is happening. Also many
object have shared properties or methods and if you use the object type
name
as the object name it will become very confusing.

Hope this helps
LS

Thanks for that.
Do you know if there is a way to change the name of the new node from
"New Folder" to
something meaningful after it has been created?

Marco

Marco,

I am not sure if you are confusing the name and the text for the node
but the line:

node.Nodes.Add(sNewFolder)

will return a TreeNode object. From that object you can change text, name
or any of the other properties.

LS
 
Thanks for that.
Do you know if there is a way to change the name of the new node from
"New Folder" to
something meaningful after it has been created?

Marco

Marco,

    I am not sure if you are confusing the name and the text for the node
but the line:

node.Nodes.Add(sNewFolder)

will return a TreeNode object.  From that object you can change text, name
or any of the other properties.

LS

What I mean is following:
when I add the new node called "Nieuwe Map" and refresh the Treeview I
want to edit the name in the treeview like
you can do with Windows Explorer. When the folder is added it is
highlighted, the cursor is in the name, you edit and click somewhere
to save the new name.

Marco
 
Co said:
What I mean is following:
when I add the new node called "Nieuwe Map" and refresh the Treeview I
want to edit the name in the treeview like
you can do with Windows Explorer. When the folder is added it is
highlighted, the cursor is in the name, you edit and click somewhere
to save the new name.

Set the Treeview's LabelEdit property to True.


Armin
 
Thanks for that.
Do you know if there is a way to change the name of the new node from
"New Folder" to
something meaningful after it has been created?

Marco

Marco,

I am not sure if you are confusing the name and the text for the node
but the line:

node.Nodes.Add(sNewFolder)

will return a TreeNode object. From that object you can change text, name
or any of the other properties.

LS

What I mean is following:
when I add the new node called "Nieuwe Map" and refresh the Treeview I
want to edit the name in the treeview like
you can do with Windows Explorer. When the folder is added it is
highlighted, the cursor is in the name, you edit and click somewhere
to save the new name.

Marco


Ok there is a LabelEdit property for the TreeView, set it to true to allow
the behaviour you are describing. In order to catch the changes you can use
the BeforeLabelEdit, AfterLabelEdit events of the TreeView. This would mean
that you set the LabelEdit property to true and if you want to cancel the
edit use the BeforeLabelEdit and set the CancelEdit property of the
NodeLabelEditEventArgs to true to cancel. To get the text the user typed in
use the AfterLabelEdit event and pull the Label property from the
NodeLabelEditEventArgs .

LS

LS
 
What I mean is following:
when I add the new node called "Nieuwe Map" and refresh the Treeview I
want to edit the name in the treeview like
you can do with Windows Explorer. When the folder is added it is
highlighted, the cursor is in the name, you edit and click somewhere
to save the new name.

Marco

Ok there is a LabelEdit property for the TreeView, set it to true to allow
the behaviour you are describing.  In order to catch the changes you can use
the BeforeLabelEdit, AfterLabelEdit events of the TreeView.  This wouldmean
that you set the LabelEdit property to true and if you want to cancel the
edit use the BeforeLabelEdit and set the CancelEdit property of the
NodeLabelEditEventArgs to true to cancel.  To get the text the user typed in
use the AfterLabelEdit event and pull the Label property from the
NodeLabelEditEventArgs .

LS

LS

Lloyd you were right, the ID changes.

First I get the ID from the basic form:

Property MyPropertyMydata() As Integer
Get
Return iExtension
End Get
Set(ByVal Value As Integer)
iExtension = Value
End Set
End Property
The result is then 23.
Then I load the record in the form:

Private Sub frmEdit_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
LoadDocumentDetails(iExtension)
End Sub

I change the fields and click "Save":
Meanwhile mysteriously the ID has become 4 and I don't know how:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click
SaveDocumentDetails(iExtension)
End Sub

Any thoughts?

Marco
 
What I mean is following:
when I add the new node called "Nieuwe Map" and refresh the Treeview I
want to edit the name in the treeview like
you can do with Windows Explorer. When the folder is added it is
highlighted, the cursor is in the name, you edit and click somewhere
to save the new name.

Marco

Ok there is a LabelEdit property for the TreeView, set it to true to allow
the behaviour you are describing.  In order to catch the changes you can use
the BeforeLabelEdit, AfterLabelEdit events of the TreeView.  This wouldmean
that you set the LabelEdit property to true and if you want to cancel the
edit use the BeforeLabelEdit and set the CancelEdit property of the
NodeLabelEditEventArgs to true to cancel.  To get the text the user typed in
use the AfterLabelEdit event and pull the Label property from the
NodeLabelEditEventArgs .

LS

LS

Wow sounds great but it's going too fast for me.
Could you give me some short examples please?

Marco
 
What I mean is following:
when I add the new node called "Nieuwe Map" and refresh the Treeview I
want to edit the name in the treeview like
you can do with Windows Explorer. When the folder is added it is
highlighted, the cursor is in the name, you edit and click somewhere
to save the new name.

Marco

Ok there is a LabelEdit property for the TreeView, set it to true to allow
the behaviour you are describing. In order to catch the changes you can
use
the BeforeLabelEdit, AfterLabelEdit events of the TreeView. This would
mean
that you set the LabelEdit property to true and if you want to cancel the
edit use the BeforeLabelEdit and set the CancelEdit property of the
NodeLabelEditEventArgs to true to cancel. To get the text the user typed
in
use the AfterLabelEdit event and pull the Label property from the
NodeLabelEditEventArgs .

LS

LS

Lloyd you were right, the ID changes.

First I get the ID from the basic form:

Property MyPropertyMydata() As Integer
Get
Return iExtension
End Get
Set(ByVal Value As Integer)
iExtension = Value
End Set
End Property
The result is then 23.
Then I load the record in the form:

Private Sub frmEdit_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
LoadDocumentDetails(iExtension)
End Sub

I change the fields and click "Save":
Meanwhile mysteriously the ID has become 4 and I don't know how:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click
SaveDocumentDetails(iExtension)
End Sub

Any thoughts?

Marco

Without seeing your code for SaveDocumentDetails that is difficult. I am
not sure what you mean when you refer to ID.

LS
 
What I mean is following:
when I add the new node called "Nieuwe Map" and refresh the Treeview I
want to edit the name in the treeview like
you can do with Windows Explorer. When the folder is added it is
highlighted, the cursor is in the name, you edit and click somewhere
to save the new name.

Marco

Ok there is a LabelEdit property for the TreeView, set it to true to allow
the behaviour you are describing. In order to catch the changes you can
use
the BeforeLabelEdit, AfterLabelEdit events of the TreeView. This would
mean
that you set the LabelEdit property to true and if you want to cancel the
edit use the BeforeLabelEdit and set the CancelEdit property of the
NodeLabelEditEventArgs to true to cancel. To get the text the user typed
in
use the AfterLabelEdit event and pull the Label property from the
NodeLabelEditEventArgs .

LS

LS

Wow sounds great but it's going too fast for me.
Could you give me some short examples please?

Marco

Marco,
Don't want to sound like a teacher but ... You haven't said what your
level of experience is but I would have to think not too much (and that is
not a problem). I would suggest you go to the following page:


http://msdn.microsoft.com/en-us/vbasic/default.aspx


This will give you lots of links to information about both dot.net and Vb.

LS
 
Wow sounds great but it's going too fast for me.
Could you give me some short examples please?

Marco

Marco,
    Don't want to sound like a teacher but ... You haven't said what your
level of experience is but I would have to think not too much (and that is
not a problem).  I would suggest you go to the following page:

http://msdn.microsoft.com/en-us/vbasic/default.aspx

This will give you lots of links to information about both dot.net and Vb..

LS

LS

I made an error somewhere else which made the ID turn into something
else.
I found it out and it is working now.
But you are right I'm new at dotnet. I used to work with VBA en VB6.
This is my first project in vb.net.

Marco
 
Back
Top