Microsoft KB Archive/327555

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

Article ID: 327555

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft Visual Basic 2005 Express Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



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 = &quot;My Text&quot;

Visual C# .NET
ComboBox1.SelectedIndex = -1; ComboBox1.Text = &quot;My Text&quot;; 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.



Steps to Reproduce the Behavior
 Create a new Visual Basic .NET or Visual Basic 2005 Windows application.

By default, Form1 is created. Add an \fbox command, add a TextBox control, and then add a Button control to Form1. Open a Code window on Form1.  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 = &quot;ContactName&quot; myCOL2.ColumnName = &quot;ContactID&quot;

myCOL1.DataType = System.Type.GetType(&quot;System.String&quot;) myCOL2.DataType = System.Type.GetType(&quot;System.Int32&quot;)

myDT.Columns.Add(myCOL1) myDT.Columns.Add(myCOL2)

' Populate the DataTable. Dim myDR As DataRow

myDR = myDT.NewRow myDR.Item(0) = &quot;North&quot; myDR.Item(1) = 0 myDT.Rows.Add(myDR)

myDR = myDT.NewRow myDR.Item(0) = &quot;South&quot; myDR.Item(1) = 1 myDT.Rows.Add(myDR)

myDR = myDT.NewRow myDR.Item(0) = &quot;East&quot; myDR.Item(1) = 2 myDT.Rows.Add(myDR)

' Bind the ComboBox. ComboBox1.DataSource = myDT ComboBox1.DisplayMember = &quot;ContactName&quot; ComboBox1.ValueMember = &quot;ContactID&quot; End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _     ByVal e As System.EventArgs) Handles Button1.Click ComboBox1.Text = &quot;Hello!&quot; 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:

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

 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.</ol>

Keywords: kbvs2005applies kbvs2005swept kbvs2002sp1sweep kbbug KB327555

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.