Page 1 of 2 12 LastLast
Results 1 to 40 of 54

Thread: Need help with listbox texts to have a value and add onto another textbox

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Need help with listbox texts to have a value and add onto another textbox

    Basically, let's say in listbox1 I put a code staying listbox1.items.add(" lmao =$50")
    Listbox1.items.add("xD = $30")

    So, what I want is that when running this build when I select both of these on the list box I want a textbox to have the value 80 (50+30)

    I can voice call on discord if required but I really need help to finish my assignment, I'm freaking out.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,440

    Re: Need help with listbox texts to have a value and add onto another textbox

    You will need to handle the SelectedValueChanged event to determine when the user has selected or unselected items. You can find documentation on this specific event here: https://docs.microsoft.com/en-us/dot...edvaluechanged

    You will need to do a couple of things in the SelectedValueChanged event:
    1. Check if any items are selected
    2. If so, then loop over each item
    3. Get substring starting after the dollar sign
    4. Convert the substring to a number
    5. Add the result to a "total" variable


    Here is a brief example:
    Code:
    Public Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim total As Double = 0
        If (ListBox1.SelectedItems.Count > 0) Then
            For Each item In ListBox1.SelectedItems
                Dim dollarAmount = item.ToString().SubString(item.ToString().IndexOf("$") + 1)
                total += CDbl(dollarAmount)
            Next
        End If
    
        TextBox1.Text = total.ToString()
    End Sub
    "Code is like humor. When you have to explain it, it’s bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    I got like maybe 50% of that, I'll try it out and I just wanted to say thank you for taking the time out and trying to explain it. Is there anyway I could like stream and show you?

  4. #4
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,657

    Re: Need help with listbox texts to have a value and add onto another textbox

    The tough part about trying to help people with homework is that the assignment is often designed for the student to make use of very specific concepts taught in that chapter, and previous chapters. Without the context of what you are learning, it is very likely that suggested code from people here would include concepts not yet taught, which could be a red flag to the instructor that the code wasn't written by you.

    If your listbox is intended to have the text as written "Product = $Cost", then the assignment must expect you to store the prices in something like a separate parallel array to the listbox contents, since I doubt they would want you to do text parsing to extract the price from the listbox text itself.

    Maybe it would help if you post the wording of the assignment and give a brief list of what topics have been covered in your class recently.

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    Scenario
    Chess Champions offer online training courses for beginners, and for players looking to
    improve their ratings, which they sell direct to customers. They have asked you to create an
    application to allow customers to purchase chess boards from them.
    Customers must enter their Elo rating, so that they can be offered the most suitable
    selection of training plans for their current level. An Elo rating is a number which indicates
    the player’s current skill at the game: they range from 0 to 3000, and the higher the number,
    the better the player. This must be entered before the plans are displayed. Once the Elo
    rating has been entered, the training plan options should be displayed, only including
    suitable plans for the given rating.
    Customers must choose at least one of the training plans offered. For each course, they
    may choose the option of online one-to-one tuition from an expert player, for an additional
    cost. In addition, they may also choose from a range of optional items, including chess
    books and luxury chess boards. If any of the optional physical items are selected,
    customers must enter their full address. However, if customers only select an online training
    course with no optional items, they will only need to enter their email address and credit
    card details.
    The list of courses, Elo rating range for each course, and their prices are shown below.
    Course Name Elo
    Range
    Cost
    for
    course
    Cost of
    additional oneto-one tuition
    Beginners: How to Play 0 –
    100
    £10
    £50
    Beginners: Opening Fundamentals 101 –
    900
    £20
    Beginners: Middle Game Tactics 101 –
    900
    £20
    Beginners: Finding Checkmate 101 –
    900
    £20
    Intermediate: Winning Chess
    Strategies
    751 –
    1500
    £30
    £150
    Intermediate: Reading the Board 751 –
    1500
    £25
    Advanced: Optimal Move Selection 1400+ £40
    £300
    Advanced: Play Like a GM 1400+ £50

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    Attachment 181942

    basically when i click those i want the valur of it to show on the "total" textbox

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    Attachment 181943

    thats the code i tried which is obviously wrong cause it just keeps on showing 0

  8. #8
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,657

    Re: Need help with listbox texts to have a value and add onto another textbox

    Posting attachments here is flaky at best, and since code is just text, please copy and paste your code directly into this thread and use the "#" thread tool button to enclose your code in code tags so that it appears readable. Thanks!

  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    Code:
     Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListBox1.Items.Add("Beginner: How to Play - £10")
            ListBox2.Items.Add("Bobby Fischer Goes to War, by David Edmonds & John Eidinow, Paperback - £11.00")
            ListBox2.Items.Add("Chess Fundamentals, by José Raúl Capablanca - £15.00")
            ListBox2.Items.Add("Attacking the Strongpoint: The Philosophy of Chess, by Igor Zaitsev and Garry Kasparov - £20.00")
            ListBox2.Items.Add("Luxury Glass Chess Set - £50")
            ListBox2.Items.Add("Handcrafted Walnut Wooden Chess Set - £90")
    
    
    
        End Sub
    
        Private Sub Label4_Click(sender As Object, e As EventArgs)
        End Sub
    
        Friend WithEvents purbtn As Button
        Private Sub purbtn_Click(sender As Object, e As EventArgs) Handles purbtn.Click
    
            Dim sum As Integer = Val(ListBox1.SelectedItem) + Val(ListBox2.SelectedItem)
            TextBox1.Text = sum
    
        End Sub
    Thank you for being so patient with me

  10. #10
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,657

    Re: Need help with listbox texts to have a value and add onto another textbox

    The Val() function essentially looks at the first character passed to it and determines if it can be interpreted as a number. It isn't smart enough to take the text as a whole and do a deep dive to see if somewhere buried inside of the text is something numeric. Since the "SelectedItem" property of all the items in your listboxes start with words, it won't give you the price you are looking for.

    So, you've got 5 items in ListBox2. Those items can be visualized as being contained in an array, where the indices of the items are 0 through 4, inclusive.

    What you can do is create a separate array that contains the prices that correspond to the listbox data.

    Example:

    Code:
    Dim prices2(4) As Integer
    prices2(0) = 11
    prices2(1) = 15
    ...
    And so on.

    Then, in your calculation, instead of using the ListBox's SelectedItem property, you use the ListBox's SelectedIndex property. So, if I highlight the top item in ListBox2, then the ListBox2.SelectedIndex = 0. You then use that .SelectedIndex as the index for the prices2 array. Highlighting the second item, the .SelectedIndex = 1, third item = 2, and so on.

    If I highlight the "Bobby Fischer" book, the ListBox2.SelectedIndex = 0, and prices2(0) = 11, which is the price you are looking for.

    I'll leave it up to you to piece the concepts together in code.

    Good luck.
    Last edited by OptionBase1; Jul 29th, 2021 at 11:46 AM.

  11. #11

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    omg can i say I love you?

  12. #12

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    Also another help i required was that i needed a label to read a text from a text file

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: Need help with listbox texts to have a value and add onto another textbox

    Code:
    Lanel1.Text = IO.File.ReadAllText(“path”)
    Last edited by .paul.; Jul 29th, 2021 at 03:08 PM.

  14. #14

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    thank you thank you, it worked!

  15. #15

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    Quote Originally Posted by OptionBase1 View Post
    The Val() function essentially looks at the first character passed to it and determines if it can be interpreted as a number. It isn't smart enough to take the text as a whole and do a deep dive to see if somewhere buried inside of the text is something numeric. Since the "SelectedItem" property of all the items in your listboxes start with words, it won't give you the price you are looking for.

    So, you've got 5 items in ListBox2. Those items can be visualized as being contained in an array, where the indices of the items are 0 through 4, inclusive.

    What you can do is create a separate array that contains the prices that correspond to the listbox data.

    Example:

    Code:
    Dim prices2(4) As Integer
    prices2(0) = 11
    prices2(1) = 15
    ...
    And so on.

    Then, in your calculation, instead of using the ListBox's SelectedItem property, you use the ListBox's SelectedIndex property. So, if I highlight the top item in ListBox2, then the ListBox2.SelectedIndex = 0. You then use that .SelectedIndex as the index for the prices2 array. Highlighting the second item, the .SelectedIndex = 1, third item = 2, and so on.

    If I highlight the "Bobby Fischer" book, the ListBox2.SelectedIndex = 0, and prices2(0) = 11, which is the price you are looking for.

    I'll leave it up to you to piece the concepts together in code.

    Good luck.
    Code:
     Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim prices1(0) As Integer
            Dim prices2(4) As Integer
            prices1(0) = 10
            prices2(0) = 11
            prices2(1) = 15
            prices2(2) = 20
            prices2(3) = 50
            prices2(4) = 90
            ListBox1.Items.Add("Beginner: How to Play - £10")
            ListBox2.Items.Add("Bobby Fischer Goes to War, by David Edmonds & John Eidinow, Paperback - £11.00")
            ListBox2.Items.Add("Chess Fundamentals, by José Raúl Capablanca - £15.00")
            ListBox2.Items.Add("Attacking the Strongpoint: The Philosophy of Chess, by Igor Zaitsev and Garry Kasparov - £20.00")
            ListBox2.Items.Add("Luxury Glass Chess Set - £50")
            ListBox2.Items.Add("Handcrafted Walnut Wooden Chess Set - £90")
    
    
    
        End Sub
    
        Private Sub Label4_Click(sender As Object, e As EventArgs)
        End Sub
    
        Friend WithEvents purbtn As Button
        Private Sub purbtn_Click(sender As Object, e As EventArgs) Handles purbtn.Click
    
            If ListBox1.SelectedIndex = 0 Then
    i did it till here, but im very lost as to how to add the prices cause as soon as i do that it says prices1(0) need to be declared even tho i did declare it

  16. #16

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    I'm trying to code the button so that when i click it, the textbox will show the amount of whatever I had highlighted previously from the two listboxes

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,465

    Re: Need help with listbox texts to have a value and add onto another textbox

    You declared the prices1 array as a local variable within the load method. By doing that, the array is not visible outside of the method. No other method can use it. To solve that, just move the declarations outside of the method. Generally, variable declarations are done prior to any method, but there's no rule that says it HAS to be that way.

    So, you want to move:

    Dim prices1(0) As Integer

    outside of the method. It can be right before it, or higher up in the file prior to any other method. You should also change Dim to Private, though it really won't make any difference if you do. Dim is used for local variables, and works just fine for class level variables, but it would be better form to use Private. I always forget what Dim means (public, private, or friend) when not used on a local variable.
    My usual boring signature: Nothing

  18. #18

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    okay yeah that worked, but do i have to do each of the combos separately? like if i highlight the 1st one from the listbox1 and the 3rd one from listbox2 how do i make it so that it automatically does the calculation without me having to declare each one

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: Need help with listbox texts to have a value and add onto another textbox

    Where’s your code that calculates values?

  20. #20

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    Code:
     ListBox1.Items.Add("Beginner: How to Play - £10")
            ListBox2.Items.Add("Bobby Fischer Goes to War, by David Edmonds & John Eidinow, Paperback - £11.00")
            ListBox2.Items.Add("Chess Fundamentals, by José Raúl Capablanca - £15.00")
            ListBox2.Items.Add("Attacking the Strongpoint: The Philosophy of Chess, by Igor Zaitsev and Garry Kasparov - £20.00")
            ListBox2.Items.Add("Luxury Glass Chess Set - £50")
            ListBox2.Items.Add("Handcrafted Walnut Wooden Chess Set - £90")
    
    
    
        End Sub
    
        Private Sub Label4_Click(sender As Object, e As EventArgs)
        End Sub
    
        Friend WithEvents purbtn As Button
        Private Sub purbtn_Click(sender As Object, e As EventArgs) Handles purbtn.Click
            Dim prices1(0) As Integer
            Dim prices2(4) As Integer
            prices1(0) = 10
            prices2(0) = 11
            prices2(1) = 15
            prices2(2) = 20
            prices2(3) = 50
            prices2(4) = 90
            If ListBox1.SelectedIndex = 0 Then
                TextBox1.Text = prices1(0)
            End If
            If ListBox2.SelectedIndex = 0 Then
                TextBox1.Text = prices2(0)
            End If
    
            If ListBox1.SelectedIndex = 0 And ListBox2.SelectedIndex = 0 Then
                TextBox1.Text = prices1(0) + prices2(0)
            End If
    i can do it one by one but there are so many possibilities and its so much grunt work, I'm sure there's an easier way

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: Need help with listbox texts to have a value and add onto another textbox

    You’ve just got to loop through each listbox, then each listbox’s items

    Code:
    Dim listBoxes() As ListBox = {ListBox1, ListBox2, ListBox3}
    
    For x As integer = 0 to listBoxes.GetUpperBound(0)
        For y As integer = 0 To listBoxes(x).Items.Count - 1
            ‘Put your summing code here
    
        Next
    Next

  22. #22

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    I tried really hard to get that but alas I failed miserably

  23. #23

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    Quote Originally Posted by .paul. View Post
    You’ve just got to loop through each listbox, then each listbox’s items

    Code:
    Dim listBoxes() As ListBox = {ListBox1, ListBox2, ListBox3}
    
    For x As integer = 0 to listBoxes.GetUpperBound(0)
        For y As integer = 0 To listBoxes(x).Items.Count - 1
            ‘Put your summing code here
    
        Next
    Next
    what is my summing code? listbox1.selectedindex + listbox2.selectedindex? how would they know what the prices are? as you can see im horribly new at this and really bad at this too

  24. #24
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: Need help with listbox texts to have a value and add onto another textbox

    Instead of Arrays, Datatables would be better…

    Code:
    Dim dt1 As New DataTable
    dt1.Columns.Add(“itemName”)
    dt1.Columns.Add(“price”, GetType(Decimal))
    
    dt1.Rows.Add(“first item”, 10)
    dt1.Rows.Add(“second item”, 20)
    dt1.Rows.Add(“third item”, 30)
    
    ComboBox1.DisplayMember = “itemName”
    ComboBox1.ValueMember = “price”
    ComboBox1.DataSource = dt1
    
    ComboBox1.SelectedIndex = 1
    Msgbox(ComboBox1.SelectedItem.ToString)
    Msgbox(ComboBox1.SelectedValue.ToString)
    The Msgboxes will show “second item” and “20”

  25. #25
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,657

    Re: Need help with listbox texts to have a value and add onto another textbox

    Quote Originally Posted by .paul. View Post
    Instead of Arrays, Datatables would be better…

    Code:
    Dim dt1 As New DataTable
    dt1.Columns.Add(“itemName”)
    dt1.Columns.Add(“price”, GetType(Decimal))
    
    dt1.Rows.Add(“first item”, 10)
    dt1.Rows.Add(“second item”, 20)
    dt1.Rows.Add(“third item”, 30)
    
    ComboBox1.DisplayMember = “itemName”
    ComboBox1.ValueMember = “price”
    ComboBox1.DataSource = dt1
    
    ComboBox1.SelectedIndex = 1
    Msgbox(ComboBox1.SelectedItem.ToString)
    Msgbox(ComboBox1.SelectedValue.ToString)
    The Msgboxes will show “second item” and “20”
    Right, and the chances that those concepts have been covered in class and expected to be used in the assignment are essentially 0. Which goes along with the point I made in my first post.

  26. #26
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: Need help with listbox texts to have a value and add onto another textbox

    In my previous code, replace ComboBox with ListBox

    You’d use a new DataTable for each listbox. The itemName column is what you see in the listbox. The price column is what you will be using for the summation. Do you have multiselect in your listboxes?

    Code:
    Dim listBoxes() As ListBox = {ListBox1, ListBox2, ListBox3}
    Dim total as decimal = 0D
    
    For x As integer = 0 to listBoxes.GetUpperBound(0)
        For y As integer = 0 To listBoxes(x).SelectedItems.Count - 1
            ‘Put your summing code here
            total += cdec(listBoxes(x).SelectedValues(y))
        Next
    Next
    
    Msgbox(total.ToString(“c2”))

  27. #27

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    yes I have multiselect enabled. Is there anyway you could join a call with me? teams or messenger so i could share my screen

  28. #28
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: Need help with listbox texts to have a value and add onto another textbox

    Ok, just using ListBoxes and parallel arrays…

    Code:
    Dim prices1(0) As Integer
    Dim prices2(4) As Integer
    prices1(0) = 10
    prices2(0) = 11
    prices2(1) = 15
    prices2(2) = 20
    prices2(3) = 50
    prices2(4) = 90
    
    Dim allPrices(1)() As Integer
    allPrices(0) = prices1
    allPrices(1) = prices2
    
    Dim listBoxes() As ListBox = {ListBox1, ListBox2}
    Dim total as integer = 0
    
    For x As integer = 0 to listBoxes.GetUpperBound(0)
        For y As integer = 0 To listBoxes(x).Items.Count - 1
            ‘Put your summing code here
            If listBoxes(x).IsSelected Then
                total += allPrices(x)(y)
            End If
        Next
    Next
    Does that work how you’re hoping for? It sums all values Of selected items in listbox1 then all values of selected items in listbox2. If that’s not what you’re looking for, the code will have to be modified
    Last edited by .paul.; Jul 29th, 2021 at 05:34 PM.

  29. #29
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: Need help with listbox texts to have a value and add onto another textbox

    Sorry no teamviewer

  30. #30

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    'IsSelected' is not a member of 'Listbox'

  31. #31
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: Need help with listbox texts to have a value and add onto another textbox

    Code:
    If listBoxes(x).Items(y).IsSelected Then

  32. #32

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    Quote Originally Posted by .paul. View Post
    Ok, just using ListBoxes and parallel arrays…

    Code:
    Dim prices1(0) As Integer
    Dim prices2(4) As Integer
    prices1(0) = 10
    prices2(0) = 11
    prices2(1) = 15
    prices2(2) = 20
    prices2(3) = 50
    prices2(4) = 90
    
    Dim allPrices(1)() As Integer
    allPrices(0) = prices1
    allPrices(1) = prices2
    
    Dim listBoxes() As ListBox = {ListBox1, ListBox2}
    Dim total as integer = 0
    
    For x As integer = 0 to listBoxes.GetUpperBound(0)
        For y As integer = 0 To listBoxes(x).Items.Count - 1
            ‘Put your summing code here
            If listBoxes(x).IsSelected Then
                total += allPrices(x)(y)
            End If
        Next
    Next
    Does that work how you’re hoping for? It sums all values Of selected items in listbox1 then all values of selected items in listbox2. If that’s not what you’re looking for, the code will have to be modified
    well basically i want any highlighted items in listbox1 to be added with any highlighted items in listbox2 when a button is pressed in a textbox so, yes

  33. #33

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    Quote Originally Posted by .paul. View Post
    Code:
    If listBoxes(x).Items(y).IsSelected Then
    theres an error in this line when i try and press the button to sum the highlighted items "If listBoxes(x).Items(y).IsSelected Then" saying

    System.MissingMemberException: 'Public member 'IsSelected' on type 'String' not found.'

  34. #34
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: Need help with listbox texts to have a value and add onto another textbox

    Sorry, i was typing that on my phone. I tested it on my workstation now...

    Code:
    Dim prices1(0) As Integer
    Dim prices2(4) As Integer
    prices1(0) = 10
    prices2(0) = 11
    prices2(1) = 15
    prices2(2) = 20
    prices2(3) = 50
    prices2(4) = 90
    
    Dim allPrices(1)() As Integer
    allPrices(0) = prices1
    allPrices(1) = prices2
    
    Dim listBoxes() As ListBox = {ListBox1, ListBox2}
    Dim total As Integer = 0
    
    For x As Integer = 0 To listBoxes.GetUpperBound(0)
        For y As Integer = 0 To listBoxes(x).Items.Count - 1
            'Put your summing code here
            If listBoxes(x).SelectedIndices.Contains(y) Then
                total += allPrices(x)(y)
            End If
        Next
    Next

  35. #35
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: Need help with listbox texts to have a value and add onto another textbox

    All of the prices in prices1 have to be relevant to their parallel item in ListBox1
    All of the prices in prices2 have to be relevant to their parallel item in ListBox2
    allPrices is an array of arrays, the first element contains prices1, the second element contains prices2
    listBoxes is an array of listboxes

  36. #36

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    Quote Originally Posted by .paul. View Post
    Sorry, i was typing that on my phone. I tested it on my workstation now...

    Code:
    Dim prices1(0) As Integer
    Dim prices2(4) As Integer
    prices1(0) = 10
    prices2(0) = 11
    prices2(1) = 15
    prices2(2) = 20
    prices2(3) = 50
    prices2(4) = 90
    
    Dim allPrices(1)() As Integer
    allPrices(0) = prices1
    allPrices(1) = prices2
    
    Dim listBoxes() As ListBox = {ListBox1, ListBox2}
    Dim total As Integer = 0
    
    For x As Integer = 0 To listBoxes.GetUpperBound(0)
        For y As Integer = 0 To listBoxes(x).Items.Count - 1
            'Put your summing code here
            If listBoxes(x).SelectedIndices.Contains(y) Then
                total += allPrices(x)(y)
            End If
        Next
    Next
    I think it worked cause its not crashing anymore but it isnt showing the supposed total in the textbox1

  37. #37
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: Need help with listbox texts to have a value and add onto another textbox

    Quote Originally Posted by .paul. View Post
    All of the prices in prices1 have to be relevant to their parallel item in ListBox1
    All of the prices in prices2 have to be relevant to their parallel item in ListBox2
    allPrices is an array of arrays, the first element contains prices1, the second element contains prices2
    listBoxes is an array of listboxes
    How many items do you have in listbox1?
    What should be the total, and what is it showing?

  38. #38
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: Need help with listbox texts to have a value and add onto another textbox

    After the loops, you should have something like...

    Code:
    TextBox1.Text = total.ToString("c2")

  39. #39

    Thread Starter
    Member
    Join Date
    Jul 2021
    Posts
    32

    Re: Need help with listbox texts to have a value and add onto another textbox

    Quote Originally Posted by .paul. View Post
    After the loops, you should have something like...

    Code:
    TextBox1.Text = total.ToString("c2")
    OMG OMGOMG OMGOMG I LOVE YOU MAN! it worked so frikkin good! theres like a few more things i need help with still and is there anyway i could remove the $ thats showing before the total? or make it so that it shows £ instead?

  40. #40
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,183

    Re: Need help with listbox texts to have a value and add onto another textbox

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        Threading.Thread.CurrentThread.CurrentCulture = New CultureInfo("en-GB")
    
    End Sub

Page 1 of 2 12 LastLast

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