PG. 476 - 477 1. Membership List Write a menu-driven program to manage a members
ID: 671604 • Letter: P
Question
PG. 476 - 477 1. Membership List Write a menu-driven program to manage a membership list. See Figure 9.67 in "An Introduction to Programming Using Visual Basic 2012". Assume that the names and phone numbers of all members are stored in alphabetical order by (last name, then by first name) in the text file MemberPhones.txt.
Each record consists of two fields- a name field and a phone number field. The names should appear in a list box when the form is loaded. When a name is highlighted, both the name and phone number of the person should appear in the text boxes at the bottom of the form. To delete a person, highlight his or her name and click on the Delete menu item. To change either a person's name or phone number, make the corrections in the text boxes and click on the menu item Modify. To add anew member, type the person's name and phone number into the text boxes and click on the menu item Add. When the Exit menu item is clicked, the new membership list should be written to the file and the program should terminate.
Explanation / Answer
Menu-driven program code is below:-
Public Class Form1
Class Member
Public Name As String
Public Phone As String
End Class
Dim memberList As New List(Of Member)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sr() As String = IO.File.ReadAllLines("residents.TXT")
Dim memberline() As String
For j As Integer = 0 To sr.Count - 1
MemberLine = sr(j).Split(","c)
ListBox1.Items.Add(memberline(0))
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim sr() As String = IO.File.ReadAllLines("residents.TXT")
Dim theindex As String = ListBox1.SelectedItem
Dim list() As String
list = sr(theindex).Split(","c)
TextBox1.Text = list(0)
TextBox2.Text = list(1)
End Sub
Private Sub ModifyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModifyToolStripMenuItem.Click
memberList(ListBox1.SelectedIndex).Name = TextBox1.Text
memberList(ListBox1.SelectedIndex).Phone = TextBox2.Text
RefreshLstInfo()
End Sub
Private Sub AddToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddToolStripMenuItem.Click
Dim NewPerson As New Member
NewPerson.Name = TextBox1.Text
NewPerson.Phone = TextBox2.Text
ListBox1.Items.Add(TextBox1.Text & "," & TextBox2.Text)
RefreshLstInfo()
End Sub
Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
memberList.RemoveAt(ListBox1.SelectedIndex)
RefreshLstInfo()
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub RefreshLstInfo()
ListBox1.Items.Clear()
For j As Integer = 0 To memberList.Count - 1
ListBox1.Items.Add(memberList(j).Name)
Next
End Sub
End Class
One more program code for same menu-driven program project is below
Imports System.IO
Public Class frmMembership
Class Member
Public Name As String
Public Phone As String
End Class
Dim memberList As New List(Of Member)
Private Sub frmMembership_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sr As IO.StreamReader = IO.File.OpenText("MEMBERPHONES.TXT")
Do While (sr.Peek <> -1)
Dim th As New Member
th.Name = sr.ReadLine
th.Phone = sr.ReadLine
memberList.Add(th)
Loop
sr.Close()
For j As Integer = 0 To memberList.Count - 1
lstMembership.Items.Add(memberList(j).Name)
Next
End Sub
Private Sub lstMembership_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstMembership.SelectedIndexChanged
txtName.Text = memberList(lstMembership.SelectedIndex).Name
txtPhone.Text = memberList(lstMembership.SelectedIndex).Phone
End Sub
Private Sub mnuUpdateMod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuUpdateMod.Click
memberList(lstMembership.SelectedIndex).Name = txtName.Text
memberList(lstMembership.SelectedIndex).Phone = txtPhone.Text
RefreshLstInfo()
End Sub
Private Sub mnuUpdateAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuUpdateAdd.Click
Dim NewPerson As New Member
NewPerson.Name = txtName.Text
NewPerson.Phone = txtPhone.Text
memberList.Add(NewPerson)
RefreshLstInfo()
End Sub
Private Sub mnuUpdateDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuUpdateDel.Click
memberList.RemoveAt(lstMembership.SelectedIndex)
RefreshLstInfo()
End Sub
Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
Application.Exit()
End Sub
Private Sub RefreshLstInfo()
lstMembership.Items.Clear()
For j As Integer = 0 To memberList.Count - 1
lstMembership.Items.Add(memberList(j).Name)
Next
End Sub
End Class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.