惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
LINUX DO - 热门话题
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threat Research - Cisco Blogs
AI
AI
B
Blog RSS Feed
S
Schneier on Security
雷峰网
雷峰网
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
罗磊的独立博客
有赞技术团队
有赞技术团队
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
博客园 - Franky
Attack and Defense Labs
Attack and Defense Labs
The Cloudflare Blog
Webroot Blog
Webroot Blog
Last Week in AI
Last Week in AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 叶小钗
美团技术团队
L
Lohrmann on Cybersecurity
T
The Blog of Author Tim Ferriss
The Last Watchdog
The Last Watchdog
T
Troy Hunt's Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
Know Your Adversary
Know Your Adversary
O
OpenAI News
博客园 - 【当耐特】
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cybersecurity and Infrastructure Security Agency CISA
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
PCI Perspectives
PCI Perspectives
H
Heimdal Security Blog
I
InfoQ
GbyAI
GbyAI
T
Threatpost
C
Cisco Blogs

博客园 - 水如烟(LzmTW)

.NET的变量在代码集中是不安全的 HOW TO:获取硬盘物理序列号(VB.NET) 一个创建快捷方式类 一个简单的CodeAccessPermission生成器 认识一下Attribute HOW TO:端口打印(比较粗糙) 标记法定义和创建数据库(四) 标记法定义和创建数据库 Sql2005数据类型与Framework类型的对应关系 HOW TO:重启程序(WinForm) HOW TO:设置默认打印机 HOW TO:避免“将COM对象与其基础RCW分开后不能再使用该对象”错误 HOW TO:控制是否允许用户退出ExcelApplication的Workbook 树和自联表(五) 树和自联表(四) 树和自联表(一) 行政区划数据数据库的设计(脚本) 行政区划数据数据库的设计(七) 行政区划数据数据库的设计(六)
树和自联表(六)
水如烟(LzmTW) · 2006-11-11 · via 博客园 - 水如烟(LzmTW)

Author:水如烟  

三种情形示例

第一个示例,树情形。取MainForm主菜单的数据。ToolStripMenuItem本身是一个树。

随便在一个Form上加一些菜单。我的如图:

结果是:

代码:

Public Class FormTreePrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
Dim mCollection As New LzmTW.uSystem.uCollection.SinceLink.SinceLinkItemCollection(Of Integer, 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)
    
End SubPrivate Function GetMenuNode(ByVal form As Form) As LzmTW.uSystem.uCollection.Node(Of MenuItemInformation)
        
If form.MainMenuStrip Is Nothing Then Return Nothing
        
Dim mNode As New LzmTW.uSystem.uCollection.Node(Of MenuItemInformation)(New MenuItemInformation)
        mNode.Item.Name 
= "Root"For Each item As ToolStripMenuItem In form.MainMenuStrip.Items
            AppendNode(mNode, item)
        
NextReturn mNode
    
End FunctionPrivate Sub AppendNode(ByVal node As LzmTW.uSystem.uCollection.Node(Of MenuItemInformation), ByVal menuItem As ToolStripMenuItem)
        
Dim mCurrentNode As LzmTW.uSystem.uCollection.Node(Of MenuItemInformation) = Nothing
        mCurrentNode 
= node.Nodes.Add(New MenuItemInformation(menuItem))For Each item As ToolStripMenuItem In menuItem.DropDownItems
            AppendNode(mCurrentNode, item)
        
NextEnd Sub
End ClassPublic Class MenuItemInformation
    
Inherits LzmTW.uSystem.uCollection.SinceLink.SinceLinkItemBase(Of Integer)Private gText As String
    
Public Property Text() As String
        
Get
            
Return gText
        
End Get
        
Set(ByVal value As String)
            gText 
= value
        
End Set
    
End PropertySub New()
    
End SubSub New(ByVal menuItem As ToolStripMenuItem)
        
With menuItem
            
Me.Name = .Name
            
Me.Text = .Text
        
End With
    
End SubEnd Class

第二个示例,(Code,Name)情形。仍以行政区划数据为例。

代码:

