October 18th, 2013, 09:48 AM
Studio 2010...using listbox items for calc.
I have a listbox that contains percentages. I'm trying to store the selected percentage in a double var called rate. I've tried about 50 different ways, but can't seem to get the items in the listbox to convert to double to store in the var.
What am I missing?
Dim sales As Double
Dim rate As Double
Dim bonus As Double
rate = CDbl(ratesListBox.SelectedItem)
MessageBox.Show(" " & rate.ToString("P2"), "check", MessageBoxButtons.OK, MessageBoxIcon.Information)
bonus = sales * rate
bonusLabel.Text = bonus.ToString("C2")
October 18th, 2013, 05:13 PM
Most VB controls use Variants to store values, which usually end up being interpreted as strings. In a ListBox, the items may appear as individual items, when if fact they are a single string of Null terminated strings. You can test this yourself by adding a string with a Null Character (Chr$(0)) in the middle. It will drop everything after the Null Character.
The easiest way around this is to maintain a separate parallel array with the actual numerical data, and then use the Index value from the ListBox selection to recover that value.