Results 1 to 9 of 9

Thread: Listbox: Display a name but also have a hidden price value

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    69

    Listbox: Display a name but also have a hidden price value

    Hello,

    So I have an issue with have displayed names in a listbox and trying to write a code to have a hidden price value for it. I want the selected name in the be put into another listbox as a display name and have the price value of it be in a holding value to calculate an equation.


    I just need help declaring the name with a value in the list box.
    And How do I list each listbox number so I can retrieve it back as a price value to put in a formula. Thank you.


    Code:
      Private Sub btnAudioAddCart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAudioAddCart.Click
    
    
    Const decPrintIDidIt As Decimal = 11.95D 'Price of Print Book "I Did It Your Way"
        Const decPrintHistory As Decimal = 14.5D 'Price of Print Book "The History of Scotland"
            Dim intAudioInput As Integer 'Holds the selected Printed Book
    
    
            For intAudioInput = 0 To lstAudio.Items.Count = -1
                If lstAudio.SelectedIndex = -1 Then
                    'No book is selected
                    MessageBox.Show("Select A book.")
                Else : lstAudio.GetSelected(intAudioInput)
                    MainForm.lstProducts().Items.Add(lstAudio.SelectedItem)
                    'Gets the Selected book
                End If
            Next
        End Sub
    
    End Class

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Listbox: Display a name but also have a hidden price value

    Hi..

    I have made an example using Class:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.  
    5.         '~~~ create an object of the "Fruit" class(by passing the name and price) and add that object to our Listbox
    6.         ListBox1.Items.Add(New Fruit("Orange", 5))
    7.         ListBox1.Items.Add(New Fruit("Grape", 1))
    8.         ListBox1.Items.Add(New Fruit("Apple", 9))
    9.         ListBox1.Items.Add(New Fruit("Pineapple", 4))
    10.  
    11.     End Sub
    12.  
    13.     '~~~ When an item in the ListBox is clicked, we will be showing the price of the respective fruit
    14.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    15.         MessageBox.Show(DirectCast(ListBox1.SelectedItem, Fruit).price.ToString)
    16.     End Sub
    17. End Class
    18.  
    19. 'our class that holds the price and name of fruit
    20. Public Class Fruit
    21.     Public price As Integer
    22.     Public fruitname As String
    23.  
    24.     Public Sub New()
    25.  
    26.     End Sub
    27.  
    28.     '~~~ constructor that will assign values that are being passed as argument
    29.     Public Sub New(ByVal n As String, ByVal p As Integer)
    30.         Me.fruitname = n
    31.         Me.price = p
    32.     End Sub
    33.  
    34.     '~~~ This will display the fruitname in ListBox
    35.     Public Overrides Function ToString() As String
    36.         Return Me.fruitname     ' Return Me.fruitname & " - $" & price.ToString
    37.     End Function
    38. End Class

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    69

    Re: Listbox: Display a name but also have a hidden price value

    starting with line 6. I get the error "Too many arguments to 'Pub Sub New()' why is that

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Listbox: Display a name but also have a hidden price value

    I didn't find any problems with the code and I have tested it before posting it. That error would be shown when a constructor is not there that accepts those arguments ! But my code already have a constructor that accepts the arguments !!

    Can you show me the whole code that you have copy-pasted from the above post.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    69

    Re: Listbox: Display a name but also have a hidden price value

    When I after the coding I get a weird Windows phrase after the text. Here's a picture. Do you know why?




    Code:
    Public Class listItem
        Public display As String
        Public value As Decimal
        Public Overrides Function ToString() As String
            Return Me.display
        End Function
    End Class
    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            lstPrint.Items.Add(New listItem With {.display = "I Did It Your Way (Print)" & ToString(), .value = 11.95})
            lstPrint.Items.Add(New listItem With {.display = "The History of Scotland (Print)" & ToString(), .value = 14.5})
            lstPrint.Items.Add(New listItem With {.display = "Learn Calculus in One Day (Print)" & ToString(), .value = 29.95})
            lstPrint.Items.Add(New listItem With {.display = "Feel the Stress (Audio)" & ToString(), .value = 18.5})
            MainForm.Show()
    
        End Sub
    
    
        Private Sub lstPrint_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPrint.SelectedIndexChanged
            Dim ItemSelected As Decimal
            ItemSelected = CType(lstPrint.SelectedItem,  _
                listItem).value
        End Sub

  6. #6
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Listbox: Display a name but also have a hidden price value

    Remove all the occurrences of:
    Code:
    & ToString()

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2011
    Posts
    69

    Re: Listbox: Display a name but also have a hidden price value

    Ok I have another question
    So my question is that I have 2 list boxes in two forms aside from the main one. and I am trying to transfer what I choose in the side form to transfer to the list box. Now I have the integer values but they are not displayed. So I want them to all add up but since they are all listed in the listItem variable, how do I add them up together, no matter how many of one book I do. Also, how do get the count of books decided as well?


    [CODE]Here are some pictures and codes

    Main:

    Print:

    Audio:



    Code:
    Public Class MainForm
    
        Dim frmAudioBooks As New AudioBooks
        Dim frmPrintedBooks As New PrintedBooks
        Const decTaxRate As Decimal = 0.06D ' Tax rate for Books
        Const decShipping As Decimal = 2D ' Shipping Rate per Book
        Const decPrintIDidIt As Decimal = 11.95D 'Price of Print Book "I Did It Your Way"
        Const decPrintHistory As Decimal = 14.5D 'Price of Print Book "The History of Scotland"
        Const decPrintCalculus As Decimal = 29.95D 'Price of Print Book "Learn Calculus in One Day"
        Const decPrintStress As Decimal = 18.5D 'Price of Print Book "Feel the Stress"
        Const decAudioCaluclus As Decimal = 29.95D 'Price of Audio Book "Learn Calculus in One Day"
        Const decAudioHistory As Decimal = 14.5D 'Price of Audio Book "The History of Scotland" 
        Const decAudioScience As Decimal = 12.95D 'Price of Audio Book "The Science of Body Language"
        Const decAudioRelax As Decimal = 11.5D 'Price of Audio Book "Relaxation Techniques
    
    
        Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
            'Close the Form
            Me.Close()
        End Sub
    
        Private Sub mnuHelpAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHelpAbout.Click
            'Display a short description of the program
            MessageBox.Show("The program executes a shopping menu for the user to choose from many books both audio and printed and let's them pick from multiple books to check and buy. Designed by Justin Guan")
        End Sub
    
        Private Sub mnuProductsPrintedBooks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuProductsPrintedBooks.Click
            frmPrintedBooks.Show()
        End Sub
    
        Private Sub mnuProductsAudioBooks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuProductsAudioBooks.Click
            frmAudioBooks.Show()
        End Sub
    
        Private Sub mnuFileReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileReset.Click
            lstProducts.Items.Clear()
        End Sub
    
    End Class
    Code:
    Public Class AudioBooks
        Private Sub btnAudioClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAudioClose.Click
            Me.Close()
        End Sub
    
        Private Sub btnAudioAddCart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAudioAddCart.Click
    
    
    
            Dim intAudioInput As Integer 'Holds the selected Printed Book
    
    
            For intAudioInput = 0 To lstAudio.Items.Count = -1
                If lstAudio.SelectedIndex = -1 Then
                    'No book is selected
                    MessageBox.Show("Select A book.")
                Else : lstAudio.GetSelected(intAudioInput)
                    MainForm.lstProducts().Items.Add(lstAudio.SelectedItem)
                    'Gets the Selected book
                End If
            Next
        End Sub
    
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
            lstAudio.Items.Add(New listItem With {.display = "Learn Calculus in One Day (Audio)", .value = 29.95})
            lstAudio.Items.Add(New listItem With {.display = "The History of Scotland (Audio)", .value = 14.5})
            lstAudio.Items.Add(New listItem With {.display = "The Science of Body Language (Audio)", .value = 12.95})
            lstAudio.Items.Add(New listItem With {.display = "Relaxation Techniques (Audio)", .value = 11.5})
    
            MainForm.Show()
    
        End Sub
    
        Private Sub lstAudio_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstAudio.SelectedIndexChanged
            Dim ItemSelected As Decimal
            ItemSelected = CType(lstAudio.SelectedItem,  _
                listItem).value
        End Sub
    End Class
    Code:
    Public Class PrintedBooks
    
        Private Sub btnPrintAddCart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintAddCart.Click
    
            Dim decPrintInput As Decimal 'Holds the selected Printed Book
    
            For decPrintInput = 0 To lstPrint.Items.Count = -1
                If lstPrint.SelectedIndex = -1 Then
                    MessageBox.Show("Select a book.")
                    'No book is selected
                Else : lstPrint.GetSelected(decPrintInput)
                    MainForm.lstProducts().Items.Add(lstPrint.SelectedItem)
                End If
    
    
            Next
    
    
        End Sub
    
        Private Sub btnPrintClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintClose.Click
            Me.Close()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            lstPrint.Items.Add(New listItem With {.display = "I Did It Your Way (Print)", .value = 11.95})
            lstPrint.Items.Add(New listItem With {.display = "The History of Scotland (Print)", .value = 14.5})
            lstPrint.Items.Add(New listItem With {.display = "Learn Calculus in One Day (Print)", .value = 29.95})
            lstPrint.Items.Add(New listItem With {.display = "Feel the Stress (Print)", .value = 18.5})
            MainForm.Show()
    
        End Sub
    
    
        Private Sub lstPrint_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPrint.SelectedIndexChanged
            Dim ItemSelected As Decimal
            ItemSelected = CType(lstPrint.SelectedItem,  _
                listItem).value
        End Sub
    End Class

  8. #8
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: Listbox: Display a name but also have a hidden price value

    Quote Originally Posted by redneckrider View Post
    I just need help declaring the name with a value in the list box.
    And How do I list each listbox number so I can retrieve it back as a price value to put in a formula. Thank you.
    If that's all you want to do, then you can simply swap your listbox for a listview.

    vb.net Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'create a listview, set View to "List"
    3.  
    4.         'adding a book you can do this:
    5.         Dim book As ListViewItem = New ListViewItem
    6.         book.Text = "Put your book title here"
    7.         book.SubItems.Add("19.99")
    8.  
    9.         ListView1.Items.Add(book)
    10.  
    11.         'It will only display the book title, and keep the Price Associated with it.
    12.     End Sub
    13.  
    14.     Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
    15.         'now to get the amount:
    16.         Dim bookprice As Decimal = Decimal.Parse(ListView1.Items(0).SubItems(1).Text)
    17.  
    18.         'example
    19.         Me.Text = bookprice
    20.     End Sub
    Last edited by stepdragon; Nov 4th, 2011 at 01:33 PM. Reason: forgot the type on bookprice

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

  9. #9
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: Listbox: Display a name but also have a hidden price value

    Sorry, when I posted my last post I was just leaving for work, and didn't have time to make a nice well thought out explanation. Here's to that:

    Here's a simple book shopping app, based on what I read about what you wanted. It should be fairly simple to understand and adapt to what you specifically need. I'm using listviews instead of listboxes, as they add much more functionality.

    First, here's a picture of my form layout:



    and here's the code. Try it out. I think you'll like how it works.

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Dim taxrate = 0.06
    4.  
    5.     Private Sub AddBook(ByVal Title As String, ByVal Price As Decimal)
    6.         Dim book As ListViewItem = New ListViewItem
    7.         book.Text = Title
    8.         book.SubItems.Add(Price.ToString)
    9.  
    10.         ListView1.Items.Add(book)
    11.     End Sub
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.         'if there are no items in 'selecteditems' then they haven't selected anything, otherwise do this stuff
    15.         If ListView1.SelectedItems.Count > 0 Then
    16.             'Make a copy of the item from the book list so it can be added to
    17.             'the cart list
    18.             Dim book As ListViewItem = ListView1.SelectedItems.Item(0).Clone
    19.  
    20.             'true/false to hold if you need to update quantity vs add new item
    21.             Dim updatequant As Boolean = False
    22.  
    23.             'go through each item in the cart.
    24.             For Each item As ListViewItem In ListView2.Items
    25.                 'if the item is already in the cart
    26.                 If item.Text = book.Text Then
    27.                     'get the current quantity
    28.                     Dim tempquant As Integer = Integer.Parse(item.SubItems(2).Text)
    29.                     'add one to the quantity
    30.                     tempquant += 1
    31.                     'and update that item with the new quantity
    32.                     item.SubItems(2).Text = tempquant.ToString
    33.                     'change the flag to true so we know that we
    34.                     'updated the quantity rather than adding a
    35.                     'new item
    36.                     updatequant = True
    37.                 End If
    38.             Next
    39.  
    40.             'if we didn't find the item then
    41.             If updatequant = False Then
    42.                 'add a quantity of 1 (as a string)
    43.                 book.SubItems.Add("1")
    44.                 'and add it to the cart
    45.                 ListView2.Items.Add(book)
    46.             End If
    47.  
    48.             'update the shipping / tax / totals
    49.             UpdateTotal()
    50.  
    51.         Else
    52.             'this is if they didn't choose an item in the box
    53.             MsgBox("Please choose an item to add to the Cart", , "Add to Cart")
    54.         End If
    55.     End Sub
    56.  
    57.     Private Sub UpdateTotal()
    58.         'start the subtotal at zero
    59.         'we replace itemtotal rather than add to it
    60.         'so it doesn't need an initial value
    61.         Dim subtotal As Decimal = 0
    62.         Dim itemtotal As Decimal
    63.  
    64.         'go through each item in the cart
    65.         For Each item As ListViewItem In ListView2.Items
    66.  
    67.             'multiply the quantity and price of that item
    68.             itemtotal = Decimal.Parse(item.SubItems(1).Text) * Decimal.Parse(item.SubItems(2).Text)
    69.             'and add it to the subtotal
    70.             subtotal += itemtotal
    71.  
    72.         Next
    73.  
    74.         'calculate tax
    75.         Dim tax As Decimal = subtotal * taxrate
    76.  
    77.         'you can put your own shipping calculator here
    78.         Dim shipping As Decimal = 5.55 'whatever you choose
    79.  
    80.         'add the subtotal, tax, and shipping to get the total
    81.         Dim total As Decimal = subtotal + tax + shipping
    82.  
    83.  
    84.         'If you want to keep the totals to use later, you can change all these local variables
    85.         'to global variables, because if you try to get the data from the textboxes, they'll
    86.         'be rounded. I just like having the text boxes beautiful.
    87.         Box_ST.Text = subtotal.ToString("c")
    88.         Box_T.Text = tax.ToString("c")
    89.         Box_Ship.Text = shipping.ToString("c")
    90.         Box_Tot.Text = total.ToString("c")
    91.  
    92.     End Sub
    93.  
    94.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    95.         'if there is an item selected
    96.         If ListView2.SelectedItems.Count > 0 Then
    97.             'if it has a quantity greater than one
    98.             If Integer.Parse(ListView2.SelectedItems(0).SubItems(2).Text) > 1 Then
    99.                 'then reduce the quantity
    100.                 ListView2.SelectedItems(0).SubItems(2).Text = Integer.Parse(ListView2.SelectedItems(0).SubItems(2).Text) - 1
    101.             Else
    102.                 'otherwise, remove it completely
    103.                 ListView2.Items.Remove(ListView2.SelectedItems(0))
    104.             End If
    105.  
    106.             'then update the totals
    107.             UpdateTotal()
    108.         End If
    109.     End Sub
    110.  
    111.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    112.  
    113.         'this section should be fairly self explanitory
    114.  
    115.         Dim booktitle As String = InputBox("Book Title")
    116.  
    117.         For Each item As ListViewItem In ListView1.Items
    118.             If item.Text = booktitle Then
    119.                 MsgBox("Sorry, but that book is already in the list")
    120.                 booktitle = Nothing
    121.             End If
    122.         Next
    123.  
    124.         If booktitle <> Nothing Then
    125.  
    126.             Dim bookprice As Decimal = InputBox("Price for " & booktitle)
    127.  
    128.             If bookprice <> Nothing Then
    129.                 AddBook(booktitle, bookprice)
    130.             End If
    131.  
    132.         End If
    133.  
    134.     End Sub
    135. End Class

    Anyways, I hope this helps you get to where you want with your program!
    Last edited by stepdragon; Nov 4th, 2011 at 10:52 PM. Reason: Added Comments to the code

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width