Public Class Form1Public gRegionalDatas As DataTable '这是列为Code,Name的行政区划数据表

    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim mCollection As New LzmTW.uSystem.uCollection.SinceLink.SinceLinkItemCollection(Of String, RegionalCodeItem)("00,00,00")For Each row As DataRow In gRegionalDatas.Rows
            
With row
                mCollection.Add(.Item(
"Code").ToString, .Item("Name").ToString)
            
End With
        
NextMe.TreeView1.Nodes.Clear()
        
Me.TreeView1.Nodes.Add(mCollection.Node.ConvertToTreeNode("Name"True))Me.DataGridView1.DataSource = mCollection.Node.ConvertToDataTable(True)
    
End SubEnd ClassPublic Class RegionalCodeItem
    
Inherits LzmTW.uSystem.uCollection.SinceLink.SinceLinkItemBase(Of String)Sub New()
    
End SubSub New(ByVal code As StringByVal name As String)
        
MyBase.New(code, name)
    
End SubEnd Class

效果:

第三个示例,自联表数据情形。这里以树和自联表(四) 上的数据为例。

要使用本示例代码,需把以上数据存为一个Excel文件,文件名为Menus.xls,数据簿工作表名称为Menus。存它到程序运行目录上。

首先把数据读进一个DataTable里面。

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        
Dim cn As New Odbc.OdbcConnection("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=menus.xls;HDR=Yes;")
        
Dim cm As New Odbc.OdbcCommand("SELECT * FROM [Menus$]", cn)
        
Dim ad As New Odbc.OdbcDataAdapter(cm)
        
Dim mds As New DataSet("Menus")
        ad.Fill(mds, 
"Menu")
        mds.WriteXmlSchema(
"Menus.xsd")
        mds.WriteXml(
"menus.xml")
        
Me.DataGridView1.DataSource = mds.Tables(0)
    
End Sub

由于我没有预建一个DataTable,所以上面代码运行后它自动的将Integer类型作为Double类型来处理。因此,需把它转为Intege类型。在此,我将数据存为xml形式,同时输出Schema文件。

之后,打开Menus.xsd文件,将所有Double类型改为Integer类型,保存。

现在可以使用这些数据了:

<Serializable()> _
Public Class MenuItem
    
Inherits LzmTW.uSystem.uCollection.SinceLink.SinceLinkItemBase(Of Integer)Private gText As String
    
Private gDeclare As String
    
Private gToolTipText As String
    
Private gShortcut As Integer
    
Private gClickAction As String
    
Private gVisible As Boolean
    
Private gEnabled As BooleanPublic Property Text() As String
        
Get
            
Return gText
        
End Get
        
Set(ByVal value As String)
            gText 
= value
        
End Set
    
End PropertyPublic Property [Declare]() As String
        
Get
            
Return gDeclare
        
End Get
        
Set(ByVal value As String)
            gDeclare 
= value
        
End Set
    
End PropertyPublic Property ToolTipText() As String
        
Get
            
Return gToolTipText
        
End Get
        
Set(ByVal value As String)
            gToolTipText 
= value
        
End Set
    
End PropertyPublic Property Shortcut() As Integer
        
Get
            
Return gShortcut
        
End Get
        
Set(ByVal value As Integer)
            gShortcut 
= value
        
End Set
    
End PropertyPublic Property ClickAction() As String
        
Get
            
Return gClickAction
        
End Get
        
Set(ByVal value As String)
            gClickAction 
= value
        
End Set
    
End PropertyPublic Property Visible() As Boolean
        
Get
            
Return gVisible
        
End Get
        
Set(ByVal value As Boolean)
            gVisible 
= value
        
End Set
    
End PropertyPublic Property Enabled() As Boolean
        
Get
            
Return gEnabled
        
End Get
        
Set(ByVal value As Boolean)
            gEnabled 
= value
        
End Set
    
End Property
End Class

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
Dim ds As New DataSet
        ds.ReadXmlSchema(
"Menus.xsd")
        ds.ReadXml(
"Menus.xml")Dim mCollection As New LzmTW.uSystem.uCollection.SinceLink.SinceLinkItemCollection(Of Integer, 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)
    End Sub

结果:

用这种方法,处理以上三种通常用到的数据,还是比较方便的。