I have a program where you have three entries, CD name, artist, price and it goes into a listbox and .txt file when closing. I have to have a message box if you enter the same CD name. I have a code to open the .txt file and compare strings but I need to change it to compare only the CD name and not all three entries. Is there a way to do my string compare against my listbox without opening the .txt file?? The program loads the listbox from the .txt file when opening.
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
' adds CD information to the list box
' declare variables
Dim strName As String
Dim strArtist As String
Dim strPrice As String
Dim strConcatenatedInfo As String
Dim dblPrice As Double
'get the CD information
strName = InputBox("CD name:", "CD Collection")
strArtist = InputBox("Artist:", "CD Collection")
strPrice = InputBox("Price:", "CD Collection")
'format the price, then concatenate the
'input items, using 48 spaces for the
'CD name, 32 spaces for the artist name,
'and 5 spaces for the price
Double.TryParse(strPrice, dblPrice)
strPrice = dblPrice.ToString("N2")
strConcatenatedInfo = strName.PadRight(48) & _
strArtist.PadRight(32) & strPrice.PadLeft(5)
'check to see if CD already exists
' declare variables
Dim infile As IO.StreamReader
Dim strInfo As String
Dim blnIsValid As Boolean = True 'to add CD to list if not already in file
'verify that the file exists
If IO.File.Exists("CDs.txt") Then
'open the file for input
infile = IO.File.OpenText("CDs.txt")
'process loop instructions until end of file
Do Until infile.Peek = -1
'read a line from the file
strInfo = infile.ReadLine
If strConcatenatedInfo.ToUpper Like strInfo.ToUpper Then 'to.upper to compare strings regardless of capitals
blnIsValid = False
MessageBox.Show("CD already in file", _
"CD Collection", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Loop
'close the file
infile.Close()
'select the first item in the list box
lstCds.SelectedIndex = 0
Else
MessageBox.Show("Can't find the CDs.txt file", _
"CD Collection", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
'add the information to the list box if not on list
If blnIsValid = True Then
lstCds.Items.Add(strConcatenatedInfo)
End If
' save the list box information
' declare a StreamWriter variable
Dim outFile As IO.StreamWriter
' open the file for output
outFile = IO.File.CreateText("CDs.txt")
' write each line in the list box
For intIndex As Integer = 0 To lstCds.Items.Count - 1
outFile.WriteLine(lstCds.Items(intIndex))
Next intIndex
' close the file
outFile.Close()
End Sub
| | Nice_Guy Sunday, November 29, 2009 10:28 PM | I think you are wanting to maketwo changes - 1. Compare the file name only 2. Compare the text box entry to the list box item, not to the text file lines. To compare the name only, use strName, not strConcatenatedInfo. For instance: If Trim(strName.ToUpper) = Trim(strInfo.ToUpper) Then ... However, without knowing the exact format of the text file lines, it's not possible to predict whether or not that will work. For the list box, the format of the lines is known - the name will be in the first 48 characters, because that's how the list box item was built. To compare to the list box rather than the file, replace the file read loop with a list box items loop:
For Each strInfo As String in lstCDs.Items
If Trim(strName.ToUpper) = _
Trim(strInfo.ToUpper.SubString(0,48)) Then
blnIsValid = False
MessageBox.Show("CD already in file", _
"CD Collection", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End If
Next
- Marked As Answer byNice_Guy Monday, November 30, 2009 12:35 AM
-
| | Acamar Sunday, November 29, 2009 11:25 PM | First thanks, the for/ next statement you gave meworks as needed but now when I try to add a new CD it will not populate the list box as before. I added an if statement for the next two messageboxentries, artist and price so they only come up if there is no name match. Could it be where I placed the for/next statement? I'll list the whole code & txt file this time.
CDs.txt file:
A Little Bit Longer Jonas Brothers 12.50
At Folsom Prison Johnny Cash 11.99
Covers James Taylor 11.99
Funhouse Pink 8.99
High School Musical 3: Senior Year Original Soundtrack 12.99
Jennifer Hudson Jennifer Hudson 12.99
Lucky Ols Sun Kenny Chesney 9.99
Soul Seal 10.99
test Bozo 4.99
code:
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
' save the list box information
' declare a StreamWriter variable
Dim outFile As IO.StreamWriter
' open the file for output
outFile = IO.File.CreateText("CDs.txt")
' write each line in the list box
For intIndex As Integer = 0 To lstCds.Items.Count - 1
outFile.WriteLine(lstCds.Items(intIndex))
Next intIndex
' close the file
outFile.Close()
End Sub
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' fills the list box with data
' stored in a sequential access file
' declare variables
Dim infile As IO.StreamReader
Dim strInfo As String
'verify that the file exists
If IO.File.Exists("CDs.txt") Then
'open the file for input
infile = IO.File.OpenText("CDs.txt")
'process loop instructions until end of file
Do Until infile.Peek = -1
'read a line from the file
strInfo = infile.ReadLine
'add the line to the list box
lstCds.Items.Add(strInfo)
Loop
'close the file
infile.Close()
'select the first item in the list box
lstCds.SelectedIndex = 0
Else
MessageBox.Show("Can't find the CDs.txt file", _
"CD Collection", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
' adds CD information to the list box
' declare variables
Dim strName As String
Dim strArtist As String
Dim strPrice As String
Dim strConcatenatedInfo As String
Dim dblPrice As Double
Dim blnIsValid As Boolean = True 'to add CD to list if not already in file
'get the CD information
strName = InputBox("CD name:", "CD Collection")
For Each strInfo As String In lstCds.Items
If Trim(strName.ToUpper) = _
Trim(strInfo.ToUpper.Substring(0, 48)) Then
blnIsValid = False
MessageBox.Show("CD already in file", _
"CD Collection", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End If
Next
If blnIsValid = True Then
strArtist = InputBox("Artist:", "CD Collection")
strPrice = InputBox("Price:", "CD Collection")
'format the price, then concatenate the
'input items, using 48 spaces for the
'CD name, 32 spaces for the artist name,
'and 5 spaces for the price
Double.TryParse(strPrice, dblPrice)
strPrice = dblPrice.ToString("N2")
strConcatenatedInfo = strName.PadRight(48) & _
strArtist.PadRight(32) & strPrice.PadLeft(5)
End If
End Sub
Private Sub btnRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemove.Click
'removes the selected line from the list box
'if a line is selected, remove the line
If lstCds.SelectedIndex <> -1 Then
lstCds.Items.RemoveAt(lstCds.SelectedIndex)
End If
End Sub
Private Sub lstCds_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCds.SelectedIndexChanged
End Sub
End Class- Marked As Answer byNice_Guy Monday, November 30, 2009 12:35 AM
-
| | Nice_Guy Monday, November 30, 2009 12:20 AM | I found my populate error I deleted my line to add to list box.
thanks again!!!!!!!!! - Marked As Answer byNice_Guy Monday, November 30, 2009 12:35 AM
-
| | Nice_Guy Monday, November 30, 2009 12:35 AM | I think you are wanting to maketwo changes - 1. Compare the file name only 2. Compare the text box entry to the list box item, not to the text file lines. To compare the name only, use strName, not strConcatenatedInfo. For instance: If Trim(strName.ToUpper) = Trim(strInfo.ToUpper) Then ... However, without knowing the exact format of the text file lines, it's not possible to predict whether or not that will work. For the list box, the format of the lines is known - the name will be in the first 48 characters, because that's how the list box item was built. To compare to the list box rather than the file, replace the file read loop with a list box items loop:
For Each strInfo As String in lstCDs.Items
If Trim(strName.ToUpper) = _
Trim(strInfo.ToUpper.SubString(0,48)) Then
blnIsValid = False
MessageBox.Show("CD already in file", _
"CD Collection", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End If
Next
- Marked As Answer byNice_Guy Monday, November 30, 2009 12:35 AM
-
| | Acamar Sunday, November 29, 2009 11:25 PM | First thanks, the for/ next statement you gave meworks as needed but now when I try to add a new CD it will not populate the list box as before. I added an if statement for the next two messageboxentries, artist and price so they only come up if there is no name match. Could it be where I placed the for/next statement? I'll list the whole code & txt file this time.
CDs.txt file:
A Little Bit Longer Jonas Brothers 12.50
At Folsom Prison Johnny Cash 11.99
Covers James Taylor 11.99
Funhouse Pink 8.99
High School Musical 3: Senior Year Original Soundtrack 12.99
Jennifer Hudson Jennifer Hudson 12.99
Lucky Ols Sun Kenny Chesney 9.99
Soul Seal 10.99
test Bozo 4.99
code:
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
' save the list box information
' declare a StreamWriter variable
Dim outFile As IO.StreamWriter
' open the file for output
outFile = IO.File.CreateText("CDs.txt")
' write each line in the list box
For intIndex As Integer = 0 To lstCds.Items.Count - 1
outFile.WriteLine(lstCds.Items(intIndex))
Next intIndex
' close the file
outFile.Close()
End Sub
Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' fills the list box with data
' stored in a sequential access file
' declare variables
Dim infile As IO.StreamReader
Dim strInfo As String
'verify that the file exists
If IO.File.Exists("CDs.txt") Then
'open the file for input
infile = IO.File.OpenText("CDs.txt")
'process loop instructions until end of file
Do Until infile.Peek = -1
'read a line from the file
strInfo = infile.ReadLine
'add the line to the list box
lstCds.Items.Add(strInfo)
Loop
'close the file
infile.Close()
'select the first item in the list box
lstCds.SelectedIndex = 0
Else
MessageBox.Show("Can't find the CDs.txt file", _
"CD Collection", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
' adds CD information to the list box
' declare variables
Dim strName As String
Dim strArtist As String
Dim strPrice As String
Dim strConcatenatedInfo As String
Dim dblPrice As Double
Dim blnIsValid As Boolean = True 'to add CD to list if not already in file
'get the CD information
strName = InputBox("CD name:", "CD Collection")
For Each strInfo As String In lstCds.Items
If Trim(strName.ToUpper) = _
Trim(strInfo.ToUpper.Substring(0, 48)) Then
blnIsValid = False
MessageBox.Show("CD already in file", _
"CD Collection", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End If
Next
If blnIsValid = True Then
strArtist = InputBox("Artist:", "CD Collection")
strPrice = InputBox("Price:", "CD Collection")
'format the price, then concatenate the
'input items, using 48 spaces for the
'CD name, 32 spaces for the artist name,
'and 5 spaces for the price
Double.TryParse(strPrice, dblPrice)
strPrice = dblPrice.ToString("N2")
strConcatenatedInfo = strName.PadRight(48) & _
strArtist.PadRight(32) & strPrice.PadLeft(5)
End If
End Sub
Private Sub btnRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemove.Click
'removes the selected line from the list box
'if a line is selected, remove the line
If lstCds.SelectedIndex <> -1 Then
lstCds.Items.RemoveAt(lstCds.SelectedIndex)
End If
End Sub
Private Sub lstCds_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCds.SelectedIndexChanged
End Sub
End Class- Marked As Answer byNice_Guy Monday, November 30, 2009 12:35 AM
-
| | Nice_Guy Monday, November 30, 2009 12:20 AM | I found my populate error I deleted my line to add to list box.
thanks again!!!!!!!!! - Marked As Answer byNice_Guy Monday, November 30, 2009 12:35 AM
-
| | Nice_Guy Monday, November 30, 2009 12:35 AM | You have somehow lost the lines that add the concatenated string to the list box. Originally it was
If blnIsValid = True Then
lstCds.Items.Add(strConcatenatedInfo)
End If
so you have to include the Add line in the True branch of the new blnIsValid test. It becomes:
'...
Double.TryParse(strPrice, dblPrice)
strPrice = dblPrice.ToString("N2")
strConcatenatedInfo = strName.PadRight(48) & _
strArtist.PadRight(32) & strPrice.PadLeft(5)
lstCds.Items.Add(strConcatenatedInfo)
End If
| | Acamar Monday, November 30, 2009 12:45 AM |
|