If no selected nodes in TreeView control
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: If no selected nodes in TreeView control

Hybrid View

  1. #1
    Join Date
    Aug 2002
    Location
    Armenia
    Posts
    62

    If no selected nodes in TreeView control

    Hi...
    When I refere to selected Node in TreeView control, when no selected nodes are, error occurs.. How can I check, is there any selected Node in my TreeView control?
    Thank you very much.

  2. #2
    Join Date
    Sep 2002
    Location
    Greece
    Posts
    60

    Answer

    Hello there...

    Well, here is a way to find out if you have any selected nodes in your tree.

    For i = 1 To TreeView1.Nodes.Count

    If TreeView1.Nodes.Item(i).Selected = True Then

    ...hold the i somewhere and you have the selected node...

    End If

    Next


    Hope it helped

    vvang10

  3. #3
    Join Date
    Aug 2002
    Location
    Armenia
    Posts
    62
    Ok, thanks a lot...
    But probably there must be an easiest way? Probably using some standart method, or comparing like
    if TreeControl.Nodes.SelectedItem = Nothing ..... ?????

  4. #4
    Join Date
    Sep 2002
    Location
    Greece
    Posts
    60

    Treeview

    I think that was the easiest way to find out if you have selected nodes in Treeview.

  5. #5
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Treeview SelectedItem

    There is an easier way, browsing for each nodes in a treeview can be a pain if you have a lot of nodes (like it is often the case with treeview), try that code:

    Code:
    Private Sub Command1_Click()
        Dim objNode As Node
        
        'Get the selected node
        Set objNode = Me.TreeView1.SelectedItem
        
        'Remove the selection to test
        Set objNode = Nothing
        
        'Test if selection
        If objNode Is Nothing Then
            MsgBox "No selection"
        Else
            MsgBox "Selection: " & objNode.Text
        End If
        
    End Sub
    Remove that line afterward
    Set objNode = Nothing


    JeffB - hope it helps
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

  6. #6
    Join Date
    Mar 2013
    Posts
    1

    Re: Treeview SelectedItem

    Quote Originally Posted by JeffB View Post
    There is an easier way, browsing for each nodes in a treeview can be a pain if you have a lot of nodes (like it is often the case with treeview), try that code:

    Code:
    Private Sub Command1_Click()
        Dim objNode As Node
        
        'Get the selected node
        Set objNode = Me.TreeView1.SelectedItem
        
        'Remove the selection to test
        Set objNode = Nothing
        
        'Test if selection
        If objNode Is Nothing Then
            MsgBox "No selection"
        Else
            MsgBox "Selection: " & objNode.Text
        End If
        
    End Sub
    Remove that line afterward
    Set objNode = Nothing


    JeffB - hope it helps
    NO Me.TreeView1.SelectedItem in Visual Studio 2010.

    is there any other way?

  7. #7
    Join Date
    Aug 2002
    Location
    Armenia
    Posts
    62
    Ok! thanks a lot!

  8. #8
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: If no selected nodes in TreeView control

    This is the VB6 forum, and VS2010 is supported in the .Net forum...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center