-
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
-
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
-
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?
-
 Originally Posted by rudyehinojosa
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
-
:)
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
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks