Microsoft KB Archive/813031

= FIX: The LostFocus event is raised before the GotFocus event when you press the TAB key to change the focus to a ComboBox control in the .NET Framework 1.0 =

Article ID: 813031

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.0 Service Pack 2
 * Microsoft .NET Framework 1.0 Service Pack 1
 * Microsoft .NET Framework 1.0

-





SYMPTOMS
When you press the TAB key to change the focus to a ComboBox control, the LostFocus event of the ComboBox control is raised before the GotFocus event. This behavior occurs when the DropDownStyle property of the ComboBox control is set to Simple or to DropDown.

Note This problem also occurs when you change the focus to a form that contains the ComboBox control.



CAUSE
A ComboBox control that has the DropDownStyle property set to Simple or to DropDown has an inner Edit control. When you change the focus to the ComboBox control by using the TAB key, the focus is set to the ComboBox control, and then the focus is set to the inner Edit control. The LostFocus event is raised when the focus is set to the inner Edit control.



RESOLUTION
To resolve this problem, obtain the latest service pack for the Microsoft .NET Framework 1.0. To download the latest service pack, visit the following Microsoft Web site:

http://www.microsoft.com/downloads/details.aspx?familyid=6978D761-4A92-4106-A9BC-83E78D4ABC5B&displaylang=en



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section. This problem was first corrected in the Microsoft .NET Framework 1.0 Service Pack 3.



Steps to reproduce the behavior
 Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. The New Project dialog box appears. Under Project Types, click Visual Basic Projects. Under Templates, click Windows Application. In the Name box, type MyApp, and then click OK. By default, one of the following files is created: <ul> If you are using Microsoft Visual Basic .NET, a file that is named Form1.vb is created.</li> If you are using Microsoft Visual C# .NET, a file that is named Form1.cs is created.</li></ul> </li> Add a TextBox control and a ComboBox control to the Form1 form.</li> Right-click the ComboBox control, and then click Properties.</li> In the Properties window, set the DropDownStyle property to Simple or to DropDown.</li> In Solution Explorer, use one of the following procedures: <ul> If you are using Visual Basic .NET, right-click Form1.vb, and then click View Code.</li> If you are using Visual C# .NET, right-click Form1.cs, and then click View Code.</li></ul> </li> Use one of the following procedures, depending on the programming language that you are using: <ul> If you are using Visual Basic .NET, follow these steps: <ol style="list-style-type: lower-alpha;">  Locate the following code in the Form1.vb file. End Class </li>  Add the following code before the code that you located in step a. Private Sub ComboBox1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.GotFocus System.Diagnostics.Debug.WriteLine(&quot;ComboBox1 GotFocus&quot;) End Sub Private Sub ComboBox1_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.LostFocus System.Diagnostics.Debug.WriteLine(&quot;ComboBox1 LostFocus&quot;) End Sub </li></ol> </li> If you are using Visual C# .NET, follow these steps: <ol style="list-style-type: lower-alpha;">  Locate the following code in the Form1.cs file. this.comboBox1.Name = &quot;comboBox1&quot;; </li>  Add the following code after the code that you located in step a. this.comboBox1.GotFocus += new System.EventHandler(this.comboBox1_GotFocus); this.comboBox1.LostFocus += new System.EventHandler(this.comboBox1_LostFocus); </li>  Add the following code after the Main procedure. private void comboBox1_GotFocus(object sender, System.EventArgs e) { System.Diagnostics.Debug.WriteLine(&quot;comboBox1_GotFocus&quot;); } private void comboBox1_LostFocus(object sender, System.EventArgs e) { System.Diagnostics.Debug.WriteLine(&quot;comboBox1_LostFocus&quot;); } </li></ol> </li></ul> </li> <li>On the Build menu, click Build Solution.</li> <li>On the Debug menu, click Start. Press TAB.</li></ol>

In the Output window, you can see that the LostFocus event of the ComboBox control occurs before the GotFocus event.

<div class="references_section">