using treeview vb.net adding, deleting, editting nodes

Hello experts, I have treeview on my winform that gets built from an xml file.  I want the user to be able to add, delete and edit nodes.  Basically the treeview is a hierarchy of websites.  I have two textfields one contains the name of the website the user wishes to add, edit or delete.  The other contains the url of the website.  I want the tag property to contain the url and I am having problems assigning it.  Also how do I set the selectednode.index = -1 so that the user can add a new branch?  Currently I use the catch to determine if there isnt a node selected.   Here is my code

  Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
        editnodes("Delete")
    End Sub

    Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
        editnodes("Edit")
    End Sub

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        editnodes("Add")
    End Sub

    Function editnodes(ByVal stringval As String)
        Dim i As Integer
        If txtname.Text.Length = 0 Then
            MessageBox.Show("Please Enter valid name")
            Exit Function
        Else
            If txturl.Text.Substring(0, 4) <> "http" Then
                txturl.Text = "none"
            End If
        End If
        Try
            i = tvmainmenu.SelectedNode.Index
            Select Case stringval
                Case "Add"
                    tvmainmenu.SelectedNode.Nodes.Add(txtname.Text)
                    tvmainmenu.SelectedNode.Nodes(i).Tag = txturl.Text
                Case "Edit"
                    tvmainmenu.SelectedNode.Nodes(i).Text = txtname.Text
                    tvmainmenu.SelectedNode.Nodes(i).Tag = txturl.Text
                Case "Delete"
                    tvmainmenu.SelectedNode.Remove()
            End Select
        Catch ex As Exception
            'There is not a node selected so add new node
            tvmainmenu.Nodes.Add(txtname.Text)
            tvmainmenu.Nodes(txtname.Text).Tag = txturl.Text
        End Try
    End Function
tentavariousAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
I think this is what you are trying to do...

    Function editnodes(ByVal stringval As String)
        If txtname.Text.Length = 0 Then
            MessageBox.Show("Please Enter valid name")
            Exit Function
        Else
            If txturl.Text.Substring(0, 4) <> "http" Then
                txturl.Text = "none"
            End If
        End If

        Dim curNode As TreeNode = tvmainmenu.SelectedNode
        Select Case stringval
            Case "Add"
                Dim tn As New TreeNode(txtname.Text)
                tn.Tag = txturl.Text
                If Not IsNothing(curNode) Then
                    curNode.Nodes.Add(tn)
                Else
                    tvmainmenu.Nodes.Add(tn)
                End If

            Case "Edit"
                If Not IsNothing(curNode) Then
                    curNode.Text = txtname.Text
                    curNode.Tag = txturl.Text
                End If

            Case "Delete"
                If Not IsNothing(curNode) Then
                    tvmainmenu.Nodes.Remove(curNode)
                End If

        End Select
    End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tentavariousAuthor Commented:
Looks pretty good, one more question how do I set selected index to nothing so that I can add a new branch?  I added new button that I want to reset the textfields back to the default so a new branch can be added.  My code doesnt work

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '  tvmainmenu.SelectedNode.Nodes.Add("Test this page")
        Try
            Dim curNode As TreeNode = tvmainmenu.SelectedNode
            curNode = Nothing  'set the selected index to nothing
            txtname.Text = ""
            txturl.Text = "http://"
            Me.gbhyperlink.Visible = True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try...

    tvmainmenu.SelectedNode = Nothing
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.