treeview 拖拽 节点 实现目录的更改

莫凡 2010-01-21 09:18:27
目录如下:

A_奥迪
|____A4L
B_宝马
|____BMW232
|____BMW525


现在如果想通过拖拽treeview节点实现以下结果,希望不吝赐教。

A_奥迪
|____A4L
|____BMW525
B_宝马
|____BMW232

...全文
231 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
莫凡 2010-01-30
  • 打赏
  • 举报
回复
多谢大家,正在解决中。。。
guyehanxinlei 2010-01-27
  • 打赏
  • 举报
回复
收藏一下,顺便帮顶
AddDark 2010-01-26
  • 打赏
  • 举报
回复
帮顶,还真没遇到过这种问题
倒大霉的上帝 2010-01-26
  • 打赏
  • 举报
回复
新建窗体,放置一个Treeview(默认名Treeview1)拷贝下面代码:

Option Explicit
Private mvarSelectKey As String
Private Sub Form_Load()

TreeView1.Indentation = 300 '设置缩进
TreeView1.LineStyle = tvwRootLines '设置显示方式
TreeView1.LabelEdit = tvwManual '不可编辑

TreeView1.Nodes.Add , , "A_aoDi", "A_奥迪" '根节点
TreeView1.Nodes.Add "A_aoDi", tvwChild, "A4L", "A4L" '子节点

TreeView1.Nodes.Add , , "B_baoMa", "B_宝马" '另一个根节点
TreeView1.Nodes.Add "B_baoMa", tvwChild, "B232", "BMW232" ''''
TreeView1.Nodes.Add "B_baoMa", tvwChild, "B525", "BMW525" '''''

