ItemData property does not exist for ListBox and ComboBox controls

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 "More Information" section.

Status

This behavior is by design.

More Information

Steps to reproduce the behavior

Build a Visual Basic 6.0 sample for upgrade

  1. Create a new Standard EXE project in Visual Basic 6.0. Form1 is created by default.
  2. Add a ListBox control and a Label control to Form1.
  3. 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
  4. Click the ItemData property of the ListBox control, and then add the following values:

    1001
    1002
    1004
    1005
  5. 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) & " "
    Msg = Msg & List1.List(List1.ListIndex)
    Label1.Caption = Msg
    End Sub
  6. Save your project.
  7. On the Run menu, click Start to run your project. Ensure that the project runs without any errors.
  8. Save any changes, and then close the Visual Basic environment.

Upgrade the Visual Basic 6.0 project to Visual Basic .NET or Visual Basic 2005

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Basic 2005.
  2. 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.
  3. Follow the instructions on the screen to upgrade the project.
  4. 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="vbup2075"'
    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) & " "
    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
  5. 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.
  6. 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)
  7. Save your project. On the Debug menu, click Start to run your project.
  8. 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("Carol Philips", 1001))

Build a new project in Visual Basic .NET or Visual Basic 2005

  1. 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:

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

  2. Place a ComboBox and a Label control on Form1.
  3. 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.
  4. Rename the class MyList.vb, and then click OK.
  5. 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 = ""
    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
  6. In Solution Explorer, double-click Form1.vb to open the Design window of Form1.
  7. 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("Carol Philips", 101))
    .Items.Add(New MyList("Jim Kim", 102))
    .Items.Add(New MyList("Jossef Goldberg", 103))
    .Items.Add(New MyList("Patricia Doyle", 104))
    .SelectedIndex = 0 'Set first item as selected item.
    End With
  8. In Solution Explorer, double-click Form1.vb to open the Design window of Form1.
  9. 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 & " " & mList.Name
    'Alternately, you can use the following syntax.
    'Label1.Text = ComboBox1.Items(ComboBox1.SelectedIndex).ItemData _
    ' & " " & ComboBox1.Items(ComboBox1.SelectedIndex).ToString
  10. Save your project. On the Debug menu, click Start to run the project.
  11. Select any item in the combo box. Notice that the ItemData property of the ComboBox control appears with Employee Name in the Label control.
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 "References" section.

References

For more information about how to upgrade the ItemData property from Visual Basic 6.0 to Visual Basic .NET or Visual Basic 2005, refer to the "ItemData property cannot be upgraded" topic in the Visual Studio .NET or Visual Basic 2005 Online Help documentation.

To better understand the differences between a class and a structure, refer to the "Structures and Classes" topic in the Visual Studio .NET or Visual Basic 2005 Online Help documentation.

Note The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, email address, logo, person, places, or events is intended or should be inferred.
Proprietăți

ID articol: 311340 - Ultima examinare: 29 mar. 2007 - Revizie: 1

Feedback