BUG: The Text property of a data-bound ComboBox returns an incorrect value when you set the value in code (327555)



The information in this article applies to:

  • Microsoft .NET Framework
  • Microsoft Visual Basic 2005 Express Edition
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# 2005, Express Edition
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

This article was previously published under Q327555

SYMPTOMS

If you programmatically set the Text property for a data-bound member of the ComboBox control, the value typed in the text box portion of the ComboBox is displayed as expected. However, the Text property and the SelectedIndex property incorrectly return the value of the last item that was selected in the list box instead of returning the value typed in the text box.

When you type in the ComboBox at runtime, Text property returns the typed value, and SelectedIndex returns a value of -1.

RESOLUTION

To resolve this problem, set the SelectedIndex property to -1 before you set the Text property for a data-bound member of the ComboBox, as in the following examples.

Visual Basic .NET

ComboBox1.SelectedIndex = -1
ComboBox1.Text = "My Text"
				

Visual C# .NET

ComboBox1.SelectedIndex = -1;
ComboBox1.Text = "My Text";
				
NOTE: Do not use the Text property to select a data-bound member of the ComboBox. You must locate the item in the list that you want to show, and then set the SelectedIndex to the index of the item. You do not have to set the Text property.

For example, if you bind the DisplayMember property and the ValueMember property of the ComboBox to a list that contains the numbers 1 through 10, and you want the ComboBox to display the number 5 in the text box and show 5 as selected in the list, you must set the SelectedIndex property to 5.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Visual Basic .NET or Visual Basic 2005 Windows application.

    By default, Form1 is created.
  2. Add an \fbox command, add a TextBox control, and then add a Button control to Form1.
  3. Open a Code window on Form1.
  4. Paste the following code into the Code window below the Windows Form Designer Generated Code section:
        Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
            ' Create a DataTable on which to bind the ComboBox.
            Dim myDT As New DataTable()
    
            Dim myCOL1 As DataColumn = New DataColumn()
            Dim myCOL2 As DataColumn = New DataColumn()
    
            myCOL1.ColumnName = "ContactName"
            myCOL2.ColumnName = "ContactID"
    
            myCOL1.DataType = System.Type.GetType("System.String")
            myCOL2.DataType = System.Type.GetType("System.Int32")
    
            myDT.Columns.Add(myCOL1)
            myDT.Columns.Add(myCOL2)
    
            ' Populate the DataTable.
            Dim myDR As DataRow
    
            myDR = myDT.NewRow
            myDR.Item(0) = "North"
            myDR.Item(1) = 0
            myDT.Rows.Add(myDR)
    
            myDR = myDT.NewRow()
            myDR.Item(0) = "South"
            myDR.Item(1) = 1
            myDT.Rows.Add(myDR)
    
            myDR = myDT.NewRow()
            myDR.Item(0) = "East"
            myDR.Item(1) = 2
            myDT.Rows.Add(myDR)
    
            ' Bind the ComboBox.
            ComboBox1.DataSource = myDT
            ComboBox1.DisplayMember = "ContactName"
            ComboBox1.ValueMember = "ContactID"
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
            ComboBox1.Text = "Hello!"
            MessageBox.Show(ComboBox1.Text)
        End Sub
    					
    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:
  5. Run the application, and then click the Button control.

    The Text property value that is returned is equal to the selected item in the ComboBox, and is not equal to the value that was set programmatically.

Modification Type:MajorLast Reviewed:2/3/2006
Keywords:kbvs2005applies kbvs2005swept kbvs2002sp1sweep kbbug KB327555