Top DevX Stories
Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Search the forums:

Go Back   DevX.com Forums > DevX Developer Forums > .NET

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 08-20-2001, 10:18 AM
Raj
Guest
 
Posts: n/a
Itemdata in Combo box


How to get the Itemdata functionality present in VB6 in Vb.Net. Pls help
Reply With Quote
  #2  
Old 08-20-2001, 01:00 PM
Jim Cooper
Guest
 
Posts: n/a
Re: Itemdata in Combo box

You do it by subclassing the combo box. You can then have any kind
of object data for each combo entry, and if that object has a toString
method
it will be displayed.

Raj wrote:

> How to get the Itemdata functionality present in VB6 in Vb.Net. Pls help


Reply With Quote
  #3  
Old 08-20-2001, 03:51 PM
Rob Teixeira
Guest
 
Posts: n/a
Re: Itemdata in Combo box


"Raj" <raj_shk101@hotmail.com> wrote:
>
>How to get the Itemdata functionality present in VB6 in Vb.Net. Pls help



Raj,
There are four things you can do. But before I discuss that, let's look at
what ItemData is used for. ItemData is an old way to associate data to a
string list item, and is implemented by a legacy style of 'object' code in
the underlying Windows list control. It was usefull in C/C++ because aside
from a number, you could also put a pointer to some data element in there.
In a new OO approach, there are several ways to implement this.

1) Look for an index
One of the pieces of data you often associated with a string item in VB6
was its index, which came in handy when the list order wasn't the same as
the order the items were added. In VB.NET, if you want the index of an item,
you can simply use the following:

MessageBox.Show(ListBox1.items.IndexOf("My String List Item"))

In a way, this kills two birds with one stone, since you couldn't get the
index directly from VB6 (without some API work), even though the underlying
control supported that feature.

2) If you need associated data, create the associated data in a class
This prevents you from having to do anything to the listbox itself. In other
words...

Let's say I'm adding Employees to a listbox. I then want to add an ID to
the ItemData, so I can easily query the database. In VB.NET, you can do the
following:

Public Class Employee
Public FirstName As String
Public LastName As String
Public ID As Integer

Public Overrides Function ToString() As String
Return LastName + ", " + FirstName
End Function
End Class

So later on, instead of adding a string to listbox, you can add the entire
object to the listbox. For example:

Dim emp As Employee = New Employee()
emp.FirstName = "John"
emp.LastName = "Doe"
emp.ID = 250

ListBox1.Items.Add(emp)

' do some other stuff here and wait for user to select item -

MessageBox.Show(ListBox1.SelectedItem.ID)

So in this way, you don't need item data to associate some data with an item.
Also, the listbox will display whatever is returned from the ToString() method
of the object you added, which in this case, conveniently resolves to [Lastname,
Firstname]. The last piece of code in this example needs to be cast appropriately
if you have Option Strict On.

Furthermore, the associated data doesn't have to be a simple number. For
example, the Employee object can have a reference to a Department object,
and so on and so forth.

3) Inherit Listbox and add the ItemData property
If you absolutely feel as if you must use the same construct as VB6, you
can create your own listbox (by simply inheriting from the winforms Listbox)
and add the ItemData property yourself. In your custom listbox class, simply
add the following code:

Private Const LB_GETITEMDATA = &H199
Private Const LB_SETITEMDATA = &H19A

Public Property ItemData(ByVal Index As Integer) As Integer
Get
'check to see if index is valid
If (Index < 0) Or (Index > Items.Count) Then
Throw New IndexOutOfRangeException()
End If

'create Get Item Data message
Dim m As Message = Message.Create(Me.Handle, LB_GETITEMDATA,
IntPtr.op_Explicit(Index), IntPtr.Zero)

'submit message to window proc (sendmessage)
wndproc(m)
Return m.Result.ToInt32
End Get

Set(ByVal Value As Integer)
'check to see if index is valid
If (Index < 0) Or (Index > Items.Count) Then
Throw New IndexOutOfRangeException()
End If

