<b>This is an image of the Data Entry Form:</b> (I\'ve since added a menu strip
ID: 3627282 • Letter: #
Question
<b>This is an image of the Data Entry Form:</b>
(I've since added a menu strip with File as item and Lookup Form as subitem.)
<b>And here is my code for the DataEntryForm:</b>
' Description: Program will store personal information for a little electronic "black book".
' Allows user to enter data in the text boxes and save data to file blackbook.dat.
' Lookup Form allows user to select filename as source of data and view data for
' each person by choosing a name from the dropdown list.
Imports System.IO
Public Class DataEntryForm
' Declare module-level variable
Friend BlackBookStreamWriter As StreamWriter
Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
'Close data file and Data Entry Form.
If BlackBookStreamWriter IsNot Nothing Then
BlackBookStreamWriter.Close()
End If
Me.Close()
End Sub
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
' Save the record to file.
If BlackBookStreamWriter IsNot Nothing Then ' Is the file open?
BlackBookStreamWriter.WriteLine(NameTextBox.Text)
BlackBookStreamWriter.WriteLine(PhoneTextBox.Text)
BlackBookStreamWriter.WriteLine(PagerTextBox.Text)
BlackBookStreamWriter.WriteLine(CellTextBox.Text)
BlackBookStreamWriter.WriteLine(VoiceMailTextBox.Text)
BlackBookStreamWriter.WriteLine(EmailTextBox.Text)
' Clear form for next contact.
With NameTextBox
.Clear()
.Focus()
End With
PhoneTextBox.Clear()
PagerTextBox.Clear()
CellTextBox.Clear()
VoiceMailTextBox.Clear()
EmailTextBox.Clear()
Else ' File is not open.
MessageBox.Show("You must first open the file before you can save a record.", "File Not Open", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
' Display the File Open dialog box.
LookupForm.OpenFileToolStripMenuItem_Click(sender, e)
End If
End Sub
Private Sub LookupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookupToolStripMenuItem.Click
' Open the Lookup Form.
LookupForm.ShowDialog()
End Sub
End Class
<b>Writes data to blackbook.dat
****************sample***************</b>
Peter Rabbit
345-555-4321
345-555-5432
345-555-6543
345-555-7654
prabbit@rabbitholes.com
Billy Gates
999-123-9876
911
999-123-6789
777
bgates@msn.com
<b>Here is an image of the Lookup Form:</b>
The Lookup Form should read from same data file blackbook.dat
** Note that there is a combox dropdown list for the name field, allowing user to navigate records by selecting a name from the list.
<b>Here is where I'm having trouble.......Reading the data from blackbook.dat and displaying it as pictured above. Below is the code I have so far, for the Lookup Form. It's kind of a mess and I am stuck.</b>
' Description: Program will store personal information for a little electronic "black book".
' Allows user to enter data in the text boxes and save data to file blackbook.dat.
' Lookup Form allows user to select filename as source of data and view data for
' each person by choosing a name from the dropdown list.
Imports System.IO
Public Class LookupForm
' Declare module-level variable.
Private BlackbookStreamReader As StreamReader
Private Sub LookupForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Open the input file and display the first record.
Try
BlackbookStreamReader = New StreamReader("BlackBook.dat")
DisplayRecord()
Catch ex As Exception
MessageBox.Show("File is not found or is invalid.", "Data Error")
End Try
End Sub
Private Sub DisplayRecord()
' Read and display the next record.
Dim NameString As String
Dim ResponseDialogResult As DialogResult
Try
Do Until BlackbookStreamReader.Peek = -1
NameComboBox.Text = BlackbookStreamReader.ReadLine()
NameComboBox.Items.Add(NameString)
PhoneTextBox.Text = BlackbookStreamReader.ReadLine()
PagerTextBox.Text = BlackbookStreamReader.ReadLine()
VoicemailTextBox.Text = BlackbookStreamReader.ReadLine()
EmailTextBox.Text = BlackbookStreamReader.ReadLine()
Loop
' Close the file.
BlackbookStreamReader.Close()
Catch LookupException As Exception
' File missing.
ResponseDialogResult = MessageBox.Show("Create a new file?", "File Not Found", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If ResponseDialogResult = DialogResult.No Then
' Exit the program
Me.Close()
End If
End Try
End Sub
Friend Sub OpenFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileToolStripMenuItem.Click
' Open the file.
Dim RespsonseDialogResult As DialogResult
' Is the file already open?
If DataEntryForm.BlackBookStreamWriter IsNot Nothing Then
DataEntryForm.BlackBookStreamWriter.Close()
End If
' Set up and display the Open File dialog.
With OpenFileDialog1
' Begin in the current folder.
.InitialDirectory = Directory.GetCurrentDirectory
.FileName = "BlackBook.dat"
.Title = "Select File or Directory for File."
' Display the Open File dialog box.
RespsonseDialogResult = .ShowDialog()
' Make sure the user didn't click the Cancel button.
If RespsonseDialogResult <> DialogResult.Cancel Then
' Open the output file.
DataEntryForm.BlackBookStreamWriter = New StreamWriter(OpenFileDialog1.FileName)
End If
End With
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
' Close the data file and the Lookup Form.
If DataEntryForm.BlackBookStreamWriter IsNot Nothing Then ' Is the file open?
DataEntryForm.BlackBookStreamWriter.Close()
End If
Me.Close()
End Sub
End Class
<b>Please edit or rewrite the above code for the Lookup Form it will read records from blackbook.dat and display them one record at a time using the textboxes pictured above.</b>
Thanks in advance for your help!
Explanation / Answer
Public Class DataEntryForm
' Declare module-level variable
Friend BlackBookStreamWriter As StreamWriter
'Close data file and Data Entry Form.
If BlackBookStreamWriter IsNot Nothing Then
BlackBookStreamWriter.Close()
End If
Me.Close()
End Sub
Private Sub SaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveButton.Click
' Save the record to file.
If BlackBookStreamWriter IsNot Nothing Then
' Is the file open?
BlackBookStreamWriter.Write(NameTextBox.Text + ",")
BlackBookStreamWriter.Write(PhoneTextBox.Text + ",")
BlackBookStreamWriter.Write(PagerTextBox.Text + ",")
BlackBookStreamWriter.Write(CellTextBox.Text + ",")
BlackBookStreamWriter.Write(VoiceMailTextBox.Text + ",")
BlackBookStreamWriter.WriteLine(EmailTextBox.Text)
' Clear form for next contact.
With NameTextBox
.Clear()
.Focus()
End With
PhoneTextBox.Clear()
PagerTextBox.Clear()
CellTextBox.Clear()
VoiceMailTextBox.Clear()
EmailTextBox.Clear()
Else ' File is not open.
MessageBox.Show("You must first open the file before you can save a record.", "File Not Open", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
' Display the File Open dialog box.
'LookupForm.Show()
LookupForm.OpenFileToolStripMenuItem_Click(sender, e)
End If
End Sub
Private Sub LookupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookUpToolStripMenuItem.Click
' Open the Lookup Form.
BlackBookStreamWriter.Close()
LookupForm.ShowDialog()
End Sub
Private Sub DataEntryForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'open existing file for writing(file should be in your directory location)
BlackBookStreamWriter = File.AppendText("BlackBook.txt")
End Sub
End Class
'***************** Lookup Form.vb****************
'creating structure
Structure BlackBook
Dim Name As String
Dim Phone As String
Dim Pager As String
Dim Cell As String
Dim VoiceMail As String
Dim EMail As String
End Structure
' Declare module-level variable.
Private BlackbookStreamReader As StreamReader
Dim Book(10) As BlackBook
Private Sub LookupForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DisplayRecord()
End Sub
' Read and display
Dim records() As String = IO.File.ReadAllLines("BlackBook.txt")
Dim line As String
Dim data() As String
Dim count As Integer = records.Count
ReDim Book(count)
NameComboBox.Items.Clear()
NameComboBox.Items.Add("Select Name")
'filling structure array
For i As Integer = 0 To count - 1
line = records(i)
data = line.Split(","c)
Book(i).Name = data(0)
NameComboBox.Items.Add(data(0))
Book(i).Phone = data(1)
Book(i).Pager = data(2)
Book(i).Cell = data(3)
Book(i).VoiceMail = data(4)
Book(i).EMail = data(5)
Next
End Sub
Friend Sub OpenFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenFileToolStripMenuItem.Click
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
' Close the data file and the Lookup Form.
If DataEntryForm.BlackBookStreamWriter IsNot Nothing Then ' Is the file open?
DataEntryForm.BlackBookStreamWriter.Close()
End If
Me.Close()
End Sub
Private Sub NameComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NameComboBox.SelectedIndexChanged
'setting selected record data into text boxes
Dim query = From bk In Book
Where bk.Name = NameComboBox.Text
Select bk
For Each b In query
PhoneTextBox.Text = b.Phone
PagerTextBox.Text = b.Pager
CellTextBox.Text = b.Cell
VoiceMailTextBox.Text = b.VoiceMail
EmailTextBox.Text = b.EMail
Next
End Sub
End Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.