Thread: VB Code Help

    #1
  1. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    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)
  2. #2
  3. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Pleas Post your Code:
    As a hunch I believe:
    1. you are grabbing the wrong item in strName
    2. Try MessageBox.show(dlgbutton)
  4. #3
  5. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    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
  6. #4
  7. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    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.
  8. #5
  9. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0
    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)
  10. #6
  11. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    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
  12. #7
  13. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    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

  1. Replies: 0
    Last Post: November 2nd, 2010, 07:00 PM
  2. Code for reading xml code doesnt work anymore
    By littleNewbie in forum .NET Development
    Replies: 1
    Last Post: May 18th, 2009, 03:23 AM
  3. Code Bank code samples- make use of them
    By shamrog12 in forum ASP Development
    Replies: 2
    Last Post: August 28th, 2005, 01:19 AM
  4. Moving code to code behind file causing weird problem...
    By palomar in forum Visual Basic Programming
    Replies: 0
    Last Post: June 21st, 2005, 08:10 PM
  5. JavaScript prompt when code is used without [code] tags!
    By baseballdude_ in forum Suggestions & Feedback
    Replies: 9
    Last Post: April 9th, 2005, 02:07 PM

IMN logo majestic logo threadwatch logo seochat tools logo