Results 1 to 5 of 5

Thread: Visual Basics Virgin. Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    8

    Exclamation Visual Basics Virgin. Help

    Hi, so i have this list box with the last column being a numeric value. What i needs for when i hit the display total button, u need for a line to be added to the display box with the words "Total:", and then the total of all the net pays of the employess listed in the box. Any help is nice! this is my code for the formatting. Thank you. The total on the picture right now is not accurate i just put in a simple decimal to see if i could get the format down.
    With lstOutput.Items
    .Add(String.Format(strFormatter, "Employee", "Department", "Weekly Hours", "Net Pay"))
    .Add("---------------------------------------------------------------")
    End With
    Name:  heelp.jpg
Views: 271
Size:  40.7 KB

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,230

    Re: Visual Basics Virgin. Help

    Hi,

    All you need to do here is to loop through the Items Collection of the ListBox and take the Last value of the Array after splitting each element in the list. (.paul. showed you how to split the string yesterday)

    You then need to use the TryParse Method of the Decimal type to work out if this Last element of the Array is a decimal value. If so, then you need to add the value to a Total Pay variable. The only tricky bit here is dealing with the Currency Symbol which can be sorted using the Second Overload of the TypeParse Method and passing in a Number Style and a Format Provider.

    All you need to do then is to add that Total Pay value back to the ListBox.

    Give that a go for yourself and if you get stuck post back and we will be able to help you with coding this to completion.

    Hope that helps.

    Cheers,

    Ian

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    8

    Re: Visual Basics Virgin. Help

    Thanks for the help. Still a bit confused. So how do i change the code
    For x As Integer = 0 To ListBox1.Items.Count - 1
    Dim fields() As String = ListBox1.Items(x).ToString.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
    to get to the last element of the array? I tried changing the integer but it didnt seem to do anything.

  4. #4
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Visual Basics Virgin. Help

    "fields" is a string array with fields.Length elements, it's zero based so fields(0) will be the first element and fields(fields.Length - 1) will be the last.

    You need to check you add the right fields elements together, so you should check that fields is the length you expect. To add the relevant elements you'll also need to convert them to the appropriate type.

    Beware that the relevant fields might not be a number, so it's also worth checking that what you're adding together really are numbers and not text or whatever.
    Last edited by Bulldog; Mar 8th, 2015 at 01:20 PM.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,398

    Re: Visual Basics Virgin. Help

    Code:
    Dim sum As Decimal = 0
    For x As Integer = 0 To ListBox1.Items.Count - 1
        Dim fields() As String = ListBox1.Items(x).ToString.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
        Dim oneValue As Decimal
        Decimal.TryParse(fields(3).Trim, Globalization.NumberStyles.AllowCurrencySymbol, Globalization.CultureInfo.CurrentCulture, oneValue)
        sum += oneValue
    Next
    MsgBox(sum)

Posting Permissions

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



Click Here to Expand Forum to Full Width