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

by Grant Schenck » Tue, 14 Jun 2005 05:09:19 GMT

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



CSharp/C# >> RE: Looking for good windows forms sample in C# showing how to work with ListBox

by v-kevy » Tue, 14 Jun 2005 10:48:49 GMT


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/default.asp?url=/library/en-us/vbcon/html/
vbtskdatabindingcomboboxcheckedlistboxorlistboxcontrol.asp

HTH.

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

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

by Joshua Flanagan » Tue, 14 Jun 2005 10:52:40 GMT

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.

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

by Grant Schenck » Tue, 14 Jun 2005 19:09:07 GMT

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






http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/

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

by Grant Schenck » Tue, 14 Jun 2005 19:13:33 GMT

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





did

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

by Joshua Flanagan » Tue, 14 Jun 2005 23:30:47 GMT

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.

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

by v-kevy » Wed, 15 Jun 2005 16:27:03 GMT

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."

Similar Threads

1. Windows Forms look good

Does anyone know how to make windows forms applications look good?  Some
kind of skin maybe.


2. Best Codeplex sample for showing best coding practices? - Asp.Net

3. Strange problem working on C# Windows Forms app at work and at hom

I started an app at work (VS 2005 + SQL Server 2005).  I added a UserControl 
and use it in 6 forms.  At home I have the same setup.  Connection string is 
set to localhost so data access works in both places with no problems.

When I leave work, I copy the whole folder and db files to USB key.  I can 
build and run at home, but I cannot open any form with the user control on 
it.  I get error:

An error has occurred while establishing a connection to the server. When 
connecting to SQL Server 2005, this failure may be caused by the fact that 
under the default settings SQL Server does not allow remote connections. 
(provider: Named Pipes Provider, error: 40 - Could not open a connection to 
SQL Server) 

I assume somewhere deep down the name of my machine at work is in a 
connection string, but I can't find it.  Even rebuilding doesn't solve the 
problem.

Any ideas?

4. Need a sample or VBA code to open a C# windows forms in Excel using Macro - Excel

5. Looking for C# ListBox which works like Windows-XP's Add/Remove programs ListBox.

Hi,
Im looking for ListBox which works like Windows-XP's Add/Remove
programs listbox.
where can i get this kind of listbox  ?
I want to show a text in ListItem & it should expand with its
description when user selects a particular item. Also i want to add a
button into to ListItem. it is same like Add/Remove programs ListBox.

anybody help me.
-Thiyagu

6. C# Windows Form Databound Listbox Problem - Help!! - CSharp/C#

7. Q:) Need sample code for a best practices Windows Form app

Is there anything like Petshop or Duwamish but more of a regular
client/server windows forms based app that demonstrates good Window Forms
coding practices?   Or something that just demonstrates the right way to
bind controls to data for a c/s app?

My .Net experience is mostly web app based so I'm used to opening datasets
and going back to refetch records on subsequent server roundtrips, etc.

I'm not sure about the best way to work with data sets and bound controls on
a client/server type of app where I can be fully coupled to the database.
I used to work with Delphi which had components for persistent database
connections and table objects that you opened once, wired your grids and
edit boxes up to, and did all of your updates through.   I know .net can do
that but everything seems oriented towards disconnected apps.  Then again,
maybe thats the better way, either way - some pointers would be helpful...

Thanks

bill
wiw at nyc . rr  com




8. Looking for good c# sample NetworkStream BeginRead/EndRead... - Microsoft .NET Framework