Results 1 to 5 of 5

Thread: Drop Down List

  1. #1

    Thread Starter
    Junior Member drizz's Avatar
    Join Date
    Jan 2004
    Location
    Redmond
    Posts
    19

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

  2. #2

    Thread Starter
    Junior Member drizz's Avatar
    Join Date
    Jan 2004
    Location
    Redmond
    Posts
    19
    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
    Drew

  3. #3
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    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
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  4. #4

    Thread Starter
    Junior Member drizz's Avatar
    Join Date
    Jan 2004
    Location
    Redmond
    Posts
    19
    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!
    Drew

  5. #5
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    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.
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

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