Unexpected events are raised when you switch between tab pages with a ComboBox or a ListBox control (820633)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)

SYMPTOMS

You add a ListBox control or a ComboBox control to the TabPage property of the TabControl control. When you switch between the tab pages of the TabControl, the BindingContextChanged event of the ComboBox or of the ListBox is raised. When you bind the ListBox or the ComboBox to a Datasource property and then switch between the tab pages, the BindingContextChanged event, the SelectedValueChanged event, and the SelectedIndexChanged event of the control are raised.

Note In Microsoft Visual Studio .NET 2003, when you bind the ListBox or the ComboBox to a Datasource property and then switch between the tab pages, the BindingContextChanged event and the SelectedValueChanged event of the control are raised.

CAUSE

When you select a particular TabPage property on the TabControl, the Visible property of the TabPage property is set to true, and the SetVisibleCore(True) method is called. This method calls the Control.CreateControl method. The Control.CreateControl method calls the OnBindingContextChanged method. The OnBindingContextChanged method call is at the root of the SelectedValueChanged call, the SelectedIndexChanged call, and the BindingContextChanged call. As a result, the SelectedValueChanged event, the BindingContextChanged event, and the SelectedIndexChanged event are raised.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to reproduce the behavior

Create the form and add the controls

  1. In Visual Studio 2005 or in Visual Studio .NET 2003, create a new Windows Application project by using Microsoft Visual Basic 2005, Microsoft Visual Basic .NET, or Microsoft Visual C# .NET. By default, Form1 is created.
  2. Add a TabControl control to Form1. The TabControl1 control is created.
  3. Right-click the TabControl1 control, and then click Properties.
  4. In the Properties window, click to select the TabPages property, and then click the ellipsis (...) button.
  5. In the TabPage Collection Editor dialog box, click Add two times, and then click OK.
    By default, the TabPage1 and TabPage2 properties are created.
  6. Click the TabPage2 property. Add a ListBox control to the TabControl1 control.
  7. In the Properties window of ListBox1, click to select the Items property, and then click the ellipsis (...) button.
  8. In the String Collection Editor dialog box, type item1, item2, item3, and then click OK.

Add code for the events in the ListBox control

  1. Add the following code to the BindingContextChanged event of the ListBox1 control.

    Visual Basic 2005 or Visual Basic .NET code
     'Print the output in the console when the event is raised.
            Console.WriteLine("BindingContext changed.")
    Visual C# .NET code
    //Print the output in the console when the event is raised.
            Console.WriteLine("BindingContext changed.");
  2. Add the following code to the SelectedValueChanged event of the ListBox1 control.

    Visual Basic 2005 or Visual Basic .NET code
     'Print the output in the console when the event is raised.
            Console.WriteLine("SelectedValue changed.")
    Visual C# .NET code
    //Print the output in the console when the event is raised.
            Console.WriteLine("SelectedValue changed.");
  3. Add the following code to the SelectedIndexChanged event of the ListBox1 control.

    Visual Basic 2005 or Visual Basic .NET code
     'Print the output in the console when the event is raised.
            Console.WriteLine("SelectedIndex changed.")
    Visual C# .NET code
    //Print the output in the console when the event is raised.
            Console.WriteLine("SelectedIndex changed.");
  4. In Solution Explorer, right-click WindowsApplication1, and then click Properties.
  5. In the Output type list, select Console Application, and then click OK.

Build the project

  1. On the Build menu, click Build Solution.
  2. On the Debug menu, click Start. Notice that the BindingContextChanged event is raised one time.
  3. Click TabPage2. The BindingContextChanged event is raised multiple times.
  4. Click TabPage1, and then click TabPage2. The BindingContextChanged event is raised only one time. Notice the output when you switch tab pages repeatedly.

Add a database connection

  1. Return to Form Designer.
  2. On the View menu, click Server Explorer.
  3. In Server Explorer, right-click Data Connections, and then click Add Connection.
  4. On the Connection tab, type your server name, select the Northwind database from the list, and then click OK.
  5. Expand Data Connections, and then expand your server name.
  6. Expand Tables, and then add the Customers table to Form1.
  7. Right-click SqlDataAdapter1, and then click Generate Dataset.
  8. In the Generate Dataset dialog box, click OK.
  9. In the Properties window of the ListBox1 control, set the DataSource property to DataSet11.Customers, and then set the DisplayMember property to City.
  10. Add the following code to the Form1_Load event handler.

    Visual Basic 2005 or Visual Basic .NET code
     SqlDataAdapter1.Fill(DataSet11)
    Visual C# .NET code
    sqlDataAdapter1.Fill(dataSet11);
  11. On the Debug menu, click Start.
  12. The BindingContextChanged, the SelectedValueChanged, and the SelectedIndexChanged events are raised multiple times. Notice the output when you switch tab pages repeatedly. The events are raised only one time.

    Note Notice the output in Visual Studio .NET 2003. The BindingContextChanged and the SelectedValueChanged events are raised multiple times. Notice the output when you switch tab pages repeatly. The events are raised only one time.

    Select an item from the list in the ListBox control, and then switch the tab pages. Only the BindingContextChanged event is raised.

REFERENCES

For more information about the ListBox control, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbEvent kbDataBinding kbTabCtrl kbListBox kbCtrl kbControl kbComboBox kbprb KB820633 kbAudDeveloper