' create Set Item Data message with Index and Value
Dim data As IntPtr = IntPtr.op_Explicit(Value)
Dim m As Message = Message.Create(Me.Handle, LB_SETITEMDATA,
IntPtr.op_Explicit(Index), data)

'submit message to window proc (sendmessage)
wndproc(m)
End Set
End Property

This structure imposes the same limitations as VB6, but behaves the same
way. When you're working with your forms, simply add an instance of your
new custom Listbox class instead of the standard Listbox class. In fact,
it wouldn't surprise me if someone actually builds a bunch of components
that behave along the same lines as the VB6 components. Bill McCarthy talked
about making a VB6-style Form in a previous post.

4) Add another collection/list for data
In this scenario, you inherit from Listbox, just as you did in the previous
example, but instead of using the Windows message to store the integer number,
you can maintain your own collection or list (such as an ArrayList). That
being the case, you can store anything you like.

Hope that helps,
-Rob
Reply With Quote
  #4  
Old 08-20-2001, 04:49 PM
Jacob Grass
Guest
 
Posts: n/a
Re: Itemdata in Combo box

Rob-

Just to add something:

> 2) If you need associated data, create the associated data in a class
> This prevents you from having to do anything to the listbox itself. In

other
> words...
>
> Let's say I'm adding Employees to a listbox. I then want to add an ID to
> the ItemData, so I can easily query the database. In VB.NET, you can do

the
> following:
>
> Public Class Employee
> Public FirstName As String
> Public LastName As String
> Public ID As Integer
>
> Public Overrides Function ToString() As String
> Return LastName + ", " + FirstName
> End Function
> End Class
>
> So later on, instead of adding a string to listbox, you can add the entire
> object to the listbox. For example:
>
> Dim emp As Employee = New Employee()
> emp.FirstName = "John"
> emp.LastName = "Doe"
> emp.ID = 250
>
> ListBox1.Items.Add(emp)
>
> ' do some other stuff here and wait for user to select item -
>
> MessageBox.Show(ListBox1.SelectedItem.ID)
>
> So in this way, you don't need item data to associate some data with an

item.
> Also, the listbox will display whatever is returned from the ToString()

method
> of the object you added, which in this case, conveniently resolves to

[Lastname,
> Firstname]. The last piece of code in this example needs to be cast

appropriately
> if you have Option Strict On.
>
> Furthermore, the associated data doesn't have to be a simple number. For
> example, the Employee object can have a reference to a Department object,
> and so on and so forth.


The Listbox also has a property called DisplayMember, which it inherits from
ListControl. The purpose of the DisplayMember property is to inform the
listbox which member of the associated class to display. So, rather than
override the ToString Method to return the correct data, the property
containing the correct data can be assigned. So, to take advantage of this
ability with your example above, create a ReadOnly property called FullName
that returns the LastName & ", " & FirstName. Set the DisplayMember
Property = "FullName" and voila. The advantage of this method is that
should you wish to display a different element of the class, it isn't
necessary to rewrite the ToString Method, rather, you can just change the
value of the DisplayMember property.

Jacob


Reply With Quote
  #5  
Old 08-20-2001, 05:11 PM
Rob Teixeira
Guest
 
Posts: n/a
Re: Itemdata in Combo box


Good catch

My WinForms is getting rusty since i'm dedicating a lot (read: too much)
time to components and socket/protocols work.

-Rob


"Jacob Grass" <JGrass@AbilitiSolutions.com> wrote:
>
>The Listbox also has a property called DisplayMember, which it inherits

from
>ListControl. The purpose of the DisplayMember property is to inform the
>listbox which member of the associated class to display. So, rather than
>override the ToString Method to return the correct data, the property
>containing the correct data can be assigned. So, to take advantage of this
>ability with your example above, create a ReadOnly property called FullName
>that returns the LastName & ", " & FirstName. Set the DisplayMember
>Property = "FullName" and voila. The advantage of this method is that
>should you wish to display a different element of the class, it isn't
>necessary to rewrite the ToString Method, rather, you can just change the
>value of the DisplayMember property.
>
>Jacob
>
>


