Results 1 to 13 of 13

Thread: Listbox text stored as decimal variable

  1. #1
    New Member
    Join Date
    Aug 12
    Posts
    6

    Question Listbox text stored as decimal variable

    Hi,

    I am sorry ahead of time for being a first time poster and posing what might be an obviously simple question. I googled and youtubed my brains out first.

    In my program I want to:

    Have a listbox that says: 10 gauge , 8 gauge, 6 Gauge, etc
    When a user selects "10 Gauge"
    I want to assign the value .000129 to it and display it in a label (or textbox)
    I ALSO want to use the value .000129 in a calculation.

    So basically (ListItem = "10 Gauge" = .000129)

    Does that make sense? Should I just elaborate on the whole program?

    Thank you very much for your time.

    My previous program incorporated radio buttons nexts to labels and I gave the value to the radio button, however I have MANY more wire sizes I would like to add and it looks silly with all the buttons.
    Last edited by station2station; Aug 10th, 2012 at 11:23 PM. Reason: Editing

  2. #2
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,383

    Re: Listbox text stored as decimal variable

    You could use the ItemData property. However it can only accept a Long Integer value so you'd need to scale up the value and then reverse when using it
    Code:
    Private Sub Form_Load()
    List.AddItem "10 Gauge"
    List.ItemData(List.NewIndex) = 1290
    List.AddItem "8 Gague"
    List.ItemData(List.NewIndex) = 641
    List.AddItem "6 Gague"
    List.ItemData(List.NewIndex) = 403
    End Sub
    
    Private Sub List_Click()
    Dim sngOPF As Single
    Text1.Text = List.ItemData(List.ListIndex) / 10000000
    sngOPF = CSng(Text1.Text)
    End Sub
    In the example above I've used 10 Gague = 0.000129, 8 Gague = 0.0000641 and 6 Gague = 0.0000403.

    If you know the smallest possible value then you can select a multiplier to siut.

  3. #3
    Frenzied Member
    Join Date
    Aug 11
    Location
    B.C., Canada
    Posts
    1,946

    Re: Listbox text stored as decimal variable

    is this what you want

    Code:
    Private Sub List1_Click()
    Select Case List1.List(List1.ListIndex)
    
       Case "10 Gauge"
          Text1 = "0.000129"
          
       Case "8 Gauge"
          Text1 = "8 Gauge"
          
       Case "6 Gauge"
          Text1 = "6 Gauge"
          
       Case "4 Gauge"
          Text1 = "4 Gauge"
    End Select
    
    'OR if you know which item is what you can do it like this
    
    'Select Case List1.ListIndex
    '   Case 0
    '      Text1 = "0.000129"
    '   Case 1
    '      Text1 = "8 Gauge"
    '   Case 2
    '      Text1 = "6 Gauge"
    '   Case 3
    '      Text1 = "4 Gauge"
    'End Select
    
    End Sub

  4. #4
    New Member
    Join Date
    Aug 12
    Posts
    6

    Re: Listbox text stored as decimal variable

    I think that will do it. I will give that a try.

    Thank you for helping me Doogle.

    Any more ideas are welcome. Im working on it currently.


    Max, yes that is basically what I want to do.

  5. #5
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,383

    Re: Listbox text stored as decimal variable

    Another method might be to hold a 'shadow' array of values representing the Gauge.
    Code:
    Private sngOPF(2) As Single
    
    Private Sub Form_Load()
    List.AddItem "10 Gauge"
    sngOPF(List.NewIndex) = 0.000129
    List.AddItem "8 Gague"
    sngOPF(List.NewIndex) = 0.0000641
    List.AddItem "6 Gague"
    sngOPF(List.NewIndex) = 0.0000403
    End Sub
    
    Private Sub List_Click()
    Text1.Text = sngOPF(List.ListIndex)
    End Sub

  6. #6
    New Member
    Join Date
    Aug 12
    Posts
    6

    Re: Listbox text stored as decimal variable

    Basically, (if youll mind the pun)


    I have a program that takes a users input (#Inverters and Length of run) then provides the Voltage rise in the wire for a selected wire gauge.

    I want them to select through the wires to find which wire fits their tolerance.

    I have done it many times in excel and using only 4 wires as a choice and radio buttons.

    The list is MUCH longer now and I want them to pick from a long "list" of wire gauges. When they select the wires gauge, they select the resistance/foot each of the wires have. I would want the program to display the resistance they have chosen and then take that number do some back ground math and produce a value that they will know how to work with.


    This is my last program.


  7. #7
    New Member
    Join Date
    Aug 12
    Posts
    6

    Re: Listbox text stored as decimal variable

    I have tried a number of things to take this listbox work and now I am even more confused.

  8. #8
    Super Moderator koolsid's Avatar
    Join Date
    Feb 05
    Location
    Mumbai, India
    Posts
    11,447

    Re: Listbox text stored as decimal variable

    From the screenshot it seems you are using vb.net?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  9. #9
    New Member
    Join Date
    Aug 12
    Posts
    6

    Re: Listbox text stored as decimal variable

    Its Visual Basic 2008 Express Edition

  10. #10
    Super Moderator koolsid's Avatar
    Join Date
    Feb 05
    Location
    Mumbai, India
    Posts
    11,447

    Re: Listbox text stored as decimal variable

    Quote Originally Posted by station2station View Post
    Its Visual Basic 2008 Express Edition
    I have moved the thread from Visual Basic 6 and Earlier to Visual Basic .NET where you will get appropriate responses
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  11. #11
    New Member
    Join Date
    Aug 12
    Posts
    6

    Re: Listbox text stored as decimal variable

    This is my current work in progress.
    Attached Images Attached Images  

  12. #12
    PowerPoster
    Join Date
    Jul 06
    Location
    Maldon, Essex. UK
    Posts
    5,383

    Re: Listbox text stored as decimal variable

    I'm just a beginner in VB2008 so this may be far more complicated than necessary but may give you a start and also encourage others with more knowledge than me to come up with something 'better'.
    Code:
    Public Class Form1
        Private Structure Wires
            Public Guage As String
            Public OpFt As Single
        End Structure
    
        Private myWires(2) As Wires
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            myWires(0).Guage = "10 Guage"
            myWires(0).OpFt = 0.000129
            myWires(1).Guage = "8 Guage"
            myWires(1).OpFt = 0.0000641
            myWires(2).Guage = "6 Guage"
            myWires(2).OpFt = 0.0000403
            ListBox1.Items.Add(myWires(0).Guage)
            ListBox1.Items.Add(myWires(1).Guage)
            ListBox1.Items.Add(myWires(2).Guage)
    
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            Debug.Print(myWires(ListBox1.SelectedIndex).OpFt)
        End Sub
    End Class
    It's basically an 'extension' of my earlier suggestion of 'shadowing' with an array. Select an Item and the Resistance will be displayed in the Immediate Window.
    Last edited by Doogle; Aug 11th, 2012 at 02:23 AM. Reason: Spelling

  13. #13
    PowerPoster
    Join Date
    Jun 01
    Location
    Trafalgar, IN
    Posts
    3,479

    Re: Listbox text stored as decimal variable

    Maybe something like this would work.
    Code:
        Private Sub wire_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim dtWires As New DataTable
    
            With dtWires.Columns
                .Add("Display", GetType(String))
                .Add("Value", GetType(Decimal))
            End With
    
            dtWires.Rows.Add("10 Guage", 0.000129)
            dtWires.Rows.Add("8 Guage", 0.0000641)
            dtWires.Rows.Add("6 Guage", 0.0000403)
    
            ListBox1.DisplayMember = "Display"
            ListBox1.ValueMember = "Value"
            ListBox1.DataSource = dtWires
    
    
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            Dim strDisplay As String = ListBox1.Text
            Dim decValue As Decimal = ListBox1.SelectedValue
    
            MsgBox(strDisplay & " - " & decValue.ToString)
        End Sub

Posting Permissions

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