📄 form1.vb
字号:
Imports System
Imports System.IO
Imports System.Threading
Imports System.Threading.Thread
Imports System.IO.Directory
Imports System.IO.File
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
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
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents lstInfo As System.Windows.Forms.ListBox
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents btnClear As System.Windows.Forms.Button
Friend WithEvents btnSave As System.Windows.Forms.Button
Friend WithEvents lblNumber As System.Windows.Forms.Label
Friend WithEvents lblName As System.Windows.Forms.Label
Friend WithEvents txtName As System.Windows.Forms.TextBox
Friend WithEvents txtPhone As System.Windows.Forms.TextBox
Friend WithEvents txtBook As System.Windows.Forms.TextBox
Friend WithEvents btnOpen As System.Windows.Forms.Button
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents lblAbout As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.lstInfo = New System.Windows.Forms.ListBox
Me.lblAbout = New System.Windows.Forms.Label
Me.Panel1 = New System.Windows.Forms.Panel
Me.Label1 = New System.Windows.Forms.Label
Me.btnOpen = New System.Windows.Forms.Button
Me.txtBook = New System.Windows.Forms.TextBox
Me.txtName = New System.Windows.Forms.TextBox
Me.btnClear = New System.Windows.Forms.Button
Me.btnSave = New System.Windows.Forms.Button
Me.txtPhone = New System.Windows.Forms.TextBox
Me.lblNumber = New System.Windows.Forms.Label
Me.lblName = New System.Windows.Forms.Label
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.Panel1.SuspendLayout()
Me.SuspendLayout()
'
'lstInfo
'
Me.lstInfo.BackColor = System.Drawing.Color.SlateGray
Me.lstInfo.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.lstInfo.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lstInfo.ForeColor = System.Drawing.Color.White
Me.lstInfo.ItemHeight = 16
Me.lstInfo.Location = New System.Drawing.Point(8, 64)
Me.lstInfo.Name = "lstInfo"
Me.lstInfo.Size = New System.Drawing.Size(184, 194)
Me.lstInfo.TabIndex = 4
'
'lblAbout
'
Me.lblAbout.BackColor = System.Drawing.Color.SlateGray
Me.lblAbout.Cursor = System.Windows.Forms.Cursors.Hand
Me.lblAbout.Font = New System.Drawing.Font("Verdana", 24.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblAbout.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
Me.lblAbout.Image = CType(resources.GetObject("lblAbout.Image"), System.Drawing.Image)
Me.lblAbout.ImageAlign = System.Drawing.ContentAlignment.MiddleRight
Me.lblAbout.Location = New System.Drawing.Point(0, 0)
Me.lblAbout.Name = "lblAbout"
Me.lblAbout.Size = New System.Drawing.Size(440, 56)
Me.lblAbout.TabIndex = 7
Me.lblAbout.Text = "Address Book"
Me.lblAbout.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'Panel1
'
Me.Panel1.BackColor = System.Drawing.Color.SlateGray
Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.Panel1.Controls.Add(Me.Label1)
Me.Panel1.Controls.Add(Me.btnOpen)
Me.Panel1.Controls.Add(Me.txtBook)
Me.Panel1.Controls.Add(Me.txtName)
Me.Panel1.Controls.Add(Me.btnClear)
Me.Panel1.Controls.Add(Me.btnSave)
Me.Panel1.Controls.Add(Me.txtPhone)
Me.Panel1.Controls.Add(Me.lblNumber)
Me.Panel1.Controls.Add(Me.lblName)
Me.Panel1.Location = New System.Drawing.Point(200, 64)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(240, 192)
Me.Panel1.TabIndex = 8
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
Me.Label1.Location = New System.Drawing.Point(8, 80)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(40, 16)
Me.Label1.TabIndex = 15
Me.Label1.Text = "Book"
'
'btnOpen
'
Me.btnOpen.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnOpen.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnOpen.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
Me.btnOpen.Image = CType(resources.GetObject("btnOpen.Image"), System.Drawing.Image)
Me.btnOpen.Location = New System.Drawing.Point(192, 72)
Me.btnOpen.Name = "btnOpen"
Me.btnOpen.Size = New System.Drawing.Size(32, 24)
Me.btnOpen.TabIndex = 14
'
'txtBook
'
Me.txtBook.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.txtBook.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtBook.Location = New System.Drawing.Point(56, 72)
Me.txtBook.Name = "txtBook"
Me.txtBook.Size = New System.Drawing.Size(128, 23)
Me.txtBook.TabIndex = 13
Me.txtBook.Text = ""
'
'txtName
'
Me.txtName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.txtName.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtName.Location = New System.Drawing.Point(56, 8)
Me.txtName.Name = "txtName"
Me.txtName.Size = New System.Drawing.Size(168, 23)
Me.txtName.TabIndex = 7
Me.txtName.Text = ""
'
'btnClear
'
Me.btnClear.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnClear.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnClear.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
Me.btnClear.Location = New System.Drawing.Point(152, 152)
Me.btnClear.Name = "btnClear"
Me.btnClear.Size = New System.Drawing.Size(72, 32)
Me.btnClear.TabIndex = 12
Me.btnClear.Text = "Clear"
'
'btnSave
'
Me.btnSave.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnSave.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnSave.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
Me.btnSave.Location = New System.Drawing.Point(56, 152)
Me.btnSave.Name = "btnSave"
Me.btnSave.Size = New System.Drawing.Size(72, 32)
Me.btnSave.TabIndex = 11
Me.btnSave.Text = "Save"
'
'txtPhone
'
Me.txtPhone.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.txtPhone.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtPhone.Location = New System.Drawing.Point(56, 40)
Me.txtPhone.Name = "txtPhone"
Me.txtPhone.Size = New System.Drawing.Size(168, 23)
Me.txtPhone.TabIndex = 10
Me.txtPhone.Text = ""
'
'lblNumber
'
Me.lblNumber.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblNumber.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
Me.lblNumber.Location = New System.Drawing.Point(8, 48)
Me.lblNumber.Name = "lblNumber"
Me.lblNumber.Size = New System.Drawing.Size(48, 16)
Me.lblNumber.TabIndex = 9
Me.lblNumber.Text = "Phone"
'
'lblName
'
Me.lblName.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblName.ForeColor = System.Drawing.SystemColors.ActiveCaptionText
Me.lblName.Location = New System.Drawing.Point(8, 16)
Me.lblName.Name = "lblName"
Me.lblName.Size = New System.Drawing.Size(48, 24)
Me.lblName.TabIndex = 8
Me.lblName.Text = "Name"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.SystemColors.ActiveCaptionText
Me.ClientSize = New System.Drawing.Size(440, 262)
Me.Controls.Add(Me.lblAbout)
Me.Controls.Add(Me.lstInfo)
Me.Controls.Add(Me.Panel1)
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.MaximizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Address Book"
Me.Panel1.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Dim ReadThreadStart As ThreadStart = New ThreadStart(AddressOf Display)
Dim ReadArray As ArrayList
Dim AddressBook As String
Dim Directory As Directory
Dim File As File
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtName.Clear()
txtPhone.Clear()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If Save_To_File(txtName.Text, txtPhone.Text, txtBook.Text, False) Then
MsgBox("Save Successful", MsgBoxStyle.Information, "Save Successful")
Else
MsgBox("Save Unsuccessful", MsgBoxStyle.Information, "Save unuccessful")
End If
btnClear_Click(sender, e)
End Sub
Private Sub lblAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblAbout.Click
MsgBox("Address Book" & vbCrLf & "ver. 1.0.9.1" & vbCrLf & "By Oliver Sintim-Aboagye", MsgBoxStyle.Information, "Address Book Ver. 1.0")
End Sub
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
With OpenFileDialog1
.Title = "Address Book File Open"
.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
.ShowDialog()
txtBook.Text = .FileName
AddressBook = txtBook.Text
End With
End Sub
Private Sub Display()
Dim i As Integer
Dim reader As StreamReader
reader = New StreamReader(AddressBook)
ReadArray = New ArrayList
While reader.Peek <> -1
ReadArray.Add(reader.ReadLine)
End While
reader.Close()
lstInfo.Items.Clear()
For i = 0 To ReadArray.Count - 1
lstInfo.Items.Add(ReadArray.Item(i))
Next
End Sub
Private Function Save_To_File(ByVal pName As String, ByVal pPhone As String, ByVal pBook As String, Optional ByVal pNewBook As Boolean = False) As Boolean
Dim Writer As StreamWriter
Dim _readthread As Thread
If File.Exists(pBook) Then
Writer = New StreamWriter(pBook, True)
Else
MsgBox("File Does Not Exist", MsgBoxStyle.Critical, "File Not Found")
Exit Function
End If
Try
Writer.WriteLine(pName & " " & pPhone)
_readthread = New Thread(ReadThreadStart)
_readthread.Start()
Return True
Catch e As Exception
Return False
Finally
Writer.Close()
End Try
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -