dcsimg
Results 1 to 4 of 4

Thread: Listbox Conversion from String to Decimal???

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2013
    Posts
    8

    Listbox Conversion from String to Decimal???

    OK I'm working on a project and need some help. I have two forms both with a list box. I need to get the selected item in one list box to the other form and have it totaled. I think I've managed to get the selected item from one form to the other the problem is the items in the list boxes are in String format. I need to convert the selected item from String to decimal so I can total them. My forms look like this...

    'This is the FrmMeal
    Public Shared Choice As String

    Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click
    Choice = CStr(LstMeal.SelectedItem)
    FrmDormandMeal.TxtMeal.Text = Choice
    FrmDormandMeal.Show()
    End Sub

    ' The list box items look like this
    7 meals per wek - $720 per semester
    14 meals per week - $1350 per semester
    Unimited meals - $1960 per semester

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,473

    Re: Listbox Conversion from String to Decimal???

    I was going to suggest CDec(), but if your items really look as you show, then that won't work. You'd have to be able to extract the numerical part from out of the middle of the string. There are a couple ways to do this, but I'd prefer not to do it at all. If you can maintain some other collection that has the number, that would be easier than stripping the number from the string being displayed.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2013
    Posts
    8

    Re: Listbox Conversion from String to Decimal???

    Yea I need to extract the number within the string but I'm not sure what you mean by maintain it another way? I was thinking I might have to index the numbers out? That does sound complicated.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,473

    Re: Listbox Conversion from String to Decimal???

    If you have no choice, then what you might try doing is to split on "$". This will divide the strings into two strings, the second of which will have what you want...with some extra stuff. Therefore, you can probably get the numbers by doing this:

    CDec(Val(Listbox.Item(x).ToString.Split("$"c)(1)))

    That may be a bit much to do in a single line. The idea is that you split it on the dollar sign, take the second element (index 1), and get the Val of that and convert it to Decimal. This should work because Val will stop parsing as soon as it hits a character that it doesn't recognize as a number. Therefore, it will take the number from the start of the string and stop parsing as soon as it hits the first space. Val also returns a Double, so you would want to convert it to Decimal, or even Integer, since the values you have shown are actually all integers.

    What I was getting at may not be possible. It isn't completely clear how this would work. It all depends on how the items in the listbox are set up. It sounds like the first listbox is a set of options. You could have a Dictionary(of Integer, Decimal) that was set up with something like this:

    1, 720
    2, 1250
    3, 1960

    such that each item in the listbox has a matching key in the Dictionary, where the value is the amount associated with that index in the listbox. Whenever you selected an item from the listbox, you'd add that value from the dictionary. This seems unlikely to work, as I would guess that what you have in the first listbox is a whole lot more complicated than what you showed in that example.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width