|
-
Nov 4th, 2011, 12:45 AM
#1
Thread Starter
Lively Member
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
-
Nov 4th, 2011, 04:04 AM
#2
Re: Listbox: Display a name but also have a hidden price value
Hi..
I have made an example using Class:
vb.net Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'~~~ create an object of the "Fruit" class(by passing the name and price) and add that object to our Listbox
ListBox1.Items.Add(New Fruit("Orange", 5))
ListBox1.Items.Add(New Fruit("Grape", 1))
ListBox1.Items.Add(New Fruit("Apple", 9))
ListBox1.Items.Add(New Fruit("Pineapple", 4))
End Sub
'~~~ When an item in the ListBox is clicked, we will be showing the price of the respective fruit
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
MessageBox.Show(DirectCast(ListBox1.SelectedItem, Fruit).price.ToString)
End Sub
End Class
'our class that holds the price and name of fruit
Public Class Fruit
Public price As Integer
Public fruitname As String
Public Sub New()
End Sub
'~~~ constructor that will assign values that are being passed as argument
Public Sub New(ByVal n As String, ByVal p As Integer)
Me.fruitname = n
Me.price = p
End Sub
'~~~ This will display the fruitname in ListBox
Public Overrides Function ToString() As String
Return Me.fruitname ' Return Me.fruitname & " - $" & price.ToString
End Function
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,...
-
Nov 4th, 2011, 04:09 AM
#3
Thread Starter
Lively Member
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
-
Nov 4th, 2011, 04:17 AM
#4
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,...
-
Nov 4th, 2011, 04:22 AM
#5
Thread Starter
Lively Member
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
-
Nov 4th, 2011, 04:35 AM
#6
Re: Listbox: Display a name but also have a hidden price value
Remove all the occurrences of:
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,...
-
Nov 4th, 2011, 04:41 AM
#7
Thread Starter
Lively Member
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
-
Nov 4th, 2011, 01:31 PM
#8
Re: Listbox: Display a name but also have a hidden price value
 Originally Posted by redneckrider
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:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'create a listview, set View to "List" 'adding a book you can do this: Dim book As ListViewItem = New ListViewItem book.Text = "Put your book title here" book.SubItems.Add("19.99") ListView1.Items.Add(book) 'It will only display the book title, and keep the Price Associated with it. End Sub Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged 'now to get the amount: Dim bookprice As Decimal = Decimal.Parse(ListView1.Items(0).SubItems(1).Text) 'example Me.Text = bookprice End Sub
Last edited by stepdragon; Nov 4th, 2011 at 01:33 PM.
Reason: forgot the type on bookprice
-
Nov 4th, 2011, 10:40 PM
#9
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:
Public Class Form1 Dim taxrate = 0.06 Private Sub AddBook(ByVal Title As String, ByVal Price As Decimal) Dim book As ListViewItem = New ListViewItem book.Text = Title book.SubItems.Add(Price.ToString) ListView1.Items.Add(book) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'if there are no items in 'selecteditems' then they haven't selected anything, otherwise do this stuff If ListView1.SelectedItems.Count > 0 Then 'Make a copy of the item from the book list so it can be added to 'the cart list Dim book As ListViewItem = ListView1.SelectedItems.Item(0).Clone 'true/false to hold if you need to update quantity vs add new item Dim updatequant As Boolean = False 'go through each item in the cart. For Each item As ListViewItem In ListView2.Items 'if the item is already in the cart If item.Text = book.Text Then 'get the current quantity Dim tempquant As Integer = Integer.Parse(item.SubItems(2).Text) 'add one to the quantity tempquant += 1 'and update that item with the new quantity item.SubItems(2).Text = tempquant.ToString 'change the flag to true so we know that we 'updated the quantity rather than adding a 'new item updatequant = True End If Next 'if we didn't find the item then If updatequant = False Then 'add a quantity of 1 (as a string) book.SubItems.Add("1") 'and add it to the cart ListView2.Items.Add(book) End If 'update the shipping / tax / totals UpdateTotal() Else 'this is if they didn't choose an item in the box MsgBox("Please choose an item to add to the Cart", , "Add to Cart") End If End Sub Private Sub UpdateTotal() 'start the subtotal at zero 'we replace itemtotal rather than add to it 'so it doesn't need an initial value Dim subtotal As Decimal = 0 Dim itemtotal As Decimal 'go through each item in the cart For Each item As ListViewItem In ListView2.Items 'multiply the quantity and price of that item itemtotal = Decimal.Parse(item.SubItems(1).Text) * Decimal.Parse(item.SubItems(2).Text) 'and add it to the subtotal subtotal += itemtotal Next 'calculate tax Dim tax As Decimal = subtotal * taxrate 'you can put your own shipping calculator here Dim shipping As Decimal = 5.55 'whatever you choose 'add the subtotal, tax, and shipping to get the total Dim total As Decimal = subtotal + tax + shipping 'If you want to keep the totals to use later, you can change all these local variables 'to global variables, because if you try to get the data from the textboxes, they'll 'be rounded. I just like having the text boxes beautiful. Box_ST.Text = subtotal.ToString("c") Box_T.Text = tax.ToString("c") Box_Ship.Text = shipping.ToString("c") Box_Tot.Text = total.ToString("c") End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'if there is an item selected If ListView2.SelectedItems.Count > 0 Then 'if it has a quantity greater than one If Integer.Parse(ListView2.SelectedItems(0).SubItems(2).Text) > 1 Then 'then reduce the quantity ListView2.SelectedItems(0).SubItems(2).Text = Integer.Parse(ListView2.SelectedItems(0).SubItems(2).Text) - 1 Else 'otherwise, remove it completely ListView2.Items.Remove(ListView2.SelectedItems(0)) End If 'then update the totals UpdateTotal() End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 'this section should be fairly self explanitory Dim booktitle As String = InputBox("Book Title") For Each item As ListViewItem In ListView1.Items If item.Text = booktitle Then MsgBox("Sorry, but that book is already in the list") booktitle = Nothing End If Next If booktitle <> Nothing Then Dim bookprice As Decimal = InputBox("Price for " & booktitle) If bookprice <> Nothing Then AddBook(booktitle, bookprice) End If End If End Sub 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|