If TreeView1.SelectedItem.Parent Is Null Then
没有
else
有
没有
else
有
解决方案 »
- 如果vb中exe窗口上的X(就是希望手动不能关的)
- 编译后的程序,app.hInstance 返回的值为什么和在vb设计环境下返回的值相同?
- 【问】如何给每个VB新创建的Form,添加上一段VB代码?
- 关于VB的WINSOCK,从客户端发送数据到服务端断点续传的问题,请诸位高手指点迷津!
- 一种很新颖的for循环,没见过,高手给讲讲
- 跪求花生壳远程连接sql2000的方法,谢谢!
- 如何在安装程序的时候把程序需要的SQl数据库自动加到SQl Server里
- 关于VB的优化问题
- 这个版面怎么没有庆祝生蛋快乐呢?呵呵大家生蛋快乐哟!
- 怎样能够最简单的执行写好的SQL语句?
- 谁知道哪里有播放多媒体视 频文件比较好的免费控件
- 如何用API截取其它程序的消息?救我吧,很简单的:)
这个例子为 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
明白?
If TreeView1.SelectedItem.Parent Is Nothing Then -----这里是Nothing不是Null
没有
else
有
end if
还是不行,当节点没有父节点时,用treeview1.selectitem.parent已经错误了!你可以自己试试!
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
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