Microsoft KB Archive/311340

= ItemData property does not exist for ListBox and ComboBox controls =

Article ID: 311340

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q311340



SYMPTOMS
You may notice the following symptoms:
 * When you upgrade a Microsoft Visual Basic 6.0 project to Visual Basic .NET or Visual Basic 2005, the data that is listed in the Properties window for the ItemData property is discarded.
 * In Visual Basic 6.0, you can set the ItemData property for a ListBox or a ComboBox control at design time through the Properties window. In Visual Basic .NET or in Visual Basic 2005, the ItemData property no longer exists for these controls.



RESOLUTION
To resolve this problem, use one of the following methods:
 * If you upgraded a project from Visual Basic 6.0 to Visual Basic .NET or Visual Basic 2005, use the VB6.SetItemData method (from the Visual Basic 6.0 compatibility library) to fill the list. This method is usually called in the constructor of the form (the Public Sub New procedure).
 * For a new Visual Basic .NET or Visual Basic 2005 project, use a class to hold the list items. For more information, refer to the &quot;More Information&quot; section.



STATUS
This behavior is by design.



Build a Visual Basic 6.0 sample for upgrade
 Create a new Standard EXE project in Visual Basic 6.0. Form1 is created by default. Add a ListBox control and a Label control to Form1.  Select the ListBox control. In the Property window of the ListBox control, click List Property, and then add the following items. Press the CTRL+ENTER key combination to move to the next item.     Carol Philips Jim Kim Jossef Goldberg Patricia Doyle   Click the ItemData property of the ListBox control, and then add the following values:     1001 1002    1004     1005                      Right-click Form1, and then click View Code. Add the following code in the Click event procedure of the ListBox control: Private Sub List1_Click Dim Msg As String 'Add the employee number and the employee name. Msg = List1.ItemData(List1.ListIndex) & &quot; &quot; Msg = Msg & List1.List(List1.ListIndex) Label1.Caption = Msg End Sub </li> Save your project.</li> On the Run menu, click Start to run your project. Ensure that the project runs without any errors.</li> Save any changes, and then close the Visual Basic environment.</li></ol>

Upgrade the Visual Basic 6.0 project to Visual Basic .NET or Visual Basic 2005
<ol> Start Microsoft Visual Studio .NET or Microsoft Visual Basic 2005.</li> On the Project menu, point to File, and then click Open. Select the .vbp file that you saved in the previous section, and then click OK.</li> Follow the instructions on the screen to upgrade the project.</li>  When the upgrade is complete, go to the Code window of Form1. If the Code window is not available, right-click Form1 in Solution Explorer, and then click View Code.

Notice that the Click event procedure code in the Visual Basic 6.0 application is converted to the SelectedIndexChanged event of the ListBox control in Visual Basic .NET or in Visual Basic 2005. The code that retrieves the ItemData information now appears as follows (or similar): 'UPGRADE_WARNING: Event List1.SelectedIndexChanged may fire when form is initialized. 'Click for more: ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword=&quot;vbup2075&quot;' Private Sub List1_SelectedIndexChanged(ByVal eventSender As _ System.Object, ByVal eventArgs As System.EventArgs) Handles _ List1.SelectedIndexChanged Dim Msg As String 'Add the employee number and the employee name. Msg = VB6.GetItemData(List1, List1.SelectedIndex) & &quot; &quot; Msg = Msg & VB6.GetItemString(List1, List1.SelectedIndex) Label1.Text = Msg End Sub Note This code does not require any changes. However, you can replace the VB6.GetItemString method with the Visual Basic .NET or Visual Basic 2005 syntax as follows: Msg = Msg & List1.Items(List1.SelectedIndex).ToString </li> Before you can run this application successfully, you must use the SetItemData method to add ItemData. To do this, click to expand Windows Form Designer generated code, and then click to expand the Public Sub New method if it is not already displayed.</li>  Add the following code after the InitializeComponent statement in the Sub New procedure: VB6.SetItemData(List1, 0, 1001) VB6.SetItemData(List1, 1, 1002) VB6.SetItemData(List1, 2, 1004) VB6.SetItemData(List1, 3, 1005) </li> Save your project. On the Debug menu, click Start to run your project.</li>  Click any item in the list box. Notice that the ItemData property of the ListBox appears with Employee Name in the Label control.

Note If you added ItemData and ListItems programmatically in the Visual Basic 6.0 project, your upgraded Visual Basic .NET project uses the following syntax to populate ListItems with ItemData in the Form_Load procedure: List1.Items.Add(New VB6.ListBoxItem(&quot;Carol Philips&quot;, 1001)) </li></ol>

Build a new project in Visual Basic .NET or Visual Basic 2005
<ol> Create a new Visual Basic .NET Windows Application project. Form1 is added to the project by default.

Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

</li> Place a ComboBox and a Label control on Form1.</li> In Solution Explorer, right-click Project name, point to Add, and then click Add Class. Make sure that the Visual Basic class is highlighted.

Note In Visual Basic 2005, right-click Project name, point to Add, and then click Class.</li> Rename the class MyList.vb, and then click OK.</li> <li> Add the following code to the Public Class MyList section in the Code window of the MyList class: Private sName As String Private iID As Integer  'You can also declare this as String.

Public Sub New sName = &quot;&quot; iID = 0 End Sub

Public Sub New(ByVal Name As String, ByVal ID As Integer) sName = Name iID = ID   End Sub

Public Property Name As String Get Return sName End Get

Set(ByVal sValue As String) sName = sValue End Set End Property

Public Property ItemData As Integer Get Return iID End Get

Set(ByVal iValue As Integer) iID = iValue End Set End Property

Public Overrides Function ToString As String Return sName End Function </li> <li>In Solution Explorer, double-click Form1.vb to open the Design window of Form1.</li> <li> Double-click the form to go to the Form1_Load procedure, and then add the following code to the Form1_Load procedure: With ComboBox1 .Items.Add(New MyList(&quot;Carol Philips&quot;, 101)) .Items.Add(New MyList(&quot;Jim Kim&quot;, 102)) .Items.Add(New MyList(&quot;Jossef Goldberg&quot;, 103)) .Items.Add(New MyList(&quot;Patricia Doyle&quot;, 104)) .SelectedIndex = 0  'Set first item as selected item. End With </li> <li>In Solution Explorer, double-click Form1.vb to open the Design window of Form1.</li> <li> Double-click the ComboBox1 control, and then add the following code to the ComboBox1_SelectedIndexChanged procedure: Dim mList As MyList mList = ComboBox1.Items(ComboBox1.SelectedIndex) 'In the following statement, you can either use mList.ToString or       'mList.Name. They both return the Name property. Label1.Text = mList.ItemData & &quot; &quot; & mList.Name 'Alternately, you can use the following syntax. 'Label1.Text = ComboBox1.Items(ComboBox1.SelectedIndex).ItemData _ ' & &quot; &quot; & ComboBox1.Items(ComboBox1.SelectedIndex).ToString </li> <li>Save your project. On the Debug menu, click Start to run the project.</li> <li>Select any item in the combo box. Notice that the ItemData property of the ComboBox control appears with Employee Name in the Label control.</li></ol>

Note You can also use a structure instead of a class in a similar way. For more information about how to use class versus structure in Visual Basic .NET or Visual Basic 2005, refer to the &quot;References&quot; section.

<div class="references_section">