个人电话号码名录,使用 WinForms
此应用程序以名录样式存储名字、姓氏和电话号码。这是 Steve Krile 的 Ajax Slider 的 WinForms 版本,使用了 SSDiver2112 的 CButton 和 gTrackBar 控件。

引言
当我第一次在特色文章区看到 Steve Krile 的 Ajax Rolodex 时,我就想看看能否用 WinForms
、DataGridView
和 SSDiver2112 的 CButton 和 gTrackBar 控件重新创建它。我不得不在 Visual Studio
的组件或 SSDiver2112 的组件之间做出选择。我选择了后者。接下来是问题如何向用户显示数据,当然我使用了 DataGridView
。然后,要么是 SQL 数据库,要么是逗号分隔的文件。我选择了文件。别问我为什么,我就是这么做的。
背景
我尝试创建不同外观的窗体来显示某个人的所有信息。它包括 FirstName
、LastName
、Address
、City
、State
、Zip
、Phone
、Cell
、E-mail
等等。我厌倦了输入所有这些信息。如果我不知道一个朋友住在哪里,或者我最喜欢的木材场在哪里,我就不需要去那里。此外,如果他们寄给我圣诞贺卡,我总能使用回邮地址。这就是为什么我最终选择“LastName,FirstName,PhoneNumber,
”逗号分隔的文件。
DataGridView 简介
我在不同的文章,这里和其他代码库中看到,DataGridView
相当难以适应。但我发现,你越使用它,就越容易处理。DataGridView
已从 VB 5,6
发展成为 dotNet
中一个强大的组件。这就是为什么它会让一些编码人员不知所措,可能还会让他们望而却步。继续使用它。用一段时间后,它就会变得像第二天性一样。
Using the Code
我已经删除了 contacts.txt 文件。当您首次运行应用程序时,它将被创建。请勿单击或使用任何带标签的按钮或滑块,否则您会看到运行时异常四处崩溃。每个字母至少要有一个条目。现在,只需添加您的所有信息,稍后我将添加异常处理并重新发布。
有两种方式输入您的信息,第一种方式是运行应用程序,然后单击 cbtnAddContacts
按钮。这将弹出上面显示的那个小窗体。请务必为每个字母添加一个条目。第二种方式是运行应用程序,然后关闭它。打开 Windows Explorer,找到您的 Application.StartUp
路径(Bin\Debug
),然后打开新创建的文本文件并以这种方式添加您的信息。每行应如下所示… Bunny,Bugs,212-555-1234,
或 Lumber,Tyson's Lumber,212-555-5678
。话已至此……我们继续。
代码使用… 继续
有 28 个 button_clickButtonArea
事件。我们只看一个(cbtnAll_ClickButtonArea
)。它打开 contacts.txt 文件,设置 gTrackBar.Value
,获取我们需要用于 DataGridView.RowCount
的行数,然后关闭文件。接下来我们重新打开文件以提取所有信息并将其显示在 GridView
中。
Private Sub cbtnAll_ClickButtonArea(ByVal Sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles cbtnAll.ClickButtonArea
gtb1.Value = 0
Dim i As Integer = 0
Dim rowCounter As Integer = 0
Dim fs As FileStream
Dim sr As StreamReader
Dim strFile As String
Dim parts() As String
dgv1.SelectAll()
dgv1.ClearSelection()
'We just get the row count here.
Try
fs = New FileStream(strContacts, FileMode.Open, FileAccess.Read)
sr = New StreamReader(fs)
strFile = sr.ReadLine()
Do Until strFile Is Nothing
rowCounter += 1
strFile = sr.ReadLine
Loop
fs.Close()
Catch ex As Exception
End Try
dgv1.RowCount = rowCounter
Try
fs = New FileStream(strContacts, FileMode.Open, FileAccess.Read)
sr = New StreamReader(fs)
strFile = sr.ReadLine()
For i = 0 To dgv1.RowCount - 1
parts = strFile.Split(",")
dgv1.Rows(i).Cells(0).Value = parts(0) 'Last Name
dgv1.Rows(i).Cells(1).Value = parts(1) 'First Name
dgv1.Rows(i).Cells(2).Value = parts(2) 'Phone Number
dgv1.Update()
strFile = sr.ReadLine()
If sr.EndOfStream Then
Exit For
End If
Next
fs.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
所有 26 个字母标签按钮都有相同的目标… 将 gTrackBar.Value
设置到正确的位置。
Private Sub cbtnA_ClickButtonArea(ByVal Sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles cbtnA.ClickButtonArea
gtb1.Value = 1
End Sub
Private Sub cbtnB_ClickButtonArea(ByVal Sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles cbtnB.ClickButtonArea
gtb1.Value = 2
End Sub
Private Sub cbtnC_ClickButtonArea(ByVal Sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles cbtnC.ClickButtonArea
gtb1.Value = 3
End Sub
'...D thru Z continues
当 gTrackBar.Value
更改时,会触发 gTrackBar.ValueChanged
事件。对于 gTrackBar
中的每个值,我们将标签 lblLetter.Text
的值设置为滑块轨道上的当前字母。然后我们调用子程序 OpenTextFile(ByVal fName As String)
。这是字母(A 到 Z)发生所有事情的地方。在这里,我们创建一个临时文件(temp.txt)来保存当前所选字母的信息。我们获取网格的行数,将适合我们选定字母的数据添加到新的临时文件中,然后关闭并释放。接下来我们清空 DataGridView
,从主文件(contacts.txt)读取并写入临时文件(temp.txt),设置其 DataGridView.RowCount
,重新打开临时文件并将数据添加到 DataGridView
。然后,我们通过删除临时文件并重新创建它来重复整个过程。
Private Sub OpentextFile(ByVal fname As String)
Dim i As Integer = 0
Dim rowCounter As Integer = 0
Dim ts As StreamReader
Dim fs As FileStream
Dim sw As StreamWriter
Dim sr As StreamReader
Dim strFile As String
Dim parts() As String
If File.Exists(strTemp) Then
File.Delete(strTemp)
End If
'Then re-create it.
ts = New StreamReader(File.Create(strTemp))
ts.Close()
ts.Dispose()
'Clean out the DataGridView.
dgv1.SelectAll()
dgv1.ClearSelection()
'We just get the row count here.
Try
fs = New FileStream(fname, FileMode.Open, FileAccess.Read)
sw = New StreamWriter(strTemp, True)
sr = New StreamReader(fs)
strFile = sr.ReadLine()
Do Until strFile Is Nothing
parts = strFile.Split(",")
If parts(0).StartsWith(lblLetter.Text) Then
rowCounter += 1
sw.WriteLine(strFile)
Else
'Do nothing
End If
strFile = sr.ReadLine
Loop
'Do Clean up.
fs.Close()
sw.Close()
sw.Dispose()
sr.Close()
Catch ex As Exception
End Try
dgv1.RowCount = rowCounter
Try
sr = File.OpenText(strTemp)
strFile = sr.ReadLine()
For i = 0 To dgv1.RowCount - 1
parts = strFile.Split(",")
If parts(0).StartsWith(lblLetter.Text) Then
dgv1.Rows(i).Cells(0).Value = parts(0) 'Last Name
dgv1.Rows(i).Cells(1).Value = parts(1) 'First Name
dgv1.Rows(i).Cells(2).Value = parts(2) 'Phone Number
dgv1.Update()
Else
'Do nothing
End If
strFile = sr.ReadLine()
Next
'Do Clean up.
sr.Close()
sr.Dispose()
Catch ex As Exception
End Try
End Sub
添加联系人窗体
这里没什么太多内容。我们所做的就是打开 contacts.txt 文件,并将我们在窗体上的文本框中输入的内容追加到其中。代码如下…
Private Sub cbtnSave_ClickButtonArea(ByVal Sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles cbtnSave.ClickButtonArea
'If something is blank, Get it corrected.
If txtLastName.Text = "" Or txtFirstName.Text = _
"" Or txtPhoneNumber.Text = "" Then
MessageBox.Show("Make sure ALL text boxes are filled in.", _
"Info to the Rescue!", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If
If File.Exists(strPath) Then
'Set the FileStream and StreamWriter
fs = New FileStream(strPath, FileMode.Append)
sw = New StreamWriter(fs)
'Set and Write our data.
strAppend = Trim(txtLastName.Text) & "," & Trim(txtFirstName.Text) & _
"," & Trim(txtPhoneNumber.Text) & "," & vbCrLf
sw.WriteLine(strAppend)
'Clean-Up on aisle 7.
CleanUp()
sw.Close()
fs.Close()
sw.Dispose()
fs.Dispose()
Else
MessageBox.Show("File seams to be missing. Would you like to create it?", _
"ERROR", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
If ret = Windows.Forms.DialogResult.Yes Then
File.Create(strPath)
'Display Success! if the file was created.
MessageBox.Show("File created...(contacts.txt)", "Success!", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
'Do nothing...Let the user add his/her contacts.
End If
End If
End Sub
关注点
历史
- 首次上传于 2011 年 10 月 13 日