Results 1 to 25 of 25

Thread: [RESOLVED] Getting a value from a listbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Resolved [RESOLVED] Getting a value from a listbox

    Is it possible to get a value from a listbox in this manner




    What im doing is liting all available features in the first box. Then when a feature would be added to a plan, get the value of it and have it displayed at the bottom when th command button calculate is pressed. Multiple features may be listed in the second list box so I would like for it to get the total of all of them.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Re: Getting a value from a listbox

    Here ya go.
    Attached Files Attached Files

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Getting a value from a listbox

    so what you mean is when you click on the "calculate" button, the value form the 1st listbox should be added to the below listbox without deleting anything in the 2nd listbox... is that what you want?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Re: Getting a value from a listbox

    I already have got it to do that, I just didnt show it in the pic. Like in the first box, where it has Insurance - $4.99, after I highlight is and click add, it is added to the second listbox. What I would like for it to do, is when I click calculate, get the currency $4.99 (from the second listbox) and make it show up in the label I have next to total.

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Getting a value from a listbox

    did you try?

    Label1.Caption = ListBox2.Text in the click event of the command button?

    or do you want the total sum of the entries in the listbox?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Re: Getting a value from a listbox

    Yes, I would like the total sum of the entries in the listbox

  7. #7
    Lively Member CADman's Avatar
    Join Date
    Aug 2007
    Posts
    92

    Re: Getting a value from a listbox

    I would create a text file that loads on startup that might contain:
    "Insurance", 4.99 (comma separated values)
    and so on...

    Read the file concatenating the two fields into the upper listbox. Place/copy the second field (4.99) into a variable (as single) so you can simply use the index to get the value without having to parse it from the listbox. When the user selects one then additem it to the bottom box checking first to see that it does not already exist. Then, if adding, automatically add the value to your featuretotal. The Calculate button is not really necessary since you will know whether the item is not in the lower box and so must be added to the total.
    Having a separate text file makes it easy to add or change entries without having to recompile the program.

    Cadman

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Getting a value from a listbox

    ok try this

    Code:
    Private Sub CommandButton1_Click()
    For i = 0 To ListBox1.ListCount - 1
        temp = ListBox2.List(i) + temp
    Next
    Label1.Caption = temp
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Re: Getting a value from a listbox

    That is extremely close to what I need. Is there only way for the numeric value to be shown in the total?

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Getting a value from a listbox

    if you mean without the currency sign then use the Format$ function...

    in continuation to the above code...

    Label1.Caption = Format$(temp, "0.00")
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Re: Getting a value from a listbox

    @ Cadman, the calculate button will also add the value to my main form.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Re: Getting a value from a listbox

    Quote Originally Posted by koolsid
    if you mean without the currency sign then use the format function...
    Well its showing all the text in the label, instead of just the numeric value. Can you briefly explain the format button. Thx for sticking with me on this

  13. #13
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Getting a value from a listbox

    Can you briefly explain the format button.
    already given an example on how to use it... check the last post...

    showing all the text in the label
    What text ?
    can i see your complete code?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Re: Getting a value from a listbox

    Private Sub cmdaddfeature_Click()
    listaddedfeatures.AddItem listfeatures.Text
    End Sub

    Private Sub cmdcalculate_Click()
    For i = 0 To listfeatures.ListCount - 1
    temp = listaddedfeatures.List(i) + temp
    Next
    lblfeaturetotal.Caption = temp
    End Sub

    Private Sub cmdremovefeature_Click()
    On Error Resume Next
    listaddedfeatures.RemoveItem listaddedfeatures.ListIndex


    End Sub

    Private Sub Form_Load()
    listfeatures.AddItem "Insurance - $4.99"
    listfeatures.AddItem "Test Feature - $99.99"
    End Sub
    ________________________

    In the label it shows
    Insurance - $4.99
    Test Feature - $99.99

    I would like it to add the 4.99 and 99.99 together

  15. #15
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Getting a value from a listbox

    oh ok...

    so

    Insurance - $4.99

    is added to the 2nd listbox and you want only the number from it... right?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Re: Getting a value from a listbox

    That will work. Or even if the full text insurance - $4.99 is added to the second box, I only want the numbers added together and displayed in the lblfeaturetotal label when i press calculate.

  17. #17
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Getting a value from a listbox

    Try this

    Code:
    Private Sub cmdcalculate_Click()
        Dim ar() As String, temp As Long
        For i = 0 To listfeatures.ListCount - 1
            ar = Split(listaddedfeatures.List(i), "$")
            temp = Val(ar(1)) + temp
            'the above will fail if the listbox is empty
            'can put an error check there
            ReDim ar(1)
        Next
        lblfeaturetotal.Caption = temp
    End Sub
    Last edited by Siddharth Rout; Sep 9th, 2008 at 01:54 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Re: Getting a value from a listbox

    temp = Val(ar(1)) + temp says subscript out of range

  19. #19
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Getting a value from a listbox

    yes it will ... as I mentioned in the code above. seems like there are blank values in the list. you need to put an error check there...

    let me write the code for you ....

    also

    if you want to display the $ sign then you can also use

    lblfeaturetotal.Caption = Format(temp, "£#,##0.00")

    in the above code...

    give me few secs for the code..

    ok

    try it now

    Code:
    Private Sub cmdcalculate_Click()
        Dim ar() As String, temp As Long
        For i = 0 To listfeatures.ListCount - 1
            If Len(listaddedfeatures.List(i)) <> 0 Then
                ar = Split(listaddedfeatures.List(i), "$")
                temp = Val(ar(1)) + temp
                ReDim ar(1)
            End If
        Next
        lblfeaturetotal.Caption = Format(temp, "$#,##0.00")
    End Sub
    Last edited by Siddharth Rout; Sep 9th, 2008 at 02:03 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Re: Getting a value from a listbox

    Ok, I really appreciate it. Now that I think about it I dont want to include the dollar sign since no other values on my main form have it. And why would their be blank values in the listbox if I didnt add them?

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Re: Getting a value from a listbox

    Great! One last thing. Is there anyway to make it not round up/down as it makes a difference in what im making it for? it lists 99.99 as 100 and 4.99 as 5.

  22. #22
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Getting a value from a listbox

    Quote Originally Posted by UnKnOwNCoDe
    Great! One last thing. Is there anyway to make it not round up/down as it makes a difference in what im making it for? it lists 99.99 as 100 and 4.99 as 5.
    try this

    lblfeaturetotal.Caption = Format(temp, "0.00")

    Edit:
    Please Mark your Thread "Resolved", if the query is solved...
    check the link below on how to do it...
    Last edited by Siddharth Rout; Sep 9th, 2008 at 02:30 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Re: Getting a value from a listbox

    srry, had to go to work. Using that format for the label is still making the total come out to be rounded. 4.99 = 5.00, etc.
    Last edited by UnKnOwNCoDe; Sep 10th, 2008 at 12:24 AM.

  24. #24
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Getting a value from a listbox

    change this line

    Code:
    Dim ar() As String, temp As Long
    to

    Code:
    Dim ar() As String, temp As Double
    Now Check
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    123

    Re: Getting a value from a listbox

    Great! I hope otthers can benefit from what you have posted as much as I have!

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