- Total Members: 219,993
- Threads: 525,379
- Posts: 976,963
-
October 30th, 2012, 09:26 PM
#1
VB Code Help
My code is accepting a CD name, artist, and Price through padded input boxes. When the user attempts to remove a CD from a list in a txt file, I display a message box to verify they want to remove the cd. It should reed, Do you want to remove the (x) CD? where (x) is the name of the CD entered into the input box. Here's the code for my message box. Will somebody tell me what I'm doing wrong?
dlgButton =
MessageBox.Show("Do you want to remove the " & (strName) & "CD?",
"CD Collection", MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button2)
-
October 31st, 2012, 09:07 AM
#2
Pleas Post your Code:
As a hunch I believe:
1. you are grabbing the wrong item in strName
2. Try MessageBox.show(dlgbutton)
-
October 31st, 2012, 07:49 PM
#3
VB Help: Entire Code
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Private strName As String
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 line 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
' 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 40 spaces for the
' CD name, 25 spaces for the artist name,
' and 5 spaces for the price
Double.TryParse(strPrice, dblPrice)
strPrice = dblPrice.ToString("N2")
strConcatenatedInfo = strName.PadRight(40) &
strArtist.PadRight(25) & strPrice.PadLeft(5)
' add the information to the list box
lstCds.Items.Add(strConcatenatedInfo)
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
' verify the user wants to remove the CD information
Dim dlgButton As DialogResult
dlgButton =
MessageBox.Show("Do you want to remove the " & (strName) & "CD?",
"CD Collection", MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button2)
'If the Yes button was selected, remove the CD information
If dlgButton = DialogResult.Yes Then
lstCds.Items.RemoveAt(lstCds.SelectedIndex)
End If
End If
End Sub
End Class
-
October 31st, 2012, 08:24 PM
#4
VB Help: Almost There
I've gotten a little bit closer. I changed the message box to:
dlgButton =
MessageBox.Show("Do you want to remove the " & lstCds.Text & " CD?",
"CD Collection", MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button2)
It's displaying the CD name, artist, and price. I only want it to display the cd name.
-
October 31st, 2012, 08:44 PM
#5
I GOT IT! Thank you so much eyoung100. You pointed me in the right direction. Here is how I ended up wording my message box in case you want to know.
dlgButton =
MessageBox.Show("Do you want to remove the " & lstCds.Text.Substring(0, 40).Trim & " CD?",
"CD Collection", MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button2)
-
October 31st, 2012, 10:15 PM
#6

Originally Posted by
Candigirld
I GOT IT! Thank you so much eyoung100. You pointed me in the right direction. Here is how I ended up wording my message box in case you want to know.
dlgButton =
MessageBox.Show("Do you want to remove the " & lstCds.Text.Substring(0, 40).Trim & " CD?",
"CD Collection", MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button2)
Glad I could help, love the enthusiasm. You might want to add Dim Filepath As String = String.Concat(Application.Path, "\", "CDs.txt") to your File.IO, so that the CD's text file is more fluid:
Dim Filepath As String = String.Concat(Application.Path, "\", "CDs.txt")
' open the file for output
outFile = IO.File.CreateText(Filename)
' verify that the file exists
If IO.File.Exists(Filename) Then
' open the file for input
inFile = IO.File.OpenText(Filename)
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
-
October 31st, 2012, 10:17 PM
#7

Originally Posted by
eyoung100
Glad I could help, love the enthusiasm. You might want to add Dim Filepath As String = String.Concat(Application.Path, "\", "CDs.txt") to your File.IO, so that the CD's text file is more fluid:
Dim Filepath As String = String.Concat(Application.Path, "\", "CDs.txt")
' open the file for output
outFile = IO.File.CreateText(Filename)
' verify that the file exists
If IO.File.Exists(Filename) Then
' open the file for input
inFile = IO.File.OpenText(Filename)
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
Filename should be Filepath, caught my typo too late
Similar Threads
-
By RSS_News_User in forum Technology News
Replies: 0
Last Post: November 2nd, 2010, 06:00 PM
-
By littleNewbie in forum .NET Development
Replies: 1
Last Post: May 18th, 2009, 02:23 AM
-
By shamrog12 in forum ASP Development
Replies: 2
Last Post: August 28th, 2005, 12:19 AM
-
By palomar in forum Visual Basic Programming
Replies: 0
Last Post: June 21st, 2005, 07:10 PM
-
By baseballdude_ in forum Suggestions & Feedback
Replies: 9
Last Post: April 9th, 2005, 01:07 PM