树和自联表的详细介绍
Author:水如烟
三种情形示例
第一个示例,树情形。取MainForm主菜单的数据。ToolStripMenuItem本身是一个树。
随便在一个Form上加一些菜单。我的如图:
结果是:
代码:
PublicClassFormTree
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
DimmCollectionAsNewLzmTW.uSystem.uCollection.SinceLink.SinceLinkItemCollection(OfInteger,MenuItemInformation)
mCollection.AppendFromBlankCodeNode(GetMenuNode(Me))
Me.TreeView1.Nodes.Clear()
Me.TreeView1.Nodes.Add(mCollection.Node.ConvertToTreeNode("Text",True))
Me.DataGridView1.DataSource=mCollection.Node.ConvertToDataTable(True)
EndSub
PrivateFunctionGetMenuNode(ByValformAsForm)AsLzmTW.uSystem.uCollection.Node(OfMenuItemInformation)
Ifform.MainMenuStripIsNothingThenReturnNothing
DimmNodeAsNewLzmTW.uSystem.uCollection.Node(OfMenuItemInformation)(NewMenuItemInformation)
mNode.Item.Name="Root"
ForEachitemAsToolStripMenuItemInform.MainMenuStrip.Items
AppendNode(mNode,item)
Next
ReturnmNode
EndFunction
PrivateSubAppendNode(ByValnodeAsLzmTW.uSystem.uCollection.Node(OfMenuItemInformation),ByValmenuItemAsToolStripMenuItem)
DimmCurrentNodeAsLzmTW.uSystem.uCollection.Node(OfMenuItemInformation)=Nothing
mCurrentNode=node.Nodes.Add(NewMenuItemInformation(menuItem))
ForEachitemAsToolStripMenuItemInmenuItem.DropDownItems
AppendNode(mCurrentNode,item)
Next
EndSub
EndClass
PublicClassMenuItemInformation
InheritsLzmTW.uSystem.uCollection.SinceLink.SinceLinkItemBase(OfInteger)
PrivategTextAsString
PublicPropertyText()AsString
Get
ReturngText
EndGet
Set(ByValvalueAsString)
gText=value
EndSet
EndProperty
SubNew()
EndSub
SubNew(ByValmenuItemAsToolStripMenuItem)
WithmenuItem
Me.Name=.Name
Me.Text=.Text
EndWith
EndSub
EndClass
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
DimmCollectionAsNewLzmTW.uSystem.uCollection.SinceLink.SinceLinkItemCollection(OfInteger,MenuItemInformation)
mCollection.AppendFromBlankCodeNode(GetMenuNode(Me))
Me.TreeView1.Nodes.Clear()
Me.TreeView1.Nodes.Add(mCollection.Node.ConvertToTreeNode("Text",True))
Me.DataGridView1.DataSource=mCollection.Node.ConvertToDataTable(True)
EndSub
PrivateFunctionGetMenuNode(ByValformAsForm)AsLzmTW.uSystem.uCollection.Node(OfMenuItemInformation)
Ifform.MainMenuStripIsNothingThenReturnNothing
DimmNodeAsNewLzmTW.uSystem.uCollection.Node(OfMenuItemInformation)(NewMenuItemInformation)
mNode.Item.Name="Root"
ForEachitemAsToolStripMenuItemInform.MainMenuStrip.Items
AppendNode(mNode,item)
Next
ReturnmNode
EndFunction
PrivateSubAppendNode(ByValnodeAsLzmTW.uSystem.uCollection.Node(OfMenuItemInformation),ByValmenuItemAsToolStripMenuItem)
DimmCurrentNodeAsLzmTW.uSystem.uCollection.Node(OfMenuItemInformation)=Nothing
mCurrentNode=node.Nodes.Add(NewMenuItemInformation(menuItem))
ForEachitemAsToolStripMenuItemInmenuItem.DropDownItems
AppendNode(mCurrentNode,item)
Next
EndSub
EndClass
PublicClassMenuItemInformation
InheritsLzmTW.uSystem.uCollection.SinceLink.SinceLinkItemBase(OfInteger)
PrivategTextAsString
PublicPropertyText()AsString
Get
ReturngText
EndGet
Set(ByValvalueAsString)
gText=value
EndSet
EndProperty
SubNew()
EndSub
SubNew(ByValmenuItemAsToolStripMenuItem)
WithmenuItem
Me.Name=.Name
Me.Text=.Text
EndWith
EndSub
EndClass
第二个示例,(Code,Name)情形。仍以行政区划数据为例。
代码:
PublicClassForm1
PublicgRegionalDatasAsDataTable'这是列为Code,Name的行政区划数据表
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
DimmCollectionAsNewLzmTW.uSystem.uCollection.SinceLink.SinceLinkItemCollection(OfString,RegionalCodeItem)("00,00,00")
ForEachrowAsDataRowIngRegionalDatas.Rows
Withrow
mCollection.Add(.Item("Code").ToString,.Item("Name").ToString)
EndWith
Next
Me.TreeView1.Nodes.Clear()
Me.TreeView1.Nodes.Add(mCollection.Node.ConvertToTreeNode("Name",True))
Me.DataGridView1.DataSource=mCollection.Node.ConvertToDataTable(True)
EndSub
EndClass
PublicClassRegionalCodeItem
InheritsLzmTW.uSystem.uCollection.SinceLink.SinceLinkItemBase(OfString)
SubNew()
EndSub
SubNew(ByValcodeAsString,ByValnameAsString)
MyBase.New(code,name)
EndSub
EndClass
PublicgRegionalDatasAsDataTable'这是列为Code,Name的行政区划数据表
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
DimmCollectionAsNewLzmTW.uSystem.uCollection.SinceLink.SinceLinkItemCollection(OfString,RegionalCodeItem)("00,00,00")
ForEachrowAsDataRowIngRegionalDatas.Rows
Withrow
mCollection.Add(.Item("Code").ToString,.Item("Name").ToString)
EndWith
Next
Me.TreeView1.Nodes.Clear()
Me.TreeView1.Nodes.Add(mCollection.Node.ConvertToTreeNode("Name",True))
Me.DataGridView1.DataSource=mCollection.Node.ConvertToDataTable(True)
EndSub
EndClass
PublicClassRegionalCodeItem
InheritsLzmTW.uSystem.uCollection.SinceLink.SinceLinkItemBase(OfString)
SubNew()
EndSub
SubNew(ByValcodeAsString,ByValnameAsString)
MyBase.New(code,name)
EndSub
EndClass
效果:
第三个示例,自联表数据情形。这里以树和自联表(四) 上的数据为例。
要使用本示例代码,需把以上数据存为一个Excel文件,文件名为Menus.xls,数据簿工作表名称为Menus。存它到程序运行目录上。
首先把数据读进一个DataTable里面。
PrivateSubButton2_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton2.Click
DimcnAsNewOdbc.OdbcConnection("Driver={MicrosoftExcelDriver(*.xls)};DriverId=790;Dbq=menus.xls;HDR=Yes;")
DimcmAsNewOdbc.OdbcCommand("SELECT*FROM[Menus$]",cn)
DimadAsNewOdbc.OdbcDataAdapter(cm)
DimmdsAsNewDataSet("Menus")
ad.Fill(mds,"Menu")
mds.WriteXmlSchema("Menus.xsd")
mds.WriteXml("menus.xml")
Me.DataGridView1.DataSource=mds.Tables(0)
EndSub
DimcnAsNewOdbc.OdbcConnection("Driver={MicrosoftExcelDriver(*.xls)};DriverId=790;Dbq=menus.xls;HDR=Yes;")
DimcmAsNewOdbc.OdbcCommand("SELECT*FROM[Menus$]",cn)
DimadAsNewOdbc.OdbcDataAdapter(cm)
DimmdsAsNewDataSet("Menus")
ad.Fill(mds,"Menu")
mds.WriteXmlSchema("Menus.xsd")
mds.WriteXml("menus.xml")
Me.DataGridView1.DataSource=mds.Tables(0)
EndSub
由于我没有预建一个DataTable,所以上面代码运行后它自动的将Integer类型作为Double类型来处理。因此,需把它转为Intege类型。在此,我将数据存为xml形式,同时输出Schema文件。
之后,打开Menus.xsd文件,将所有Double类型改为Integer类型,保存。
现在可以使用这些数据了:
<Serializable()>_
PublicClassMenuItem
InheritsLzmTW.uSystem.uCollection.SinceLink.SinceLinkItemBase(OfInteger)
PrivategTextAsString
PrivategDeclareAsString
PrivategToolTipTextAsString
PrivategShortcutAsInteger
PrivategClickActionAsString
PrivategVisibleAsBoolean
PrivategEnabledAsBoolean
PublicPropertyText()AsString
Get
ReturngText
EndGet
Set(ByValvalueAsString)
gText=value
EndSet
EndProperty
PublicProperty[Declare]()AsString
Get
ReturngDeclare
EndGet
Set(ByValvalueAsString)
gDeclare=value
EndSet
EndProperty
PublicPropertyToolTipText()AsString
Get
ReturngToolTipText
EndGet
Set(ByValvalueAsString)
gToolTipText=value
EndSet
EndProperty
PublicPropertyShortcut()AsInteger
Get
ReturngShortcut
EndGet
Set(ByValvalueAsInteger)
gShortcut=value
EndSet
EndProperty
PublicPropertyClickAction()AsString
Get
ReturngClickAction
EndGet
Set(ByValvalueAsString)
gClickAction=value
EndSet
EndProperty
PublicPropertyVisible()AsBoolean
Get
ReturngVisible
EndGet
Set(ByValvalueAsBoolean)
gVisible=value
EndSet
EndProperty
PublicPropertyEnabled()AsBoolean
Get
ReturngEnabled
EndGet
Set(ByValvalueAsBoolean)
gEnabled=value
EndSet
EndProperty
EndClass
PublicClassMenuItem
InheritsLzmTW.uSystem.uCollection.SinceLink.SinceLinkItemBase(OfInteger)
PrivategTextAsString
PrivategDeclareAsString
PrivategToolTipTextAsString
PrivategShortcutAsInteger
PrivategClickActionAsString
PrivategVisibleAsBoolean
PrivategEnabledAsBoolean
PublicPropertyText()AsString
Get
ReturngText
EndGet
Set(ByValvalueAsString)
gText=value
EndSet
EndProperty
PublicProperty[Declare]()AsString
Get
ReturngDeclare
EndGet
Set(ByValvalueAsString)
gDeclare=value
EndSet
EndProperty
PublicPropertyToolTipText()AsString
Get
ReturngToolTipText
EndGet
Set(ByValvalueAsString)
gToolTipText=value
EndSet
EndProperty
PublicPropertyShortcut()AsInteger
Get
ReturngShortcut
EndGet
Set(ByValvalueAsInteger)
gShortcut=value
EndSet
EndProperty
PublicPropertyClickAction()AsString
Get
ReturngClickAction
EndGet
Set(ByValvalueAsString)
gClickAction=value
EndSet
EndProperty
PublicPropertyVisible()AsBoolean
Get
ReturngVisible
EndGet
Set(ByValvalueAsBoolean)
gVisible=value
EndSet
EndProperty
PublicPropertyEnabled()AsBoolean
Get
ReturngEnabled
EndGet
Set(ByValvalueAsBoolean)
gEnabled=value
EndSet
EndProperty
EndClass
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
DimdsAsNewDataSet
ds.ReadXmlSchema("Menus.xsd")
ds.ReadXml("Menus.xml")
DimmCollectionAsNewLzmTW.uSystem.uCollection.SinceLink.SinceLinkItemCollection(OfInteger,MenuItem)
'mCollection.Read()
mCollection.AppendFromSinceLinkTable(ds.Tables(0))
'mCollection.Save()
Me.DataGridView1.DataSource=mCollection.Node.ConvertToDataTable(True)
Me.TreeView1.Nodes.Clear()
Me.TreeView1.Nodes.Add(mCollection.Node.ConvertToTreeNode("Declare",True))
'Console.WriteLine(mCollection.Node.FindFirstNode("Code","300").Item.Text)
'Console.WriteLine(mCollection.Find("Code","300").Text)
EndSub
DimdsAsNewDataSet
ds.ReadXmlSchema("Menus.xsd")
ds.ReadXml("Menus.xml")
DimmCollectionAsNewLzmTW.uSystem.uCollection.SinceLink.SinceLinkItemCollection(OfInteger,MenuItem)
'mCollection.Read()
mCollection.AppendFromSinceLinkTable(ds.Tables(0))
'mCollection.Save()
Me.DataGridView1.DataSource=mCollection.Node.ConvertToDataTable(True)
Me.TreeView1.Nodes.Clear()
Me.TreeView1.Nodes.Add(mCollection.Node.ConvertToTreeNode("Declare",True))
'Console.WriteLine(mCollection.Node.FindFirstNode("Code","300").Item.Text)
'Console.WriteLine(mCollection.Find("Code","300").Text)
EndSub
结果:
用这种方法,处理以上三种通常用到的数据,还是比较方便的。
本文地址:http://www.45fan.com/a/question/69765.html