Reply With Quote
  #6  
Old 08-20-2001, 05:31 PM
Jacob Grass
Guest
 
Posts: n/a
Re: Itemdata in Combo box

You mean you aren't overdosing on IDE stuff? : )

I have been doing nothing but WinForms 14 hours a day for 3 weeks. . .

Jacob


"Rob Teixeira" <RobTeixeira@@msn.com> wrote in message
news:3b816ee1$1@news.devx.com...
>
> Good catch
>
> My WinForms is getting rusty since i'm dedicating a lot (read: too much)
> time to components and socket/protocols work.
>
> -Rob
>
>
> "Jacob Grass" <JGrass@AbilitiSolutions.com> wrote:
> >
> >The Listbox also has a property called DisplayMember, which it inherits

> from
> >ListControl. The purpose of the DisplayMember property is to inform the
> >listbox which member of the associated class to display. So, rather than
> >override the ToString Method to return the correct data, the property
> >containing the correct data can be assigned. So, to take advantage of

this
> >ability with your example above, create a ReadOnly property called

FullName
> >that returns the LastName & ", " & FirstName. Set the DisplayMember
> >Property = "FullName" and voila. The advantage of this method is that
> >should you wish to display a different element of the class, it isn't
> >necessary to rewrite the ToString Method, rather, you can just change the
> >value of the DisplayMember property.
> >
> >Jacob
> >
> >

>



Reply With Quote
  #7  
Old 08-21-2001, 10:51 AM
Rob Teixeira
Guest
 
Posts: n/a
Re: Itemdata in Combo box


I went through that phase earlier, but due to other commitments, I've been
having to deal with a lot more back-end and transport stuff lately.

The IDE was actually the easy part for me.

-Rob

"Jacob Grass" <JGrass@AbilitiSolutions.com> wrote:
>You mean you aren't overdosing on IDE stuff? : )
>
>I have been doing nothing but WinForms 14 hours a day for 3 weeks. . .
>
>Jacob
>



Reply With Quote
  #8  
Old 08-21-2001, 11:29 AM
Jacob Grass
Guest
 
Posts: n/a
Re: Itemdata in Combo box

I know all about those commitments. . .That's why I have been so immersed in
WinForms. . .

Jacob

"Rob Teixeira" <RobTeixeira@@msn.com> wrote in message
news:3b826757$1@news.devx.com...
>
> I went through that phase earlier, but due to other commitments, I've been
> having to deal with a lot more back-end and transport stuff lately.
>
> The IDE was actually the easy part for me.
>
> -Rob
>
> "Jacob Grass" <JGrass@AbilitiSolutions.com> wrote:
> >You mean you aren't overdosing on IDE stuff? : )
> >
> >I have been doing nothing but WinForms 14 hours a day for 3 weeks. . .
> >
> >Jacob
> >

>
>



Reply With Quote
  #9  
Old 10-25-2009, 09:36 AM
visves_vimal visves_vimal is offline
Registered User
 
Join Date: Aug 2009
Posts: 1
Itemdata for Combobox in VB.Net

Combobox list item can hold object rather than string. in the object we can store any number of data and can be retrieved from SelectedItem when user click a item in combobox.

I have created userdefined class "ListItem" to store both Text and value property, and added items to combobox as

cbCountry.Items.Add(new ListItem("India", "001"));
cbCountry.Items.Add(new ListItem("Australia", "002"));

I retrieved its value as

lblCountry.Text = ((ListItem)cbCountry.SelectedItem).Text;
lblCountryCode.Text = ((ListItem)cbCountry.SelectedItem).Value;


I have added a tutorial to set Itemdata value for list item in combo box, Kindly visit

http://www.dotutorial.com/tutorial-t...in-VB.Net.aspx
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 12:58 AM.


Sponsored Links



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.