[2005]Accept Variable in Listbox index
Hello All,
I have an issue that has been plaguing me for some time now and I have gone at it with all of the limited knowledge that I possess and I can't figure out how to pull the value that is stored in a certain index of a listbox.
Let me walk you through what I have so far.
I have a simple form with:
3 listboxes: LisWorkShop, lisLocation, & LisCost
4 buttons: Only two worth noting at this point - btnWorkShop & btnCalculate
1 label: lblTotalCost
The idea behind this program is that you select a training that you would like to take from the LisWorkShop list and click btnWorkShop to add the cost to LisCost. This is the code I have for btnWorkShop:
Code:
If LisWorkshop.SelectedIndex = -1 Then
MessageBox.Show("Please select a training session")
ElseIf LisWorkshop.SelectedIndex = 0 Then
LisCost.Items.Add(FormatCurrency(595))
ElseIf LisWorkshop.SelectedIndex = 1 Then
LisCost.Items.Add(FormatCurrency(695))
ElseIf LisWorkshop.SelectedIndex = 2 Then
LisCost.Items.Add(FormatCurrency(995))
ElseIf LisWorkshop.SelectedIndex = 3 Then
LisCost.Items.Add(FormatCurrency(1295))
ElseIf LisWorkshop.SelectedIndex = 4 Then
LisCost.Items.Add(FormatCurrency(395))
End If
I know I have the FormatCurrency listed for each statement, I will correct that once I have the code locked down.
In the second List box, LisLocation, I have a list of cities in which the training is offered. On click in LisLocation the amount of lodging is moved into LisCost. The code I have to do this is the following:
Code:
If LisLocation.SelectedIndex = 0 Then
LisCost.Items.Add(FormatCurrency(95))
ElseIf LisLocation.SelectedIndex = 1 Then
LisCost.Items.Add(FormatCurrency(125))
ElseIf LisLocation.SelectedIndex = 2 Then
LisCost.Items.Add(FormatCurrency(110))
ElseIf LisLocation.SelectedIndex = 3 Then
LisCost.Items.Add(FormatCurrency(100))
ElseIf LisLocation.SelectedIndex = 4 Then
LisCost.Items.Add(FormatCurrency(92))
ElseIf LisLocation.SelectedIndex = 5 Then
LisCost.Items.Add(FormatCurrency(90))
End If
Once these two subs take place I have at least two values in LisCost. The PROBLEM I am having is finding a way to access the amount of those indexes, not the index number themselves. I don't have the knowledge needed to pull the information.
How would I go about pulling the value of the indexes in order to add the costs together. All attempts I have used to find the values of the indexes result in me getting the index itself.
I know this is simple but for a first year student this is just mind boggling at this point. I haven't had any training above If...Then, Select Cases, and Loops.. As you can tell by the extent of my post this is killing me. I have a feeling something is escaping me about the listbox control or method... the problem being that its escaped me for two days now!
I will be monitoring for the next 8 hours so let your suggestions fly!
Re: [2005]Accept Variable in Listbox index
I'm a little lost but do you simply want to find out the values in list cost (i.e. there will be only 2 values, and you want the total?). In that case you just specify their index values like this:
Code:
Dim Total As Integer
Total = CInt(Me.ListCost.Items(0)) + CInt(Me.ListCost.Items(1))
MessageBox.Show(Total.ToString)
Re: [2005]Accept Variable in Listbox index
I know I explained that a little awkward but it all comes down to referencing the value in an index vs the index itself. Its just escaping me. Let me attempt your way and see what I got.
Re: [2005]Accept Variable in Listbox index
I added the following lines to the calculate button and did not receive the desired result hehe.
Code:
Dim decLisCost(5) as Decimal
Dim decLis1 as Decimal = decLisCost(0)
Dim declis2 as Decimal = decLisCost(1)
Dim decLisTotal as Decimal
DecLisTotal = decLis1 + decLis2
lblTotalCost.Text = FormatCurrency(decLisTotal)
Both indexes 0 and 1 had values of $595 and 95$ respectively, yet my label returned a value of 0
I'm lost
Re: [2005]Accept Variable in Listbox index
First of all I doubt you probably don't need to declare them as decimals but anyway.
Your code makes no sense. Why do you suddenly declare an array type of decimal??? Of course it will return 0. What you have basically done is say:
Dim decListCost(5) As Decimal
'Therefore:
decListCost(0) = 0
decListCost(1) = 0
decListCost(2) = 0
decListCost(3) = 0
'etc or in fact = Nothing
Where have you assigned the values from the listboxes to your variables??
You need something like:
Code:
'Assuming decListCost is a listbox, NOT an array of empty values
Dim decLis1 as Decimal = CDec(decLisCost.Items(0))
Dim decLis2 as Decimal = CDec(decLisCost.Items(1))
Re: [2005]Accept Variable in Listbox index
That was my fault, sorry I was playing with arrays prior to trying your code. I'll take that out right quick. I don't mean for the code to seem odd in any way. I feel like this entire subroutine is %$##^*# me off so bad that I can't really think straight when viewing/troubleshooting it
Re: [2005]Accept Variable in Listbox index
Stimbo you are the man and I want to take this time to thank you for your replies... You sir have ended 2 days worth of anguish!! I can not possibly thank you enough in this life time for your help. I will reference you in the notes in the code man thanks
Great rating coming your way
Re: [2005]Accept Variable in Listbox index
If I may also make a suggestion.....may help make things more readable. Instead of using the If....ElseIf scenario - try using select case
Code:
select case LisWorkshop.SelectedIndex
Case -1
MessageBox.Show("Please select a training session")
Case 0
LisCost.Items.Add(FormatCurrency(595))
Case 1
LisCost.Items.Add(FormatCurrency(695))
Case 2
LisCost.Items.Add(FormatCurrency(995))
Case 3
LisCost.Items.Add(FormatCurrency(1295))
Case 4
LisCost.Items.Add(FormatCurrency(395))
Case Else
MsgBox("Unknown Value")
End Select
Re: [2005]Accept Variable in Listbox index
No problem Abrium.
Take it easy and don't get too stressed about it. ;)
Mark the thread as resolved if all done (see the Tools menu at top of post) :thumb: