Discussion Locked

Help in VB not do my work for me.

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am writing code for visual basic that needs when member name is clicked that their phone number is also shown in the textbox. I am not getting that I am getting their index number in the text file. Some one please explain why my code is messed up while I have been learning from Schneider's textbook. Thank-you as I want to get better at programming.

Imports System.IO
Public Class frmMembership

Structure Members
Dim sr As IO.StreamReader 'sets variables throughout the program
Dim sw As IO.StreamWriter
Dim Name As String
Dim Numbers As String
Dim fromFile As String
Dim toFile As String

End Structure

Dim sr As IO.StreamReader 'sets variables throughout the program
Dim sw As IO.StreamWriter
Dim identification(15) As Members
Dim fromFile As String
Dim toFile As String

Private Sub frmMembers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Assume that all numbers are from MEMBERPHONES.TEXT

Dim sr As IO.StreamReader = IO.File.OpenText("MEMBERPHONES.TXT")

For i As Integer = 0 To 15
identification(i).Name = sr.ReadLine
identification(i).Numbers = sr.ReadLine

Next
sr.Close()

SortNumbers()
ShowNumbers()
End Sub
Sub ShowNumbers()
'Display a nice orderly list
Dim fmtStr As String = "{0, -13}"
lstMembers.Items.Clear()
For i As Integer = 8 To 15

lstMembers.Items.Add(String.Format(fmtStr, identification(i).Name))
Next
End Sub

Sub SortNumbers()
'Bubble sort table in ascending order by alphabetical order
For passAlphabetically As Integer = 0 To 15
For index As Integer = 1 To 15 - passAlphabetically
If (identification(index - 1).Name > identification(index).Name) Then
SwapData(index)
End If
Next
Next
End Sub
Sub SwapData(ByVal index As Integer)
'Swap(entries)
Dim temp As Members
temp = identification(index - 1)
identification(index - 1) = identification(index)
identification(index) = temp
End Sub
Dim item As String
Private Sub AddToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddToolStripMenuItem.Click

item = txtPhone.Text
item = txtName.Text
lstMembers.Items.Add(item)
End Sub

Private Sub lstMembers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstMembers.SelectedIndexChanged

Dim fmtStr As String = "{0, 3}"

For i As Integer = 8 To 15

Next
For i As Integer = 0 To 15
txtName.Text = CStr(lstMembers.SelectedItem)
txtPhone.Text = CInt(lstMembers.SelectedIndex)
Next
End Sub

Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
lstMembers.Items.RemoveAt(lstMembers.SelectedIndex)
txtName.Clear() 'This deletes what is in the textbox after it is called
End Sub

Private Sub SaveChangesAndExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveChangesAndExitToolStripMenuItem.Click
Application.Exit()
End Sub

Private Sub ModifyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModifyToolStripMenuItem.Click
Dim Line As String
fromFile = ("MEMBERPHONES.TXT")
toFile = ("thisFile.TXT")
sr = IO.File.OpenText(fromFile)
sw = IO.File.CreateText(toFile)
Do While (sr.Peek() <> -1)
line = sr.ReadLine()
If (txtName.Text <> "") Then 'gave me the most trouble but after reading more I realized that this was to loop until a stock was found.
toFile = line
sw.WriteLine(toFile)
Else
toFile = txtPhone.Text
sw.WriteLine(toFile)
End If
Loop
sr.Close()
sw.Close()
IO.File.Delete("MEMBERPHONES.TXT")
IO.File.Move("thisFile.TXT", "MEMBERPHONES.TXT")
IO.File.Delete("thisFile.TXT") 'deletes the file after it was used
txtName.Clear()
txtPhone.Clear() 'clears textboxes
End Sub
End Class

2 replies

by slayer_

Try wrapping your code in < pre > tags, so that the tabbing remains. makes it easier for us to read it.
Like this

Private sub helloworld
     dim sTemp as string
     sTemp = inputbox("Hello world")
     If sTemp="hello" then
          call msgbox("Welcome home")
     Else
          call msgbox("You are not welcome here")
     End If
End Sub

by tony hopkinson

xtPhone.Text = CInt(lstMembers.SelectedIndex)

but lstmembers.selectedIndex is the index of the currently selected item, you should be using that to locate the phone number in the memberlist which presumably is something like
0 Fred 999999
1 Bill 88888
etc
So if Bill is is name part showing in names list box, selectedIndex is one which you then use to look up bills phone number...

Personally I suggest you choose a different language to learn to code with, one that will tell you where you've gone wrong instead of taking a wild guess at what you might have meant...
Python, C#, C, Pascal even VB.net, anything but VB6.

This discussion is closed to new replies.