-
[RESOLVED] Getting a value from a listbox
Is it possible to get a value from a listbox in this manner
http://img364.imageshack.us/img364/9...featurelg8.png
What im doing is liting all available features in the first box. Then when a feature would be added to a plan, get the value of it and have it displayed at the bottom when th command button calculate is pressed. Multiple features may be listed in the second list box so I would like for it to get the total of all of them.
-
1 Attachment(s)
Re: Getting a value from a listbox
-
Re: Getting a value from a listbox
so what you mean is when you click on the "calculate" button, the value form the 1st listbox should be added to the below listbox without deleting anything in the 2nd listbox... is that what you want?
-
Re: Getting a value from a listbox
I already have got it to do that, I just didnt show it in the pic. Like in the first box, where it has Insurance - $4.99, after I highlight is and click add, it is added to the second listbox. What I would like for it to do, is when I click calculate, get the currency $4.99 (from the second listbox) and make it show up in the label I have next to total.
-
Re: Getting a value from a listbox
did you try?
Label1.Caption = ListBox2.Text in the click event of the command button?
or do you want the total sum of the entries in the listbox?
-
Re: Getting a value from a listbox
Yes, I would like the total sum of the entries in the listbox
-
Re: Getting a value from a listbox
I would create a text file that loads on startup that might contain:
"Insurance", 4.99 (comma separated values)
and so on...
Read the file concatenating the two fields into the upper listbox. Place/copy the second field (4.99) into a variable (as single) so you can simply use the index to get the value without having to parse it from the listbox. When the user selects one then additem it to the bottom box checking first to see that it does not already exist. Then, if adding, automatically add the value to your featuretotal. The Calculate button is not really necessary since you will know whether the item is not in the lower box and so must be added to the total.
Having a separate text file makes it easy to add or change entries without having to recompile the program.
Cadman
-
Re: Getting a value from a listbox
ok try this
Code:
Private Sub CommandButton1_Click()
For i = 0 To ListBox1.ListCount - 1
temp = ListBox2.List(i) + temp
Next
Label1.Caption = temp
End Sub
-
Re: Getting a value from a listbox
That is extremely close to what I need. Is there only way for the numeric value to be shown in the total?
-
Re: Getting a value from a listbox
if you mean without the currency sign then use the Format$ function...
in continuation to the above code...
Label1.Caption = Format$(temp, "0.00")
-
Re: Getting a value from a listbox
@ Cadman, the calculate button will also add the value to my main form.
-
Re: Getting a value from a listbox
Quote:
Originally Posted by koolsid
if you mean without the currency sign then use the format function...
Well its showing all the text in the label, instead of just the numeric value. Can you briefly explain the format button. Thx for sticking with me on this :)
-
Re: Getting a value from a listbox
Quote:
Can you briefly explain the format button.
already given an example on how to use it... check the last post...
Quote:
showing all the text in the label
What text :confused: ?
can i see your complete code?
-
Re: Getting a value from a listbox
Private Sub cmdaddfeature_Click()
listaddedfeatures.AddItem listfeatures.Text
End Sub
Private Sub cmdcalculate_Click()
For i = 0 To listfeatures.ListCount - 1
temp = listaddedfeatures.List(i) + temp
Next
lblfeaturetotal.Caption = temp
End Sub
Private Sub cmdremovefeature_Click()
On Error Resume Next
listaddedfeatures.RemoveItem listaddedfeatures.ListIndex
End Sub
Private Sub Form_Load()
listfeatures.AddItem "Insurance - $4.99"
listfeatures.AddItem "Test Feature - $99.99"
End Sub
________________________
In the label it shows
Insurance - $4.99
Test Feature - $99.99
I would like it to add the 4.99 and 99.99 together
-
Re: Getting a value from a listbox
oh ok...
so
Insurance - $4.99
is added to the 2nd listbox and you want only the number from it... right?
-
Re: Getting a value from a listbox
That will work. Or even if the full text insurance - $4.99 is added to the second box, I only want the numbers added together and displayed in the lblfeaturetotal label when i press calculate.
-
Re: Getting a value from a listbox
Try this
Code:
Private Sub cmdcalculate_Click()
Dim ar() As String, temp As Long
For i = 0 To listfeatures.ListCount - 1
ar = Split(listaddedfeatures.List(i), "$")
temp = Val(ar(1)) + temp
'the above will fail if the listbox is empty
'can put an error check there
ReDim ar(1)
Next
lblfeaturetotal.Caption = temp
End Sub
-
Re: Getting a value from a listbox
temp = Val(ar(1)) + temp says subscript out of range
-
Re: Getting a value from a listbox
yes it will ... as I mentioned in the code above. seems like there are blank values in the list. you need to put an error check there...
let me write the code for you ....
also
if you want to display the $ sign then you can also use
lblfeaturetotal.Caption = Format(temp, "£#,##0.00")
in the above code...
give me few secs for the code..
ok
try it now
Code:
Private Sub cmdcalculate_Click()
Dim ar() As String, temp As Long
For i = 0 To listfeatures.ListCount - 1
If Len(listaddedfeatures.List(i)) <> 0 Then
ar = Split(listaddedfeatures.List(i), "$")
temp = Val(ar(1)) + temp
ReDim ar(1)
End If
Next
lblfeaturetotal.Caption = Format(temp, "$#,##0.00")
End Sub
-
Re: Getting a value from a listbox
Ok, I really appreciate it. Now that I think about it I dont want to include the dollar sign since no other values on my main form have it. And why would their be blank values in the listbox if I didnt add them?
-
Re: Getting a value from a listbox
Great! One last thing. Is there anyway to make it not round up/down as it makes a difference in what im making it for? it lists 99.99 as 100 and 4.99 as 5.
-
Re: Getting a value from a listbox
Quote:
Originally Posted by UnKnOwNCoDe
Great! One last thing. Is there anyway to make it not round up/down as it makes a difference in what im making it for? it lists 99.99 as 100 and 4.99 as 5.
try this :)
lblfeaturetotal.Caption = Format(temp, "0.00")
Edit:
Please Mark your Thread "Resolved", if the query is solved...
check the link below on how to do it...
-
Re: Getting a value from a listbox
srry, had to go to work. Using that format for the label is still making the total come out to be rounded. 4.99 = 5.00, etc.
-
Re: Getting a value from a listbox
change this line
Code:
Dim ar() As String, temp As Long
to
Code:
Dim ar() As String, temp As Double
Now Check
-
Re: Getting a value from a listbox
Great! I hope otthers can benefit from what you have posted as much as I have!