DPChallenge: A Digital Photography Contest You are not logged in. (log in or register
 

DPChallenge Forums >> General Discussion >> I need more Visual Basic Help
Pages:  
Showing posts 1 - 8 of 8, (reverse)
AuthorThread
03/27/2009 07:35:13 PM · #1
Here is the problem that I have to solve for school this week.

Write a menu-driven program to manage a membership list. Assume that the names and phone numbers of all members are stored in alphabetical order in the text file Memberphones.txt. The names and phone numbers should be read into an array of structures, and the names should be displayed in a list box when the form is loaded. When a name is highlighted, both the name and the phone number of the person should appear in the text boxes at the bottom of the screen. To delete a person, highlight his or her name and click on the Delete menu item. To change either the phone number or the spelling of the person's name, make the corrections in the text boxes, and click on the menu item Modify. To add a new member, type his or her name and phone number into the text boxes, and click on the menu item Add. When Exit is clicked, the new membership list should be written to the file and the program should terminate.



Ok, so I built the form, added the controls and started programing. I took the names from the file and put them into an array. I have no problem adding items to the file and reloading the array with the new information. Where I get stuck is trying to delete items. I can delete them from the listbox, but not from the text file.

Any idea on what approach I should be taking? My text book is of little help on this one.
03/27/2009 07:53:04 PM · #2
Make a new file containing everything except the deleted item, then delete the old file and rename the new one.

That might work.
03/27/2009 07:56:53 PM · #3
Edit: Too slow, Mick, you know what they say about great minds...

My experience with VB is limited to MS Excel. But I will give it a stab anyway.

Can you take the items in the array and generate a new text file, and then save the text file with same name as the original as to overwrite it?


Message edited by author 2009-03-27 19:57:49.
03/27/2009 08:05:59 PM · #4
I think that is how to do it, but if I do that I cannot figure out how to get the listbox item to correspond to the array number.
03/27/2009 08:07:58 PM · #5
don't you use the array to populate the listbox?
03/27/2009 08:38:21 PM · #6
You'll need to add some buttons (DELETE ETC...). This should give an idea on how to manipulate records in the list box. Let me know if you have any questions

Try this

RecordSet Object
Visual Basic uses an object called RecordSet to hold your table. To declare such an object and to open the table, do this:

Dim rsMyRS As RecordSet

Set rsMyRS = dbMyDB.OpenRecordSet("MyTable", dbOpenDynaset)

Accessing records

Dim rsMyRS As RecordSet

Private Sub Form_Load()

Set rsMyRS = dbMyDB.OpenRecordSet("MyTable", dbOpenDynaset)

If Not rsMyRS.EOF Then rsMyRS.MoveFirst
Do While Not rsMyRS.EOF
lstRecords.AddItem rsMyRS!Name
lstRecords.ItemData(lstRecords.NewIndex) = rsMyRS!ID
rsMyRS.MoveNext
Loop

End Sub

This will make the list box fill up with your records when the form loads.

Searching the RecordSet

Private Sub lstRecords_Click()

rsMyRS.FindFirst "ID=" & Str(lstRecords.ItemData(lstRecords.ListIndex))
txtPhone.Text = rsMyRS!Phone

End Sub

UPDATE
Private Sub cmdUpdate_Click()

rsMyRS.Edit
rsMyRS!Phone = txtPhone.Text
rsMyRS.Update

End Sub

DELETE
Private Sub cmdDelete_Click()

rsMyRS.Delete
lstRecords.RemoveItem lstRecords.ListIndex

End Sub

ADDING

Private Sub cmdNew_Click()

rsMyRS.AddNew
rsMyRS!Name = "A New Person"
lstRecords.AddItem rsMyRS!Name
lstRecords.ItemData(lstRecords.NewIndex) = rsMyRS!ID
rsMyRS!Phone = "Person's Phone Number"
rsMyRS.Update

End Sub

03/28/2009 07:11:38 PM · #7
Thanks for that post. I am going to give it a try tonight and see what happens.
03/29/2009 10:25:57 PM · #8
Here is what I came up with. I am 99% happy with it. 1% is driving me crazy.

Public Class Form1
'This is a structrue that will be used by the entire class.
Structure Membership
Dim name As String
Dim phone As String
End Structure
Dim UpperBound As Integer
Dim numLines As Integer

Dim nameTextBoxArray As Integer
Dim phoneTextBoxArray As Integer

Dim membershipInfo(99) As Membership

Dim outputFormat As String = "{0,-20}"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

loadMembershipArray()
displayMembershipArray()

'Display selected items in text boxes

End Sub
Private Sub ListBoxOutput_selectedtext() Handles ListBoxMembership.SelectedIndexChanged

'If an item from the list is selected the data from the array will
'be displayed in the textboxes.

nameTextBoxArray = ListBoxMembership.SelectedIndex
phoneTextBoxArray = ListBoxMembership.SelectedIndex

TextBoxName.Text = ListBoxMembership.SelectedItem
MaskedTextBoxPhone.Text = membershipInfo(phoneTextBoxArray).phone
'TextBoxPhone.Text = membershipInfo(phoneTextBoxArray).phone

End Sub

Sub loadMembershipArray()

'Open streamreader to read membership.txt file.

Dim sr As IO.StreamReader
'Handle a file not found error
If IO.File.Exists("membership.txt") Then
sr = IO.File.OpenText("membership.txt")

'load the array if there is a blank file then display nothing and wait for
'items to be inputed into the storage file.
If (sr.Peek <> -1) Then
displayMembershipArray()
End If

'compute the number of lines in the text file.
Do While (sr.Peek <> -1)
sr.ReadLine()
sr.ReadLine()
numLines += 1
Loop
sr.Close()
UpperBound = numLines - 1

'Read the textfile and create the structured array that holds the values.
sr = IO.File.OpenText("membership.txt")
For numberMember As Integer = 0 To 20
membershipInfo(numberMember).name = sr.ReadLine
membershipInfo(numberMember).phone = sr.ReadLine
Next
sr.Close()

'If the above doesn't happen this will.
Else
Dim message As String
Dim title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult

message = "The file " & "membership.txt" & " was not found. "
message &= "If you would like to continue and create a blank file click yes. "
message &= "If you would like to exit the program click no. "

style = MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Critical Or MsgBoxStyle.YesNo

title = "File Not Found"

response = MsgBox(message, style, title)

If response = MsgBoxResult.Yes Then

Dim sw As IO.StreamWriter = IO.File.CreateText("membership.txt")
sw.Close()

Else
'end program
End

End If

End If

End Sub

Sub displayMembershipArray()
ListBoxMembership.Items.Clear()
'This sub clears the listbox items and then displays the contents of the array
'It is called anytime a change to the list hase been processed.

For numberMember As Integer = 0 To UpperBound
For index As Integer = 1 To UpperBound '- numberMember
If (membershipInfo(index - 1).name > membershipInfo(index).name) Then
SwapData(index)
Else
End If

Next
Next

'Dislplay contents of array afer sort
For numberMember As Integer = 0 To UpperBound
ListBoxMembership.Items.Add(String.Format(outputFormat, membershipInfo(numberMember).name))
Next

End Sub

Sub SwapData(ByVal numberMember As Integer)

'Bubble sort array
'This sub procedure is called through the displayMemberShipArray sub
'Call this to sort the array

Dim temp As Membership
temp = membershipInfo(numberMember - 1)
membershipInfo(numberMember - 1) = membershipInfo(numberMember)
membershipInfo(numberMember) = temp

End Sub

Private Sub AddToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddToolStripMenuItem.Click
'When the add command has been given, the contents of the text boxes are added to the array.

Dim name As String
Dim phone As String

name = TextBoxName.Text
phone = MaskedTextBoxPhone.Text
UpperBound += 1

membershipInfo(UpperBound).name = name
membershipInfo(UpperBound).phone = phone

displayMembershipArray()

End Sub

Private Sub ModifyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModifyToolStripMenuItem.Click
'any modifications to the information is processed by this sub procedure.

Dim newName As String
Dim newPhone As String

newName = TextBoxName.Text
newPhone = MaskedTextBoxPhone.Text

membershipInfo(nameTextBoxArray).name = newName
membershipInfo(phoneTextBoxArray).phone = newPhone

displayMembershipArray()

End Sub

Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
'If a member is to be deleted the array values are given the nothing value.

membershipInfo(nameTextBoxArray).name = Nothing
membershipInfo(phoneTextBoxArray).phone = Nothing

displayMembershipArray()

End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
'Upon exit the user is prompted to determine if they would like to save the changes.
'A yes answer will make the changes and close the form.
'A no answer will not make any changes to the text file and will close the form.

Dim message As String
Dim title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult

message = "Would you like to save before exiting?"
style = MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Critical Or MsgBoxStyle.YesNo

title = "Save Before Exiting"

response = MsgBox(message, style, title)

If response = MsgBoxResult.Yes Then

saveFile()
'end program
End

Else
'end program
End

End If

End Sub
Sub saveFile()

'This is the sub that writes to the file.

Dim sw As IO.StreamWriter = IO.File.CreateText("membership.txt")

For writeFile As Integer = 0 To UpperBound
If membershipInfo(writeFile).name = Nothing Then
Else
sw.WriteLine(membershipInfo(writeFile).name)
End If
If membershipInfo(writeFile).phone = Nothing Then
Else
sw.WriteLine(membershipInfo(writeFile).phone)
End If
Next
sw.Close()

End Sub

Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click

'call saveFile sub to save the file as it is currently.
saveFile()

End Sub
End Class

Pages:  
Current Server Time: 04/19/2024 04:28:47 PM

Please log in or register to post to the forums.


Home - Challenges - Community - League - Photos - Cameras - Lenses - Learn - Prints! - Help - Terms of Use - Privacy - Top ^
DPChallenge, and website content and design, Copyright © 2001-2024 Challenging Technologies, LLC.
All digital photo copyrights belong to the photographers and may not be used without permission.
Current Server Time: 04/19/2024 04:28:47 PM EDT.