Microsoft KB Archive/820633

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

Article ID: 820633

Article Last Modified on 11/26/2007

-

APPLIES TO


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

-





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.



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.
 * 1) Click the TabPage2 property. Add a ListBox control to the TabControl1 control.
 * 2) In the Properties window of ListBox1, click to select the Items property, and then click the ellipsis (...) button.
 * 3) In the String Collection Editor dialog box, type item1, item2 , item3 , and then click OK.

Add code for the events in the ListBox control
  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(&quot;BindingContext changed.&quot;) Visual C# .NET code //Print the output in the console when the event is raised. Console.WriteLine(&quot;BindingContext changed.&quot;);   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(&quot;SelectedValue changed.&quot;) Visual C# .NET code //Print the output in the console when the event is raised. Console.WriteLine(&quot;SelectedValue changed.&quot;);   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(&quot;SelectedIndex changed.&quot;) Visual C# .NET code //Print the output in the console when the event is raised. Console.WriteLine(&quot;SelectedIndex changed.&quot;);  In Solution Explorer, right-click WindowsApplication1, and then click Properties. In the Output type list, select Console Application, and then click OK.</ol>

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
<ol> Return to Form Designer.</li> On the View menu, click Server Explorer.</li> In Server Explorer, right-click Data Connections, and then click Add Connection.</li> On the Connection tab, type, select the Northwind database from the list, and then click OK.</li> Expand Data Connections, and then expand .</li> Expand Tables, and then add the Customers table to Form1.</li> Right-click SqlDataAdapter1, and then click Generate Dataset.</li> In the Generate Dataset dialog box, click OK.</li> In the Properties window of the ListBox1 control, set the DataSource property to DataSet11.Customers, and then set the DisplayMember property to City.</li>  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); </li> On the Debug menu, click Start.</li> 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.</li></ol>

<div class="references_section">