Dim msNode As MSComctlLib.Node ''''

For Each msNode In TreeView1.Nodes '展开
msNode.Expanded = True
Next
End Sub

'拖动结束事件
Private Sub TreeView1_DragDrop(Source As Control, x As Single, y As Single)
Dim strHitKey As String
Dim strName As String

strHitKey = TreeView1.HitTest(x, y).Key
strName = TreeView1.Nodes(mvarSelectKey).Text

TreeView1.Nodes.Remove (mvarSelectKey)
TreeView1.Nodes.Add strHitKey, tvwChild, mvarSelectKey, strName

End Sub

'拖动过程事件
Private Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, State As Integer)

Set TreeView1.DropHighlight = TreeView1.HitTest(x, y)
End Sub

'鼠标按下事件
Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim strKey As String
If Button = vbLeftButton Then
If Not TreeView1.SelectedItem Is Nothing Then

strKey = TreeView1.SelectedItem.Key

If TreeView1.Nodes(strKey).Children = 0 Then
mvarSelectKey = strKey
TreeView1.DragIcon = TreeView1.SelectedItem.CreateDragImage
TreeView1.Drag vbBeginDrag
End If
End If
End If
End Sub

'鼠标移动事件
Private Sub TreeView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim strKey As String

If Not TreeView1.HitTest(x, y) Is Nothing Then
TreeView1.SelectedItem = TreeView1.HitTest(x, y)
End If

End Sub
Tiger_Zhao 2010-01-25
  • 打赏
  • 举报
回复
http://www.vb-helper.com/howto_treeview_add_node.html
孤独剑_LPZ 2010-01-24
  • 打赏
  • 举报
回复
怎么样了,lz的问题解决了吗
fudonghaiaaaa 2010-01-23
  • 打赏
  • 举报
回复
Option Explicit

Dim mnIndex As Integer ' Holds the index of a Node
Dim mbIndrag As Boolean ' Flag that signals a Drag Drop operation.
Dim moDragNode As Object ' Item that is being dragged.


Private Sub cmdLast_Click()
'Add a node using tvwLast
Dim skey As String
skey = GetNextKey()
On Error GoTo myerr 'if the treeview does not have a node selected
' the next line of code will return an error number 91
TreeView1.Nodes.Add TreeView1.SelectedItem.Index, tvwLast, skey, "Last " & skey, 1, 2
Exit Sub
myerr:
'add a root node in the last postion because no other node is selected.
TreeView1.Nodes.Add , tvwLast, skey, "Last " & skey, 1, 2
Exit Sub
End Sub
Private Sub cmdChild_Click()
'Add a node using tvwChild
Dim oNodex As Node
Dim skey As String
Dim iindex As Integer

On Error GoTo myerr 'if the treeview does not have a node selected
' the next line of code will return an error number 91
iindex = TreeView1.SelectedItem.Index 'Check to see if a Node is selected
skey = GetNextKey() ' Get a key for the new Node
Set oNodex = TreeView1.Nodes.Add(iindex, tvwChild, skey, "Child " & skey, 1, 2)
oNodex.EnsureVisible 'make sure the child node is visible
Exit Sub
myerr:
'Display a messge telling the user to select a node
MsgBox ("You must select a Node to do an Add Child" & vbCrLf _
& "If the TreeView is empty us Add Last to create the first node")
Exit Sub
End Sub
Private Sub Form_Load()
'Add some Nodes so the TreeView is not empty

Dim I As Integer
'在 ImageList 控件中添加一个图象。
Dim oNodex As Object
Dim imgX As ListImage
Dim iindex As Integer
' TreeView1.ImageList = ImageList1 '初始化ImageList。

Set imgX = ImageList1.ListImages.Add(, , LoadPicture("C:\Program Files\Microsoft Visual Studio\Common\Tools\APE\Source\Aeservic\SERVICE.ico"))

TreeView1.ImageList = ImageList1 '初始化ImageList。


TreeView1.LineStyle = tvwRootLines

TreeView1.Style = tvwTreelinesPlusMinusPictureText

Dim DriverCount As Integer

Dim GivePath As String

Set moDragNode = Nothing
cmdLast_Click
cmdLast_Click
TreeView1.Nodes(1).Selected = True
cmdChild_Click
End Sub
Private Function GetNextKey() As String
'Returns a new key value for each Node being added to the TreeView
'This algorithm is very simple and will limit you to adding a total of 999 nodes
'Each node needs a unique key. If you allow users to remove Nodes you can't use
'the Nodes count +1 as the key for a new node.

Dim sNewKey As String
Dim iHold As Integer
Dim I As Integer
On Error GoTo myerr
'The next line will return error #35600 if there are no Nodes in the TreeView
iHold = Val(TreeView1.Nodes(1).Key)
For I = 1 To TreeView1.Nodes.Count
If Val(TreeView1.Nodes(I).Key) > iHold Then
iHold = Val(TreeView1.Nodes(I).Key)
End If
Next
iHold = iHold + 1
sNewKey = CStr(iHold) & "_"
GetNextKey = sNewKey 'Return a unique key
Exit Function
myerr:
'Because the TreeView is empty return a 1 for the key of the first Node
GetNextKey = "1_"
Exit Function
End Function



Private Sub TreeView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = vbLeftButton Then ' Signal a Drag operation.
mbIndrag = True ' Set the flag to true.
' Set the drag icon with the CreateDragImage method.
TreeView1.DragIcon = TreeView1.SelectedItem.CreateDragImage
TreeView1.Drag vbBeginDrag ' Drag operation.
End If

End Sub

Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Set TreeView1.DropHighlight = TreeView1.HitTest(x, y)
'Make sure we are over a Node
If Not TreeView1.DropHighlight Is Nothing Then
'Set the Node we are on to be the selected Node
'if we don't do this it will not be the selected node
'until we finish clicking on the Node
TreeView1.SelectedItem = TreeView1.HitTest(x, y)
Set moDragNode = TreeView1.SelectedItem ' Set the item being dragged.
End If
Set TreeView1.DropHighlight = Nothing
End Sub
Private Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
If mbIndrag = True Then
' Set DropHighlight to the mouse's coordinates.
Set TreeView1.DropHighlight = TreeView1.HitTest(x, y)
End If
End Sub
Private Sub TreeView1_DragDrop(Source As Control, x As Single, y As Single)
' If user didn't move mouse or released it over an invalid area.

If TreeView1.DropHighlight Is Nothing Then
mbIndrag = False
Exit Sub
Else
' Set dragged node's parent property to the target node.
On Error GoTo checkerror ' To prevent circular errors.
Set moDragNode.Parent = TreeView1.DropHighlight
Cls
Print TreeView1.DropHighlight.Text & _
" is parent of " & moDragNode.Text
' Release the DropHighlight reference.
Set TreeView1.DropHighlight = Nothing
mbIndrag = False
Set moDragNode = Nothing
Exit Sub ' Exit if no errors occured.
End If

checkerror:
' Define constants to represent Visual Basic errors code.
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."

' Display the message box with an exclamation mark icon
' and with OK and Cancel buttons.
If MsgBox(msg, vbExclamation & vbOKCancel) = vbOK Then
' Release the DropHighlight reference.
mbIndrag = False
Set TreeView1.DropHighlight = Nothing
Exit Sub
End If
End If


End Sub
莫凡 2010-01-23
  • 打赏
  • 举报
回复
有没有具体一点的。
  • 打赏
  • 举报
回复
DragDrop事件。
Set DragNode.Parent = .DropHighlight
SYSSZ 2010-01-21
  • 打赏
  • 举报
回复
从哪向哪拖?
莫凡 2010-01-21
  • 打赏
  • 举报
回复
有人帮忙吗。
代码下载地址: https://pan.quark.cn/s/a4b39357ea24 ### HSMS/SEMI通讯E37说明书中文关键知识点 #### 一、HSMS概述 - **HSMS**(High-Speed SECS Message Service)是一种专为半导体工厂内部计算机之间进行高速消息传输而构建的通信标准。该标准旨在逐步取代原有的SEMIE4(SECS-I)和SEMIE13(SECS Message Service),特别是在需要高速数据交换或简单的点对点拓扑结构无法满足实际需求的情况下。 - HSMS的核心宗旨在于提供一种机制,使来自不同制造商的设备能够达成无缝对接和互操作状态,即便这些设备在设计理念和通信机制上存在显著差异。 - HSMS不仅适用于TCP/IP优先于OSI模型的应用环境,同时也能作为SEMIE4和SEMIE13的有效替代方案。 #### 二、HSMS的应用范围 - HSMS主要部署于半导体制造业中的计算机系统之间进行消息交换的场景,如自动化生产线、设备监控系统等。 - 该标准界定了通信接口,使得半导体工厂内的各类设备能够实现高效且可靠的数据传输。 #### 三、HSMS与其他标准的关系 - **SEMIE4 (SECS-I)**:规定了消息传输的基本规则和格式。 - **SEMIE5 (SECS-II)**:明确了消息的具体内容和结构。 - **IETF Documents**: - RFC791 - Internet协议 - RFC792 - Internet控制消息协议 - RFC793 - 传输控制协议 - RFC1120 - Internet主机通信层的要求 - RFC1340 - 指定号码 - **POSIX D...

7,787

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