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
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.
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
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
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.
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
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