|
#1
|
|||
|
|||
|
Itemdata in Combo box
How to get the Itemdata functionality present in VB6 in Vb.Net. Pls help |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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 > > |
|
#6
|
|||
|
|||
|
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 > > > > > |
|
#7
|
|||
|
|||
|
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 > |
|
#8
|
|||
|
|||
|
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 > > > > |
|
#9
|
|||
|
|||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|