Data population in Combobox for VB.NET application


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Data population in Combobox for VB.NET application

Hybrid View

  1. #1
    Narasimhan Guest

    Data population in Combobox for VB.NET application


    Hi
    I have a combobox which is used to populate City Names and their corresponding
    City Codes.

    City names will be displayed in combobox.

    If i click a particular city from combobox, i need to get corresponding city
    code for a selected city name.


    For example we have the following list of city names and city codes.

    CityName CityCode
    -------- --------
    NewYork "NY"
    Washington "WT"
    Pitspurg "PP"


    If i click "Washington" from a combobox, i should be able to get corresponding
    city code "WT" from a combobox.

    How to achieve this in VB.NET.

    Please give me a solution.

    Thanks
    Narasimhan S



  2. #2
    Len Weaver Guest

    Re: Data population in Combobox for VB.NET application

    Hello Narasimhan,

    > I have a combobox which is used to populate City Names and their

    corresponding
    > City Codes.

    [snip]
    > If i click "Washington" from a combobox, i should be able to get

    corresponding
    > city code "WT" from a combobox.
    > How to achieve this in VB.NET.


    Write a class to contain both the name and code for a given city.
    Ex:

    <Untested Code>
    Public Class CityInfo

    Private mCode As String
    Private mName As String

    Public Sub New(ByVal name As String, ByVal code As String)

    mName = name
    mCode = code
    End Sub

    Public Property Name() As String
    Get
    Return mName
    End Get
    Set
    mName = Value
    End Set
    End Property

    Public Property Code() As String
    Get
    Return mCode
    End Get
    Set
    mCode = Value
    End Set
    End Property

    Public Overrides Function ToString() As String

    Return mName
    End Function
    End Class
    </Untested Code>

    Populate your ComboBox like this:

    cboCity.Items.Add( New CityInfo("New York", "NY" ) )
    cboCity.Items.Add( New CityInfo("Washington", "WT" ) )


    Retrieve the selected CityInfo object like this:

    Dim SelectedCity As CityInfo

    SelectedCity = CType(cboCity.SelectedItem, CityInfo)

    Debug.WriteLine( SelectedCity.Name )
    Debug.WriteLine( SelectedCity.Code )

    Hope this helps,
    Len



  3. #3
    Join Date
    Mar 2010
    Posts
    3

    ok, but...

    Ok, this migth be a dumb question. I've already created a class that does that very thing, but here is my dilemma. When I select the multi valued entry in my combobox, I then parse the value I want into combobox.text = <code> BUT after I do that, it still shows the full values. What am I doing wrong?

  4. #4
    Join Date
    May 2004
    Location
    Duluth MN
    Posts
    353
    Quote Originally Posted by rudyehinojosa View Post
    Ok, this migth be a dumb question. I've already created a class that does that very thing, but here is my dilemma. When I select the multi valued entry in my combobox, I then parse the value I want into combobox.text = <code> BUT after I do that, it still shows the full values. What am I doing wrong?
    If I understand your question, Len already answered it:

    Code:
    Dim SelectedCity As CityInfo
    
    SelectedCity = CType(cboCity.SelectedItem, CityInfo)
    
    Debug.WriteLine( SelectedCity.Name )
    Debug.WriteLine( SelectedCity.Code )
    You need to take the SelectedItem value of the combobox and convert it back into an instance of your class object. Then you can retrieve either property of the class.

    ...joe

  5. #5
    Join Date
    Mar 2010
    Posts
    3

    :)

    Thank you for your quick response. I'm sorry, I wasn't clear. I've got everything to work in the code blocks. What I can't get to work is applying the non description value back to the text of the combobox property. for example. if the pair string is "01 - mydescription" -- After the user selects this row, I want the combobox to close and only show me "01". I started with combobox.text = SelectedCity.Code (for the spirit of the example). BUT, it returns back the full description.

    I just left the world of vb6, so please tell me I don't have to do some kloodgy code to hide a textbox to overlay the combobox's coordinates to show me a different value or some noise like that. I will kill myself after becoming a clocktower sniper for the day.

    Rudy

  6. #6
    Join Date
    Mar 2010
    Posts
    3

    Solution found.

    Ok. Finally got this figured out. Here's a video sample of what I wanted to happen. I had a databound combobox that simply displayed a territory number as a varchar(2). I had the dropdown populate only on the dropdown event. and using the dropdownclosed property, i cleared the combobox and added the selected item code as the .text property and whalla. If anyone is interested, I will supply the code to make this happen.

    combobox_resolved.avi

  7. #7
    Join Date
    Feb 2010
    Location
    Central WV, USA
    Posts
    212
    Code:
    Public Sub New(ByVal name As String, ByVal code As String)
    
         mName = name
         mCode = code
    
    End Sub
    
    
    
    Public Overrides Function ToString() As String
    
         Return mName
    
    End Function
    Can someone explain why you would need to set your properies = to you fileds in your class constructor? Also why use a tostring fuction?

    I also thought I once read about a way to store an associated value with the main item in a list/combo box without using a class, although I may be mistaken.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center