分享
 
 
 

TreeView树形控件与数据库结合编程!

王朝other·作者佚名  2007-10-16
窄屏简体版  字體: |||超大  

树形控件在大多数的系统中都会使用到。以其层次鲜明,操作简便的优点得到广大程序员以及使用人员的认可。不过,尽管树形控件操作比较简单,但是当与数据库结合的时候,操作会有一引起麻烦。

笔者将自己在实际应用过程中总结出来的代码编写成类,在以后使用的时候直接使用类就可以了。

作者:崔占民

EMAIL:CUIZM@163.COM

代码如下:

首先,选择菜单->工程->添加类模块,输入以下代码:

Option Explicit

Private m_TreeView As TreeView

Public Sub CreateTreeView(aTreeView As Object)

Set m_TreeView = aTreeView

End Sub

'添加数据到TREEVIEW控件

Public Sub AddTree(rs As Recordset, aID As String, aContext As String, aParentID As String)

Dim Xnod As Node

Do While Not rs.EOF

If rs.Fields(aParentID) = 0 Then

'加入根结点

Set Xnod = m_TreeView.Nodes.Add(, , "key" & rs.Fields(aID), rs.Fields(aContext), 2)

Else

'加入子节点

Set Xnod = m_TreeView.Nodes.Add("key" & rs.Fields(aParentID), tvwChild, "key" & rs.Fields(aID), rs.Fields(aContext), 1)

End If

Xnod.EnsureVisible

rs.MoveNext

Loop

End Sub

'取得所有子结点的关键字

Public Function GetSubNodeKey(aNode As Node) As String

Dim StrWhere As String

GetSubKey aNode, StrWhere

If Len(StrWhere) > 0 Then

GetSubNodeKey = "ID = " & Mid(aNode.Key, 4) & " OR " & Left(StrWhere, Len(StrWhere) - 4)

Else

GetSubNodeKey = "ID = " & Mid(aNode.Key, 4)

End If

End Function

Public Sub GetSubKey(aNode As Node, aStrWhere As String)

Dim NodeSub As Node

Set NodeSub = aNode.Child

While Not NodeSub Is Nothing

aStrWhere = aStrWhere & "ID = " & Mid(NodeSub.Key, 4) & " OR "

If NodeSub.Children > 0 Then GetSubKey NodeSub, aStrWhere

Set NodeSub = NodeSub.Next

Wend

End Sub

添加一窗口,为窗口添加一菜单,菜单项分别为:添加、修改、删除。菜单名分别为:mnuAdd、mnuModify、mnuDelete。

在窗口中添加一个TREEVIEW控件。

窗口代码如下:

Option Explicit

'工程--->引用--->Microsoft ActiveX Data Object 2.x Library(版本号)

Dim cn As ADODB.Connection

Dim m_bolAddFlag As Boolean

Dim m_strKey As String, m_strParentKey As String

Dim m_TreeOpt As New CTreeOpt

Private Sub Command1_Click()

Dim rs As New ADODB.Recordset

TreeView1.Nodes.Clear

rs.Open "SELECT * FROM tbTree", cn, adOpenDynamic, adLockReadOnly

m_TreeOpt.AddTree rs, "ID", "CONTEXT", "PARENTID"

rs.Close

Set rs = Nothing

End Sub

Private Sub Form_Load()

On Error GoTo Errhandle

Set cn = New ADODB.Connection

'连接数据库

cn.ConnectionString = "DBQ=" & App.Path & "\db1.mdb;DefaultDir=" & _

App.Path & ";Driver={Microsoft Access Driver (*.mdb)};" & _

"DriverId=25;FIL=MS Access;ImplicitCommitSync=Yes;" & _

"MaxBufferSize=512;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;" & _

"Threads=3;UID=ADMIN;UserCommitSync=Yes;PWD=admind1234;"

cn.Open

m_TreeOpt.CreateTreeView TreeView1

Command1.Value = True

Exit Sub

Errhandle:

MsgBox Err.Description, vbExclamation

End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

On Error Resume Next

cn.Close

Set cn = Nothing

Set m_TreeOpt = Nothing

End Sub

'添加结点

Private Sub mnuAdd_Click()

Dim rs As New ADODB.Recordset

m_bolAddFlag = True

If rs.State = adStateOpen Then rs.Close

rs.Open "SELECT IIF (ISNULL (MAX(ID)), 1, MAX(ID)) AS ID_M FROM tbTree", cn, adOpenStatic, adLockReadOnly

If rs.EOF Then

m_strKey = "1"

Else

m_strKey = CStr(rs!ID_M + 1)

End If

With TreeView1

m_strParentKey = .SelectedItem.Key

.Nodes.Add(m_strParentKey, tvwChild, "key" & m_strKey, "新加结点", 1).Selected = True

.StartLabelEdit

End With

rs.Close

Set rs = Nothing

End Sub

'删除结点

Private Sub mnuDelete_Click()

Dim StrWhere As String

With TreeView1

If .SelectedItem.Key = "key1" Then

MsgBox "对不起,不能删除根点!", vbExclamation

Exit Sub

End If

StrWhere = m_TreeOpt.GetSubNodeKey(.SelectedItem)

cn.Execute "DELETE FROM tbTree WHERE " & StrWhere

.Nodes.Remove .SelectedItem.Key

End With

End Sub

'修改结点

Private Sub mnuModify_Click()

m_bolAddFlag = False

With TreeView1

m_strKey = Mid(.SelectedItem.Key, 4)

.StartLabelEdit

End With

End Sub

Private Sub TreeView1_AfterLabelEdit(Cancel As Integer, NewString As String)

cn.Execute "UPDATE tbTree SET CONTEXT = '" & NewString & "' WHERE ID = " & m_strKey

End Sub

Private Sub TreeView1_BeforeLabelEdit(Cancel As Integer)

If m_bolAddFlag Then

Dim strSql As String

m_strParentKey = Mid(m_strParentKey, 4)

strSql = "INSERT INTO tbTree (ID, CONTEXT, PARENTID) VALUES (" & m_strKey & ", '新加结点', " & m_strParentKey & ")"

cn.Execute strSql

End If

End Sub

Private Sub TreeView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)

If Button = vbRightButton Then PopupMenu mnuPopup

End Sub

类里提供了将数据库中的数据显示在控件中的方法。删除结点及其下面所有子结点的方法。也可以将类做成DLL,在以后的应用中直接加载DLL就可以了。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有