473,417 Members | 1,394 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,417 software developers and data experts.

Looking for good windows forms sample in C# showing how to work with ListBox

Hello,

This seems like it should be easy...

I have a listbox on a .NET form.

I add a new item to the list box.

How can I associated a separate data value?

Can I add (and retrieve) an arbitrary data value independent of the text
displayed?

I also tried creating an object and providing a ToString override. That did
let me add the item but I still didn't see a way to lookup based on
enumerating the list...

Thanks,
--
Grant Schenck
Nov 17 '05 #1
6 1566
You were on the right track with your second approach. Create a class
that has a "Text" property and a "Data" property, or whatever you want
to call them (ex: UserName and UserID). On the ListBox, set the
DisplayMember to point to your Text property. Set the ValueMember to
point to your Data property.

Grant Schenck wrote:
Hello,

This seems like it should be easy...

I have a listbox on a .NET form.

I add a new item to the list box.

How can I associated a separate data value?

Can I add (and retrieve) an arbitrary data value independent of the text
displayed?

I also tried creating an object and providing a ToString override. That did
let me add the item but I still didn't see a way to lookup based on
enumerating the list...

Thanks,

Nov 17 '05 #2
Hi Grant,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to display a column in the
data source, and retrieve another column in the data source as value. If
there is any misunderstanding, please feel free to let me know.

As far as I know, this can be achieved with ListBox's DisplayMember and
ValueMember property. Thes two properties get or set a string that
specifies the property of the data source whose contents you want to
display and the data source from which to draw the value.

You can check the following link for more information.

http://msdn.microsoft.com/library/de...us/vbcon/html/
vbtskdatabindingcomboboxcheckedlistboxorlistboxcon trol.asp

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #3
No, this appears to relate to tying a listbox to a database.

My situation is that I need to show a text string which can change over time
for each entry in the list box. In addition, the "key" to each item is an
unique integer value. So, when they select or double click an item I want
to be able to determine what the associated integer value is for the item.
However, the Items collection is a collection of objects. There are no
methods to set or retrive a data value.

Similarly, I need to be able to find a specific item in the collection based
on the data value.
--
Grant Schenck
http://grantschenck.tripod.com


"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:Td**************@TK2MSFTNGXA01.phx.gbl...
Hi Grant,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to display a column in the
data source, and retrieve another column in the data source as value. If
there is any misunderstanding, please feel free to let me know.

As far as I know, this can be achieved with ListBox's DisplayMember and
ValueMember property. Thes two properties get or set a string that
specifies the property of the data source whose contents you want to
display and the data source from which to draw the value.

You can check the following link for more information.

http://msdn.microsoft.com/library/de...us/vbcon/html/ vbtskdatabindingcomboboxcheckedlistboxorlistboxcon trol.asp

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #4
This seems to be the way to go...

If I create a class with two members, an int and a string and then provide a
ToString override which returns my string, I guess that would work.

Then, if I want to determine the int value associated with a given Listbox
item I can cast the object to my class and access the members.

However, how are the DisplayMember and ValueMember used? Those values apply
to the whole ListBox.

Also, while there seems to be a web version of the ListBox that contains a
collection of ListItems, this isn't true for the Windows Form version of
ListBox, correct? That contains a collection of objects, correct?
--
Grant Schenck
http://grantschenck.tripod.com
"Joshua Flanagan" <jo**@msnews.com> wrote in message
news:#M**************@tk2msftngp13.phx.gbl...
You were on the right track with your second approach. Create a class
that has a "Text" property and a "Data" property, or whatever you want
to call them (ex: UserName and UserID). On the ListBox, set the
DisplayMember to point to your Text property. Set the ValueMember to
point to your Data property.

Grant Schenck wrote:
Hello,

This seems like it should be easy...

I have a listbox on a .NET form.

I add a new item to the list box.

How can I associated a separate data value?

Can I add (and retrieve) an arbitrary data value independent of the text
displayed?

I also tried creating an object and providing a ToString override. That did let me add the item but I still didn't see a way to lookup based on
enumerating the list...

Thanks,

Nov 17 '05 #5
Correct - the web version of ListBox is different in that it holds
ListItems instead of objects.

And you are correct that you can retrieve specific values of an object
by casting it to the appropriate type.

Grant Schenck wrote:
This seems to be the way to go...

If I create a class with two members, an int and a string and then provide a
ToString override which returns my string, I guess that would work.

Then, if I want to determine the int value associated with a given Listbox
item I can cast the object to my class and access the members.

However, how are the DisplayMember and ValueMember used? Those values apply
to the whole ListBox.

Also, while there seems to be a web version of the ListBox that contains a
collection of ListItems, this isn't true for the Windows Form version of
ListBox, correct? That contains a collection of objects, correct?

Nov 17 '05 #6
Hi Grant,

I agree with Joshua. Object collection binding is similar to DataSet
binding. We can set "Text" as DisplayMember and "Data" as ValueMember in
this case. HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #7

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

Similar topics

3
by: MikeY | last post by:
Hi Everyone, I am working in C#, windows forms.My question is this. All my button dynamic controls properties are present and accounted for except for the"FlatStyle" properties. I can't seem to...
7
by: GTi | last post by:
In Win32 I have a function that opens several Dialogs and put the window handle in a listbox (together with a string): HWND hWnd = CreateDialogParam(...) LB_SETITEMDATA, index, (LPARAM)hWnd); ...
8
by: gumi | last post by:
Hi, I am looking for code for a alarm clock program that pops up a messege to be used as part of my VB.Net class project. Any help is very much appreciated. Thanks
4
by: Mika M | last post by:
Hi! If Windows Form -application has for example a button on Form1 which Click-event opens other Form2-form like... Dim f as New Form2 f.ShowDialog() If (f.DialogResult = DialogResult.OK)...
3
by: Garth Wells | last post by:
I'm working with an SDK from a commercial software vendor and am using a sample Windows Application written in C# to learn the SDK (they do not have an ASP.Net example). I need to create an...
0
by: Jake Montgomery | last post by:
I am using .NET 2.0 forms and running on Windows XP SP2. When I have a tooltip on a combo box, and the combo box is expanded (“dropped down”) using the arrow, it will no longer display it’s...
1
by: Frank | last post by:
Hi, Is it possible to code a Windows Form application (v.1.1) to create an HTML file in the Forms project folder, embedd variable values in hidden fields, and have the HTML page redirect to...
4
by: Stewart Berman | last post by:
Is there a control equivalent to the old Spin control? The one with two hour heads and an optional textbox in the middle? The closest I can find is the NumericUpDown control and it's not the same...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.