Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 24th, 2008, 10:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default LISTITEM missing in Windows .NET

I want to populate a WINDOWS listbox with a list of items AND values, where the values are different from the items. In the wonderful world of web programming, I would do something like this:

dim liName as New ListItem("Ron",1)
lstNames.items.add(liName)

For some bizarre reason only MS can explain, ListItem doesn't exist in the WINDOWS IDE. So how do I do this in a WINDOWS application?
 
Old July 24th, 2008, 11:02 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I believe this is because you can bind anything to a list item in a win forms app. In the web, it can only be a text based item name and item value (being rendered to HTML). In win forms, it can be anything under System.Object. You can specify what property of the object to use as the displayed value. I'm not a winforms expert, but thats how I understand it to work.

-Peter
compiledthoughts.com
 
Old July 24th, 2008, 11:16 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

But not evey bit of data is so complex that binding is required. I just have a short, variable list of Item/Value pairs that are being generated on the fly. It seems like a lot unnecessary work to have to build something that can be bound to this control in Windows when it's so simple on the Web (and it has to be first for me to admit I've actually found something that's easier to do on the Web!)

I note that the listbox takes an "object". Is there some object not called ListItem in the Windows world that the listbox will accept that allows me to specify a both a item and value?
 
Old July 24th, 2008, 11:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Nevermind. I've developed a work around to this screwy limitation of Windows .NET by putting my value before the item ("1: Ron"). It looks like crap and I also had to write special code to extract the stupid key, but I don't have time to figure out the "right" way to do this in Windows.

Thanks, MS, for another half-baked .NET object!
 
Old July 24th, 2008, 11:52 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I'm sorry that you feel that this is a screwy limitation and half-backed object. I think it is quite the opposite. The way the win forms controls are set up is far more flexible than the ASP.NET counterparts. Of course, this comes with an added complexity.

You might take a look at the docs for the DisplayMember property of the listbox:

http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol.displaymember(VS. 71).aspx

I think you will find what you need in there. By default, the listbox control will call the ToString() method of each list member and show the result in the listbox. You can create another property to emit exactly what you like for the display output. Also, you can specify a particular property for the value of the item. This should give you all the flexibility you need to reach your goals.

As far as the simple object you are looking for, you might try the DictionaryEntry class:

http://msdn.microsoft.com/en-us/library/system.collections.dictionaryentry_members(VS.71). aspx

That is basically just a key/value pair. The key and value properties are of System.Object type so you'll still need to cast them when you want to consume them. Later versions of .NET have added some typed and generic versions to provide strong typing of the entry.

-Peter
compiledthoughts.com
The Following User Says Thank You to planoie For This Useful Post:
Ron Howerton (February 22nd, 2013)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing ListItem objects Andrew.Berry ASP.NET 2.0 Professional 1 April 8th, 2008 12:25 PM
C# Windows Form Controls Missing jcrudisill C# 2 August 3rd, 2007 12:32 PM
listitem alyeng2000 ASP.NET 1.0 and 1.1 Basics 2 January 3rd, 2004 10:13 AM
SelectedIndexChange uses ListItem value Randy76 VS.NET 2002/2003 3 September 11th, 2003 09:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.