ComboBox

 

I have a combobox question, two parts:

When using a combobox, normally the items are added to the drop down list by using items.add.  Is there a way to have the list of items but not be hard coded into the code or require some type of database   In another language I messed with, there was a string table that would be a part of the application but provided a central location for list items.

Is there a way to associate items in a listbox or combobox with an alternative id... example... Alaska... AK... North Carolina... NC... in a list box... the full name of the states would be listed, but the abbreviations of the states would be what is used in something like a filename or textbox.  I'm trying to stay away from if...then or case statements that would test for the state name and then equate the abbreviation.

I'm new to .NET and have been coding in VB6.  Any help would be greatly appreciated.  Thanks in advance...



Answer this question

ComboBox

  • case23_69

    Hi. there are a lot of ways to tackle this one. I'm not sure what version of visual studio you're using so I'll assume VS 2003. I would recommend populating a hashtable with both the full state name as the key and the abbreviation as the value

    Private RegionHash as new HashTable

    RegionHash.Add("Alaska", "AK")

    RegionHash.Add("Alabama", "AL")

    You can then populate your combo box as follows

    For Each RegionKey as String in RegionHash.keys

    Combobox1.items.add(RegionKey)

    Next

    Whenever you want to get the abbreviated version you would simply use (perhaps inside the selectedindexchanged event)

    RegionHash(combobox1.selectedindex)

    I hope this answers your question!

    Christopher



  • bas31

    I think RKimble has the right idea. In this case he is using a XML file as a datasource and then using the databinding to hook up the control to the datasource.

    Datasources can be from a variety of source - XML being one of them. Often the datasource is a database whether it be SQL Server/ SQL express/ Access or something else. Either way you would populate a dataset and then set the datasource to that datatable within the dataset, with the displaymember and valuemember properties being set to fields in the datatable.

    ComboBox1.DisplayMember = "Key"
    ComboBox1.ValueMember = "Value"
    ComboBox1.DataSource = DSTable '//Datatable

    Data binding is described in numerous places and provides a real quick and flexible way to connect to a backing data store.

    Some Resource to look through which cover this topic and more

    E Book for Learning VB 2005
    http://msdn.microsoft.com/vbasic/learning/introtovb2005/

    Video on Various VB Subjects for Beginners (One specifically on data binding - Number 9)

    http://msdn.microsoft.com/vstudio/express/vb/learning/


  • Scire

    I would suggest that you use XML. This will allow you to create and maintain a simple XML file that can be modified without making changes to the code. You code will simply know how to read and load your XML data into an ArrayList that the ComboBox can bind to.

    Here is an example of the XML file:

    < xml version="1.0" encoding="utf-8" >
    <Abbreviations>
    <State name="Alaska" abbv="AK" />
    <State name="Arkansas" abbv="AR" />
    <State name="Arizona" abbv="AZ" />
    </
    Abbreviations>

    Here is a function that would read the XML file into an ArrayList of DictionaryEntry objects:

    Private Function LoadListFromXML(ByVal file As String) As System.Collections.ArrayList

    Dim output As New System.Collections.ArrayList
    Dim xd As New Xml.XmlTextReader(file)

    While xd.Read

    If xd.Name = "State" Then

    Dim de As New DictionaryEntry(xd.Item("name"), xd.Item("abbv"))
    output.Add(de)

    End If

    End While

    xd.Close()
    Return output

    End Function

    And finally, here's a code snipit that hooks up a ComboBox

    ComboBox1.DisplayMember = "Key"
    ComboBox1.ValueMember = "Value"
    ComboBox1.DataSource = LoadListFromXML("XMLData.xml")

    That should give you what you were looking for.

    HTH, GL!



  • ComboBox