用代码将数据填充到树控件中.txt

时间:2023-03-23 19:22:22
【文件属性】:

文件名称:用代码将数据填充到树控件中.txt

文件大小:7KB

文件格式:TXT

更新时间:2023-03-23 19:22:22

vb

Private Sub Command1_Click() '* ----------------------------------------------------------------- '*用代码将数据填充到树控件中 '* ----------------------------------------------------------------- Dim Nodeindex As Node '*------------------------------------------------------------------ '*解释:定义Node '*Node是树控件的对象 '*每个Node都有三个东西,图标,文本,索引值 '*图标和文本都是实际显示出来的,索引值是隐含的 '*------------------------------------------------------------------ '设置最*的“爷”: '* --------------------------- '* ----Set Nodeindex = TreeView.Nodes.Add(, , "爷", "鄯善县") '* ----Nodeindex.Sorted = True '*------------------------------------------------------------------ '设置第二级“父” '* --------------------------- Set Nodeindex = TreeView.Nodes.Add("爷", tvwChild, "父1", "吐峪沟乡") Nodeindex.Sorted = True Set Nodeindex = TreeView.Nodes.Add("爷", tvwChild, "父2", "鲁克沁镇") Nodeindex.Sorted = True '*------------------------------------------------------------------ '*第一个参数“爷”是指这一层对应上层“爷”的 '*tvwChild参数是规定格式,指相对来说,这一层是爷的子层 '*“父1”是索引值,因为“父”有2个,而索引值是唯一的,所以要编号,用“父1”“父2”分开 '*“苹果”“葡萄”是要显示的文本,K1是显示图标的索引值 '*现在知道为什么在“爷”层设置时,第一,第二个参数是空的,因为这是最顶层 '*------------------------------------------------------------------ '设置第三级“子” '* --------------------------- Set Nodeindex = TreeView.Nodes.Add("父1", tvwChild, "子1", "吐峪沟村") Nodeindex.Sorted = True Set Nodeindex = TreeView.Nodes.Add("父1", tvwChild, "子2", "苏巴什村") Nodeindex.Sorted = True Set Nodeindex = TreeView.Nodes.Add("父2", tvwChild, "子3", "三个桥村") Nodeindex.Sorted = True Set Nodeindex = TreeView.Nodes.Add("父2", tvwChild, "子4", "木卡姆村") Nodeindex.Sorted = True Set Nodeindex = TreeView.Nodes.Add("父2", tvwChild, "子5", "木卡姆村") Nodeindex.Sorted = True Set Nodeindex = TreeView.Nodes.Add("父11", tvwChild, "子6", "新楼兰社区") Nodeindex.Sorted = True Set Nodeindex = TreeView.Nodes.Add("父11", tvwChild, "子7", "巴扎村") Nodeindex.Sorted = True Set Nodeindex = TreeView.Nodes.Add("父19", tvwChild, "子8", "英夏村") Nodeindex.Sorted = True Set Nodeindex = TreeView.Nodes.Add("父19", tvwChild, "子9", "阿凡提社区") Nodeindex.Sorted = True '*------------------------------------------------------------------ '*第一个参数“父1,2”是指这一层对应上层“父”的,但要注意对应的是“父1”还是“父2” '*tvwChild参数是规定格式,指相对来说,这一层是父的子层 '*“子1”是索引值,因为“子”有4个,而索引值是唯一的,所以要编号,用“1,2,3,4”分开 '*“红富士”等是要显示的文本,K1是显示图标的索引值 '*------------------------------------------------------------------ End Sub Private Sub Command2_Click() '* ----------------------------------------------------------------- '* 用数据库表(查询也一样)中数据填充树控件 '乡镇:大区ID,大区名称 '省份表:省份ID,省份名称,所属大区 '客户表:客户ID,客户名称,所属省份 '* ----------------------------------------------------------------- Dim Rec As New ADODB.Recordset Dim stRecQL As String Dim Item As Integer Dim i As Integer Dim nodindex As Node '* ----------------------------------------------------------------- '* 定义各类 '* ----------------------------------------------------------------- '设置最*的"爷" '* --------------------------- Set nodindex = TreeView.Nodes.Add(, , "鄯善县", "鄯善县") nodindex.Sorted = True '* ----------------------------------------------------------------- '*这里的设置跟第一小时里基本是一样的 '*但最后多了一个"K2"的参数,"K1"代表的是未被选中时的图标,"K2"代表是被选中后的图标 '*仔细观察一下,你会发现选中和没选中的图标是不一样的,一个是一个文件夹,一个是一个打开的文件夹 '* ----------------------------------------------------------------- '设置第二级"父" '* --------------------------- Rec.Open "select * from 乡镇 ", myconn, adOpenKeyset, adLockOptimistic, 1 Set Nodeindex = TreeView.Nodes.Add("鄯善县", tvwChild, "父1", "吐峪沟乡") '*Rec.Open "乡镇", CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdTableDirect For i = 0 To Rec.RecordCount - 1 Set nodindex = TreeView.Nodes.Add("鄯善县", tvwChild, Rec.Fields("乡镇ID"), Rec.Fields("户籍所在乡镇")) nodindex.Sorted = True Rec.MoveNext Next Rec.Close nodindex.Sorted = False '* ----------------------------------------------------------------- '*第一行意思是打开一个表去寻找数据(查询也是可以的) '*关键在与Add参数的变化 '*大家看第三个参数,在第一小时里,这里是"父1",这里用Rec.Fields("大区ID")来代替"1",意思是用表的编号来代替手工编号 '*第四个参数也是一样,直接用表中的名称字段来取代原来我们手工的命名 '* ----------------------------------------------------------------- '设置第三级"子" '* --------------------------- ' On Error Resume Next '* -----Rec.Open "省份表", CurrentProject.Connection, adOpenKeyset, adLockOptimistic, adCmdTableDirect Rec.Open "select * from 乡镇社区 ", myconn, adOpenKeyset, adLockOptimistic, 1 For i = 0 To Rec.RecordCount - 1 ' Set nodindex = TreeView.Nodes.Add("父" & Rec.Fields("户籍所在乡镇"), tvwChild, "子" & Rec.Fields("


网友评论