Chapter 3 - Programming Project 1 - Membership List
CODE
Public Class Form1
Dim file() As String = IO.File.ReadAllLines("Phones.txt")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
Display()
End Sub
Private Sub lstMembers_SelectedIndexChanged(sender As Object, e As
EventArgs) Handles lstMembers.SelectedIndexChanged
'loop through the file and find name that matches the selected text
in the list box
'and return the name and appropriate phone number
Dim query = From p In file
Let name As String = p.ToString.Split(",")(0)
Let phone = p.ToString.Split(",")(1)
Where name = lstMembers.SelectedItem
Select name, phone
txtName.Text = query.ToList.First.name.ToString
mtbPhone.Text = query.ToList.First.phone.ToString
End Sub
Private Sub mnuAdd_Click(sender As Object, e As EventArgs) Handles
mnuAdd.Click
'if name has been entered into text boxes, append it to the file
If (txtName.Text <> "") And (mtbPhone.Text <> "") Then
ReDim Preserve file(file.Count) 'add one more index to array
file(file.Count - 1) = txtName.Text + "," + mtbPhone.Text
Display()
Else
MessageBox.Show("Nothing to add")
End If
End Sub
Private Sub mnuDelete_Click(sender As Object, e As EventArgs) Handles
mnuDelete.Click
Dim nameToDelete As String = lstMembers.SelectedItem
'we're deleting, so array now holds one less name than is in the
current file
Dim tempName(lstMembers.Items.Count - 2) As String
If nameToDelete <> "" Then
'select name to delete
Dim query = From n In file
Let name As String = n.Split(",")(0)
Where name <> nameToDelete
Select n
'write the temp data into array and display new list
CODE
Public Class Form1
Dim file() As String = IO.File.ReadAllLines("Phones.txt")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
Display()
End Sub
Private Sub lstMembers_SelectedIndexChanged(sender As Object, e As
EventArgs) Handles lstMembers.SelectedIndexChanged
'loop through the file and find name that matches the selected text
in the list box
'and return the name and appropriate phone number
Dim query = From p In file
Let name As String = p.ToString.Split(",")(0)
Let phone = p.ToString.Split(",")(1)
Where name = lstMembers.SelectedItem
Select name, phone
txtName.Text = query.ToList.First.name.ToString
mtbPhone.Text = query.ToList.First.phone.ToString
End Sub
Private Sub mnuAdd_Click(sender As Object, e As EventArgs) Handles
mnuAdd.Click
'if name has been entered into text boxes, append it to the file
If (txtName.Text <> "") And (mtbPhone.Text <> "") Then
ReDim Preserve file(file.Count) 'add one more index to array
file(file.Count - 1) = txtName.Text + "," + mtbPhone.Text
Display()
Else
MessageBox.Show("Nothing to add")
End If
End Sub
Private Sub mnuDelete_Click(sender As Object, e As EventArgs) Handles
mnuDelete.Click
Dim nameToDelete As String = lstMembers.SelectedItem
'we're deleting, so array now holds one less name than is in the
current file
Dim tempName(lstMembers.Items.Count - 2) As String
If nameToDelete <> "" Then
'select name to delete
Dim query = From n In file
Let name As String = n.Split(",")(0)
Where name <> nameToDelete
Select n
'write the temp data into array and display new list