• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2261
  • Last Modified:

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
0
tentavarious
Asked:
tentavarious
  • 2
1 Solution
 
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
 
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

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now