473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Add items with value and text to a combobox

I was databinding to a combobox and supplying both a DisplayMember and
a ValueMember,
so that each item.

Now I've decided against databinding, and will populate it by going
through a datatable.

What's the easy way to populate it so that each item has both a value
and a text?

Also, once populated, what's the easy way to find items without setting
the selectedIndex?

Dec 4 '06 #1
1 7569
Try the following class.

Public Class ListItemData
' ----- Class used to support ListBox and ComboBox items.
Public ItemText As String
Public ItemData As Long

Public Sub New(ByVal newText As String, ByVal newData As Long)
' ----- Required constructor.
ItemText = newText
ItemData = newData
End Sub

Public Overrides Function ToString() As String
' ----- Show the text in ListBox and ComboBox displays.
Return ItemText
End Function

Public Overrides Function Equals(ByVal obj As Object) As Boolean
' ----- Allow IndexOf() and Contains() searches by ItemData.
If (TypeOf obj Is Long) Then
Return CBool(CLng(obj) = ItemData)
Else
Return MyBase.Equals(obj)
End If
End Function
End Class

Add items to your listbox using the following code.

ListBox1.Items.Add(New ListItemData(someText, someNumericValue))

When you want to locate an item by its numeric value, use this code:

position = ListBox1.Items.IndexOf(someNumericValue)

-----
Tim Patrick - www.timaki.com
Start-to-Finish Visual Basic 2005
I was databinding to a combobox and supplying both a DisplayMember and
a ValueMember,
so that each item.
Now I've decided against databinding, and will populate it by going
through a datatable.

What's the easy way to populate it so that each item has both a value
and a text?

Also, once populated, what's the easy way to find items without
setting the selectedIndex?

Dec 4 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Varun | last post by:
Hello I am trying to make the items in my combobox be removed if they are present in my array I cant however get my code to work. The values are read into my combobox using a datareader. I then...
1
by: sherif | last post by:
when i access the items in a combobox by using the value member property i find that the retreived values aren't correct so could you tell me how to access them right.
0
by: andreas.baus | last post by:
Hi. Does anyone have an idea if it's possible to display a tooltip on individual items in a combobox (in dropped-down state). That means, what I want to do is the following: when the user is about...
4
daffurankan
by: daffurankan | last post by:
Hai, this ankan here by this kindly tell me that how can i save a value in combobox which is type in its text field . on visual basic 6.0 act soon asap it is very important for me...
3
by: anoopanand | last post by:
i would like to know how to add items on datagridviews combobox column with source code.im usin VB.Net ex: i want to add 3 items in the datagridview1 's 3rd column ( its a combobox)
6
by: iDesmet | last post by:
Good day! I was wondering if someone could show me the way with this little problem I have. I need to get the value/text of every checked SubItems in a listview so I can execute a Sub. The...
8
by: lttan123 | last post by:
hi there i am having some difficulties to bind value to combobox in windows application. combo.Items.Add(new List("a","b")) I have created a class. The value is there but what is displayed...
1
by: Joel1334 | last post by:
Hi! How can I change forecolor of a selected value in combobox? when I select a value in a combobox and press a button to "activate" what I've selected I want the text to be green and then...
4
by: felipebucaram | last post by:
Hi, I have a combobox named "combo7", I have 3 problems to resolve. First, I need the user to be able to add items to the combobox from the userform. Second, I need the combobox to autocomplete...
0
by: wasim jack | last post by:
sir,I want to change value of combobox of datagridview on the basis of previous combobox value of the same raw of same datagridview
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.