|
-
Jan 19th, 2004, 04:52 PM
#1
Thread Starter
Junior Member
Drop Down List
Basically I am trying to write a simple conversion tool. Take x amount of feet and convert that into y amount of meters and vice versa. I am using a whole bunch of different measurements so I decided to use two comboboxes as dropdownlists. Basically, if the user inputs 5 they can then easily select meters to feet or feet to meters, etc.
I have my equation written but can not figure out for the life of me how to assign a value or key to the items in my dropdownlist. I am using visual studio .net. I populated my list originally using the List Property. How do I give those a property? Say I want Meters to equal sngMet in my code. How would I do that?
I need to Populate the two lists, give each item in the list a unique ID, tie that id to a value and then insert that value into an equation.
If you couldn't tell by now, I am a noobie. I am just at the point that looking at this any longer will make me mad! I've tried to RTFM but its a POS.
Thanks,...
-
Jan 19th, 2004, 04:52 PM
#2
Thread Starter
Junior Member
Here's a sample of the code I am using. The problem is that I can't call my "Item.Key" later in the program. The "Item.Value" prints fine. The "Item.Key" string comes up blank:
<vb code>
Dim mySortedList as new System.Collections.SortedList
Dim Item as DictionaryEntry
mySortedList("sngMet") = "Meters"
mySortedList("sngIn") = "Inches"
mySortedList("sngFt") = "Feet"
mySortedList("sngCm") = "Centimeters"
For each Item in mySortedList
Dim newListItem as new ListItem()
newListItem.Text = Item.Value
newListItem.Value = Item.Key
DropDownList1.Items.Add(newListItem)
Next
</vb code>
__________________
Drew
-
Jan 19th, 2004, 05:59 PM
#3
Fanatic Member
hi,
I don't know if this is too complicated for you as a noob but it's a way I know of getting it to work. You need to create your own class called cUnit. Here you can store your names and keys. Here's the code. Let me know if you have any questions
Code:
Dim blnFinishedLoading As Boolean = False
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myArrayList As New System.Collections.ArrayList
Dim Item As cUnit
Item = New cUnit
Item.key = "sngMet"
Item.unit = "Meters"
myArrayList.Add(Item)
Item = New cUnit
Item.key = "sngFeet"
Item.unit = "Feet"
myArrayList.Add(Item)
ComboBox1.DataSource = myArrayList
ComboBox1.ValueMember = "key"
ComboBox1.DisplayMember = "unit"
blnFinishedLoading = True
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If blnFinishedLoading = True Then
MessageBox.Show(ComboBox1.SelectedValue & " " & ComboBox1.Text)
End If
End Sub
End Class
'NOTE THIS IS THE END OF THE FORMS CLASS. IF YOU COPY AND
'PASTE MAKE SURE YOU DON'T HAVE TOO MANY End Class LINES
'OF CODE
Public Class cUnit
Dim strKey As String
Dim strUnit As String
Public Property key() As String
Get
Return strKey
End Get
Set(ByVal Value As String)
strKey = Value
End Set
End Property
Public Property unit() As String
Get
Return strUnit
End Get
Set(ByVal Value As String)
strUnit = Value
End Set
End Property
end class
you can just copy and paste it right underneath the 'windows designed code' bit
Hope this helps
Nick
-
Jan 19th, 2004, 06:08 PM
#4
Thread Starter
Junior Member
Actually that makes a lot of sense. Since my last post, I created an array and was able to get it to return the proper .SelectedIndex and .Text. So I am close... I appreciate your help. Your way makes more sense than mine though. I may rewrite using your technique. Is your technique a better way or a more accepted method?
Here is the code I am currently using:
<vbcode>
Dim strCurrency(7) As String
strCurrency(0) = "Select Currency"
strCurrency(1) = "European Euro"
strCurrency(2) = "Mexican Peso"
strCurrency(3) = "Indian Rupee"
strCurrency(4) = "British Pound"
strCurrency(5) = "Japanese Yen"
strCurrency(6) = "Canadian Dollar"
strCurrency(7) = "American Dollar"
Dim i As Integer
For i = 0 To 7
cboFrom.Items.Add(strCurrency(i))
cboTo.Items.Add(strCurrency(i))
Next i
</vbcode>
Thanks!
-
Jan 20th, 2004, 03:53 AM
#5
Fanatic Member
well it's using classes which is a good start. This means you can seperate out any logic you may need in the class into them. You could pass these objects created from the class around. You can seperate out your classes to seperate .dll's which can help scaleability a lot.
Using classes and OOP in .NET is the way forward but whether it's too complicated for this problem I don't know. I suppose it depends what else the application is going to do.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|