📄 form1.vb
字号:
Imports System.Xml
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents lstBooks As System.Windows.Forms.ListBox
Friend WithEvents txtName As System.Windows.Forms.TextBox
Friend WithEvents txtPrice As System.Windows.Forms.TextBox
Friend WithEvents txtAuthor As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents btnAdd As System.Windows.Forms.Button
Friend WithEvents btnRemove As System.Windows.Forms.Button
Friend WithEvents btnSave As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.lstBooks = New System.Windows.Forms.ListBox()
Me.txtName = New System.Windows.Forms.TextBox()
Me.txtPrice = New System.Windows.Forms.TextBox()
Me.txtAuthor = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.btnAdd = New System.Windows.Forms.Button()
Me.btnRemove = New System.Windows.Forms.Button()
Me.btnSave = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'lstBooks
'
Me.lstBooks.ItemHeight = 12
Me.lstBooks.Name = "lstBooks"
Me.lstBooks.Size = New System.Drawing.Size(248, 268)
Me.lstBooks.TabIndex = 0
'
'txtName
'
Me.txtName.Location = New System.Drawing.Point(296, 8)
Me.txtName.Name = "txtName"
Me.txtName.Size = New System.Drawing.Size(120, 21)
Me.txtName.TabIndex = 1
Me.txtName.Text = ""
'
'txtPrice
'
Me.txtPrice.Location = New System.Drawing.Point(296, 40)
Me.txtPrice.Name = "txtPrice"
Me.txtPrice.Size = New System.Drawing.Size(120, 21)
Me.txtPrice.TabIndex = 1
Me.txtPrice.Text = ""
'
'txtAuthor
'
Me.txtAuthor.Location = New System.Drawing.Point(296, 72)
Me.txtAuthor.Name = "txtAuthor"
Me.txtAuthor.Size = New System.Drawing.Size(120, 21)
Me.txtAuthor.TabIndex = 1
Me.txtAuthor.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(256, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(32, 16)
Me.Label1.TabIndex = 2
Me.Label1.Text = "书名"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(256, 48)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(32, 16)
Me.Label2.TabIndex = 2
Me.Label2.Text = "价格"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(256, 80)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(32, 16)
Me.Label3.TabIndex = 2
Me.Label3.Text = "作者"
'
'btnAdd
'
Me.btnAdd.Location = New System.Drawing.Point(264, 144)
Me.btnAdd.Name = "btnAdd"
Me.btnAdd.Size = New System.Drawing.Size(56, 24)
Me.btnAdd.TabIndex = 3
Me.btnAdd.Text = "添加"
'
'btnRemove
'
Me.btnRemove.Location = New System.Drawing.Point(344, 144)
Me.btnRemove.Name = "btnRemove"
Me.btnRemove.Size = New System.Drawing.Size(64, 24)
Me.btnRemove.TabIndex = 4
Me.btnRemove.Text = "移除"
'
'btnSave
'
Me.btnSave.Location = New System.Drawing.Point(264, 184)
Me.btnSave.Name = "btnSave"
Me.btnSave.Size = New System.Drawing.Size(56, 24)
Me.btnSave.TabIndex = 5
Me.btnSave.Text = "保存"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(424, 270)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSave, Me.btnRemove, Me.btnAdd, Me.Label1, Me.txtName, Me.lstBooks, Me.txtPrice, Me.txtAuthor, Me.Label2, Me.Label3})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private hashTable As Collections.Hashtable = New hashTable()
Private Sub ReloadList()
lstBooks.Items.Clear()
'告诉列表框:在元素添加的时候,应该显示它们的"DisplayBook"属性
'此属性定义在Book类中
lstBooks.DisplayMember = "DisplayBook"
'获得该哈希表的遍历器,用它可以一个一个的访问哈希表的所有元素
Dim myEnumerator As IDictionaryEnumerator = hashTable.GetEnumerator()
'利用遍历器在哈希表中移动,把每个元素加入到列表框中
While myEnumerator.MoveNext()
lstBooks.Items.Add(myEnumerator.Value)
End While
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadXML()
ReloadList()
End Sub
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
Dim b As Book
'从列表框中得到被选择的书
b = CType(lstBooks.SelectedItem, Book)
'在哈希表中移除它
hashTable.Remove(b.Name)
ReloadList()
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If txtName.Text <> "" And txtPrice.Text <> "" And txtAuthor.Text <> "" Then
Dim b As Book = New Book()
b.Name = txtName.Text
b.Price = Integer.Parse(txtPrice.Text)
b.Author = txtAuthor.Text
'以下的判断并没有必要,只是为了演示Contain方法的使用
If hashTable.Contains(b.Name) Then
hashTable.Item(b.Name) = b
Else
hashTable.Add(b.Name, b)
End If
ReloadList()
End If
End Sub
Sub LoadXML()
'从XML文件中装入xml文档
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("..\Books.xml")
'得到Book节点的列表
Dim nodeList As XmlNodeList
nodeList = xmlDoc.GetElementsByTagName("Book")
Dim oNode, oChild As XmlNode
'遍历所有的Book节点,并且把数据加入到哈希表中
For Each oNode In nodeList
Dim b As New Book()
oChild = oNode.Item("Name")
b.Name = oChild.InnerText
oChild = oNode.Item("Price")
b.Price = Integer.Parse(oChild.InnerText)
oChild = oNode.Item("Author")
b.Author = oChild.InnerText
hashTable.Add(b.Name, b)
Next
End Sub
Sub SaveXML()
'为了避免自行生成XML文档,所以先从旧的XML文档中装入
'这样就可以利用它的结构了
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("..\Books.xml")
'同理,为了避免自行生成Books节点,从旧的XML文档中得到
'这样也可以利用其原有的结构
Dim oRootNode As XmlNode
oRootNode = xmlDoc.GetElementsByTagName("Books").Item(0)
'得到Book节点
Dim oNode As XmlNode
oNode = oRootNode.Item("Book")
'清空原有的Books节点,准备加入新内容
oRootNode.RemoveAll()
'获得该哈希表的遍历器,用它可以一个一个的访问哈希表的所有元素
Dim myEnumerator As IDictionaryEnumerator = hashTable.GetEnumerator()
'利用遍历器在哈希表中移动,把每个元素加入到列表框中
While myEnumerator.MoveNext()
Dim b As Book
b = myEnumerator.Value
Dim oNewNode As XmlNode
'把原有Book节点的内容完整的克隆到一个新节点中
oNewNode = oNode.CloneNode(True)
'然后更改新节点的内容
oNewNode.Item("Name").InnerText = b.Name
oNewNode.Item("Price").InnerText = b.Price
oNewNode.Item("Author").InnerText = b.Author
'将新节点放回节点树,准备保存
oRootNode.AppendChild(oNewNode)
End While
'把节点树放回XML文档,并保存到文件中
xmlDoc.AppendChild(oRootNode)
xmlDoc.Save("..\NewBooks.xml")
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
SaveXML()
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -