By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,133 Members | 1,113 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,133 IT Pros & Developers. It's quick & easy.

Listbox is selected the name and corresponding phone number should be should

P: 3
Basically i want to design a membership Name and Telephone List form using the following command buttons.

Edit
Add New;
Update;
Delete;
Cancel;
Save;
Clear, and
Exit

The names and phone numbers of members are stored in a sequential text file called MEMBERS.TXT which i have created.

When the program is loaded, names and telephone numbers should be read from the file and stored in an array or arrays in memory and then, from memory, the names and telephone numbers are displayed in alphabetical names sequence in a list box.


I used two textboxs to hold the Name and the other to hold the Number

When a name in the listbox is selected (clicked) the name and corresponding phone number should be should be displayed in the textboxes but i cant manage to do this.

To change any fields, the user first uses the Edit button to enable the textboxes to be changed.

The user makes the amendments to the textboxes and uses the update button to effect the change in memory. I need your help on this too.

To delete an item, Delete is used. But i dont know how to do this using sequantial Files. Thanks for your help.

Code sofar

Public Class MembersForm

Private Sub ExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExitButton.Click
Close()
End Sub

Private Sub MembersForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim intButton As Integer, objStreamWriter As System.IO.StreamWriter

'display the form immediately
Me.Show()
'determine if the user wants to create a new file
intButton = MessageBox.Show("Create new File?", "Members Phone List", _
MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2)
'create new sequential acces file, if necessary
If intButton = Windows.Forms.DialogResult.Yes Then
objStreamWriter = System.IO.File.CreateText("Members.txt")
objStreamWriter.Close()
Else
objStreamWriter = System.IO.File.AppendText("Members.txt")
objStreamWriter.Close()
End If

Dim strMembers As String, objStreamReader As System.IO.StreamReader
'load members to listbox
If System.IO.File.Exists("Members.txt") = True Then
objStreamReader = System.IO.File.OpenText("Members.txt")
Do While objStreamReader.Peek <> -1
strMembers = objStreamReader.ReadLine()
Me.MembersListBox.Items.Add(strMembers)
Loop
objStreamReader.Close()
Else
MessageBox.Show("File does not exits.", "Members Phone List", _
MessageBoxButtons.OK, MessageBoxIcon.Information)

End If
End Sub

Private Sub AddButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddButton.Click

Dim objStreamWriter As System.IO.StreamWriter
objStreamWriter = System.IO.File.AppendText("Members.txt")

objStreamWriter.Write(Me.NameTextBox.Text.PadRight (30))
'objStreamWriter.Write(Space(5))
objStreamWriter.WriteLine(Me.NumberTextBox.Text.Pa dRight(10))
'objStreamWriter.WriteLine()
objStreamWriter.Close()

Me.NameTextBox.Text = ""
Me.NumberTextBox.Text = ""
Me.NameTextBox.Focus()

End Sub

Private Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
Dim strMembers As String, objStreamReader As System.IO.StreamReader
'update members to memberslistbox
If System.IO.File.Exists("Members.txt") = True Then
objStreamReader = System.IO.File.OpenText("Members.txt")
Me.MembersListBox.Items.Clear()
Do While objStreamReader.Peek <> -1
strMembers = objStreamReader.ReadLine()
Me.MembersListBox.Items.Add(strMembers)
Loop
objStreamReader.Close()
Me.MembersListBox.SelectedIndex = 0

End If
End Sub

Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveButton.Click
Dim objStreamWriter As System.IO.StreamWriter
objStreamWriter = System.IO.File.AppendText("Members.txt")

objStreamWriter.Write(Me.NameTextBox.Text.PadRight (30))
objStreamWriter.Write(Space(5))
objStreamWriter.Write(Me.NumberTextBox.Text.PadLef t(10))
objStreamWriter.WriteLine()
objStreamWriter.Close()

Me.NameTextBox.Text = ""
Me.NumberTextBox.Text = ""
Me.NameTextBox.Focus()
End Sub

Private Sub MembersListBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MembersListBox.Click

Me.NameTextBox.Text = MembersListBox.SelectedItem()

End Sub

End Class
Jan 17 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.