#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    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

    Double.TryParse(salesTextBox.Text, sales)
    rate = CDbl(ratesListBox.SelectedItem)
    MessageBox.Show(" " & rate.ToString("P2"), "check", MessageBoxButtons.OK, MessageBoxIcon.Information)

    bonus = sales * rate
    bonusLabel.Text = bonus.ToString("C2")
    End Sub
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    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.

    J.A. Coutts

IMN logo majestic logo threadwatch logo seochat tools logo