Using VB.NET To Code The VB6 ItemData Property
About Visual Basic reader Peter wrote with yet another VB6 to VB.NET question:
"In VB6 I made a lot of use of the ItemData property. I understand that this isn't offered in VB.NET except through the VB6 compatibility object."
Right you are, Peter. At least there is some support. They don't give you anything to replace Control Arrays and Lines.
Peter is saying that in VB6, you can associate a number with the value in a ListBox or ComboBox.
For example (remember ... this is VB6), if the items in a ComboBox are:
Then you can associate the numbers ...
... with those items. This gives you a way to use numbers instead of whatever else is in your ComboBox to do magic stuff. And Peter is w-a-a-y into magic stuff.
Peter could use the VB6 compatibility object to do things the same old way ... wellll ... a "similar" new way. In place of the old VB6 ...
Peter could have coded the VB.NET ...
Oh ... and you also have to add a Reference to your project for the Microsoft.VisualBasic.Compatibility namespace and use an Imports statement to import the VB6 library of that namespace. Which illustrates an important reason why Peter - wise fellow that he is - declined to do it this way. Microsoft doesn't guarantee that this VB6 stuff will be supported. They pulled the "flush" chain on VB6 already. They're not likely to do VB6 any favors in the future.
That's why they make you dance around the barn to use it in your project now.
The good news is that doing this the .NET way is - not easier - but certainly more OOP! (Object Oriented Programming) Microsoft's reasoning goes like this. Using VB6 ItemData solves 90 percent of the problems most programmers will have in populating ListBoxes and GroupBoxes. (Maybe 99 percent.) But it doesn't get 100 percent of them. There will be times when associating a simple integer with an item just won't do the job. Microsoft is into doing one hundred percent these days. There is no problem that can't be solved and once you get used to doing it this way, it will start to seem like, "Why wasn't it always this way."
Peter adds, "the Microsoft documentation isn't being helpful again." Right again, Peter. If you look up their documentation, they tell you "what" but they don't even mention "why". Here's why.
If you think about it, An integer ItemData is just one of a lot of "properties" that you might want to associate with an item in a ComboBox. You might want to associate a color with a time of day. You might want to associate a printer with a MapIndex property that you created in your own program. An integer ItemData is just too limiting. The .NET way lets you do the simple integer association, but it will also do anything else. And the way you create this kind of association is to create your own object. In VB.NET, most objects are Classes. (They can also be Structures. Structures are covered on the next page.) So ... Add a new Class to your project. I called mine myIDC (for "my ItemData Class"). Here's the code for the Class.
This is about as standard as classes get. A New method expects a DateTime type and a Color type. There are two properties, one returns a DateTime value and the other just returns a string with the name of the color for simplicity. The final Function is necessary because the ComboBox uses the very fundamental ToString method of all objects (see The Object Object Quick Tip here on this site) to return values in the Item property.
You can create a new myIDC object this way:
This object associates Black with 5:00 AM. Sounds right to me.
The ComboBox is populated this way:
And you can use the properties of the myIDC object this way:
Which yields:
Peter also had a question about using a Structure instead of a Class. I answer that question on the next page!
"In VB6 I made a lot of use of the ItemData property. I understand that this isn't offered in VB.NET except through the VB6 compatibility object."
Right you are, Peter. At least there is some support. They don't give you anything to replace Control Arrays and Lines.
Peter is saying that in VB6, you can associate a number with the value in a ListBox or ComboBox.
For example (remember ... this is VB6), if the items in a ComboBox are:
Black
White
Red
Blue
Then you can associate the numbers ...
4
1
3
2
... with those items. This gives you a way to use numbers instead of whatever else is in your ComboBox to do magic stuff. And Peter is w-a-a-y into magic stuff.
Peter could use the VB6 compatibility object to do things the same old way ... wellll ... a "similar" new way. In place of the old VB6 ...
myComboBox.ItemData(myComboBox.ListIndex)
Peter could have coded the VB.NET ...
GetItemData(myComboBox, myComboBox.SelectedIndex)
Oh ... and you also have to add a Reference to your project for the Microsoft.VisualBasic.Compatibility namespace and use an Imports statement to import the VB6 library of that namespace. Which illustrates an important reason why Peter - wise fellow that he is - declined to do it this way. Microsoft doesn't guarantee that this VB6 stuff will be supported. They pulled the "flush" chain on VB6 already. They're not likely to do VB6 any favors in the future.
That's why they make you dance around the barn to use it in your project now.
The good news is that doing this the .NET way is - not easier - but certainly more OOP! (Object Oriented Programming) Microsoft's reasoning goes like this. Using VB6 ItemData solves 90 percent of the problems most programmers will have in populating ListBoxes and GroupBoxes. (Maybe 99 percent.) But it doesn't get 100 percent of them. There will be times when associating a simple integer with an item just won't do the job. Microsoft is into doing one hundred percent these days. There is no problem that can't be solved and once you get used to doing it this way, it will start to seem like, "Why wasn't it always this way."
Peter adds, "the Microsoft documentation isn't being helpful again." Right again, Peter. If you look up their documentation, they tell you "what" but they don't even mention "why". Here's why.
If you think about it, An integer ItemData is just one of a lot of "properties" that you might want to associate with an item in a ComboBox. You might want to associate a color with a time of day. You might want to associate a printer with a MapIndex property that you created in your own program. An integer ItemData is just too limiting. The .NET way lets you do the simple integer association, but it will also do anything else. And the way you create this kind of association is to create your own object. In VB.NET, most objects are Classes. (They can also be Structures. Structures are covered on the next page.) So ... Add a new Class to your project. I called mine myIDC (for "my ItemData Class"). Here's the code for the Class.
Public Class myIDC
Private sTime As DateTime
Private sID As String
Public Sub New( _
ByVal Time As DateTime, _
ByVal ID As Color)
sTime = Time
sID = ID.Name
End Sub
Public Property theTime() As DateTime
Get
Return sTime
End Get
Set(ByVal iValue As DateTime)
sTime = iValue
End Set
End Property
Public Property theColor() As String
Get
Return sID
End Get
Set(ByVal iValue As String)
sID = iValue
End Set
End Property
Public Overrides Function ToString() As String
Return sTime
End Function
End Class
This is about as standard as classes get. A New method expects a DateTime type and a Color type. There are two properties, one returns a DateTime value and the other just returns a string with the name of the color for simplicity. The final Function is necessary because the ComboBox uses the very fundamental ToString method of all objects (see The Object Object Quick Tip here on this site) to return values in the Item property.
You can create a new myIDC object this way:
New myIDC(#5:00:00 AM#, Color.Black)
This object associates Black with 5:00 AM. Sounds right to me.
The ComboBox is populated this way:
With ComboBox1
.Items.Add( _
New myIDC(#5:00:00 AM#, Color.Black))
.Items.Add( _
New myIDC(#5:00:00 PM#, Color.Gold))
.Items.Add( _
New myIDC(#12:00:00 PM#, Color.OrangeRed))
.Items.Add( _
New myIDC(#10:00:00 AM#, Color.Chocolate))
.SelectedIndex = 0
End With
And you can use the properties of the myIDC object this way:
Dim TimeColor As myIDC
TimeColor = ComboBox1.Items(0)
MsgBox( _
"When I get up at " _
& TimeColor.theTime.ToString("t") & _
" my mood is " & _
TimeColor.theColor)
Which yields:
"When I get up at 5:00 AM my mood is Black"
Peter also had a question about using a Structure instead of a Class. I answer that question on the next page!