If TreeView1.SelectedItem.Parent Is Null Then 
  没有
else
  有

解决方案 »

  1.   

    你还中多看看MSDN中关于TREEVIEW的内容吧!你问的问题太....
      

  2.   

    Parent 属性(Node 对象)示例
    这个例子为 TreeView 控件添加几个 Node 对象。在选择了 Node 对象后,可以单击并拖动它到任何其它 Node,以使它成为该目标 Node 的子节点。为试用此例,要在窗体上安放 TreeView 和 ImageList 控件,并在该窗体的声明部分粘贴该代码。运行此例,并将一些 Node 对象拖动到其它 Node 对象之上来观察结果。'声明全局变量。
    Dim indrag As Boolean '指示拖放操作的标志。
    Dim nodX As Object '要拖动的项。Private Sub Form_Load()
       '在 Imagelist 控件中加载一个位图。
       Dim imgX As ListImage
       Dim BitmapPath As String
       BitmapPath = "icons\mail\mail01a.ico"
       Set imgX = ImageList1.ListImages.Add(, , LoadPicture(BitmapPath))
       
       '初始化 TreeView 控件并创建几个节点。
       TreeView1.ImageList = ImageList1
       Dim nodX As Node      '创建树。
       Set nodX = TreeView1.Nodes.Add(, , , "Parent1", 1)
       Set nodX = TreeView1.Nodes.Add(, , , "Parent2", 1)
       Set nodX = TreeView1.Nodes.Add(1, tvwChild, , "Child 1", 1)
       Set nodX = TreeView1.Nodes.Add(1, tvwChild, , "Child 2", 1)
       Set nodX = TreeView1.Nodes.Add(2, tvwChild, , "Child 3", 1)
       Set nodX = TreeView1.Nodes.Add(2, tvwChild, , "Child 4", 1)
       Set nodX = TreeView1.Nodes.Add(3, tvwChild, , "Child 5", 1)
       nodX.EnsureVisible '展开树,显示全部节点。
    End SubPrivate Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
       Set nodX = TreeView1.SelectedItem '设置要拖动的项。
       Set TreeView1.DropHighlight = Nothing
    End SubPrivate Sub TreeView1_MouseMove _
    (Button As Integer, Shift As Integer, x As Single, y As Single)
       If Button = vbLeftButton Then '指示拖动操作。
          indrag = True '设置标志为 true。
          '用 CreateDragImage 方法设置拖动图标。
          TreeView1.DragIcon = TreeView1.SelectedItem.CreateDragImage
          TreeView1.Drag vbBeginDrag '拖动操作。
       End If
    End SubPrivate Sub TreeView1_DragDrop(Source As Control, x As Single, y As Single)
       '如果用户没移动鼠标,或在无效区释放它。
       If TreeView1.DropHighlight Is Nothing Then
          indrag = False
          Exit Sub
       Else
          '设置被拖动的节点的 parent 属性为目标节点。
          On Error GoTo checkerror '阻止循环错误。
          Set nodX.Parent = TreeView1.DropHighlight
          Cls
          Print TreeView1.DropHighlight.Text & _
          " is parent of " & nodX.Text
          '释放 DropHighlight 引用。
          Set TreeView1.DropHighlight = Nothing
          indrag = False
          Exit Sub '如未发生错误则退出。
       End If
     
    checkerror:
       '定义表示 Visual Basic 错误代码的常数。
       Const CircularError = 35614
       If Err.Number = CircularError Then
          Dim msg As String
          msg = "A node can't be made a child of its own children."
          '显示带有一个感叹号图标
          '和“确定”与“取消”按钮的消息框。
          If MsgBox(msg, vbExclamation & vbOKCancel) = vbOK Then
             '释放 DropHighlight 引用。
             indrag = False
             Set TreeView1.DropHighlight = Nothing
             Exit Sub
          End If
       End If
    End SubPrivate Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
       Set TreeView1.DropHighlight = TreeView1.HitTest(x, y)
    End Sub
      

  3.   

    若其index为1,则为root.
    明白?
      

  4.   

    to sindia(乖乖兔):这是什么函数,msdn都搜不出来,api?还有注意parent跟root的区别,root是根节点,parent父节点to tommychim(大脚鸟) 在回答别人问题之前,要先有把握方法行得通,又浪费我宝贵得调试时间,你的方法有问题to cqq_chen(我是谁) 我的问题是很菜呀,怎么了,你看看前面的哪个答出来了,认为自己厉害就回答三。to myxiaopei(天神)  这段msdn的东东跟我的问题一点不着边to ayusay(阿于) 老大,你比我还菜耶,有parent不意味着是根结点,根结点的index才是1,不是root,是parent,明白?拜托高手们,不要耍我啦,既然问题这么简单,倒是回答三,我很急耶!
      

  5.   

    看来看去还是我的错误捕捉的方法行的通,捕捉91号错误,然后返回没有parent!还有没有人有别的办法
      

  6.   

    呵呵,是有问题,不过问题不大,有点错误而已
    If TreeView1.SelectedItem.Parent Is Nothing Then -----这里是Nothing不是Null
      没有
    else
      有 
    end if
      

  7.   

    To tommychim(阿布洛迪) 
    还是不行,当节点没有父节点时,用treeview1.selectitem.parent已经错误了!你可以自己试试!
      

  8.   

    下面是我的程序,在FORM里放一个treeview
    Private Sub Form_Load()
        With TreeView1
            .Nodes.Add , , "P1", "P1"
            .Nodes.Add , , "P2", "P2"
            .Nodes.Add "P1", tvwChild, "P11", "P11"
            .Nodes.Add "P1", tvwChild, "P12", "P12"
            .Nodes.Add "P11", tvwChild, "P111", "P111"
            .Nodes.Add "P11", tvwChild, "P112", "P112"
            .Nodes.Add "P2", tvwChild, "P21", "P21"
            .Nodes.Add "P2", tvwChild, "P22", "P22"
        End With
    End SubPrivate Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
        If Node.Parent Is Nothing Then
            MsgBox "没有父节点!", vbOKOnly + vbExclamation, "提示"
        Else
            MsgBox "有父节点!", vbOKOnly + vbExclamation, "提示"
        End If
    End Sub
      

  9.   

    tommychim(阿布洛迪) 的办法是正确的。Private Sub Form_Load()
    With TreeView1.Nodes
        .Add , , "A1", "a111111111111111"
        .Add "A1", tvwChild, "A1-1", "a111111111111111-1"
        .Add "A1", tvwChild, "A1-2", "a111111111111111-2"
        .Add "A1", tvwChild, "A1-3", "a111111111111111-3"
        .Add "A1-2", tvwChild, "A1-2-1", "a111111111111111-2-1"
        .Add "A1-2", tvwChild, "A1-2-2", "a111111111111111-2-2"
    End With
    End SubPrivate Sub TreeView1_Click()
        If TreeView1.SelectedItem.Parent Is Nothing Then MsgBox "ROOT"
    End Sub