Microsoft KB Archive/320768

= BUG: DataGrid Leave Event Fires Leave Event in Adjacent Controls =

Article ID: 320768

Article Last Modified on 5/28/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



This article was previously published under Q320768



SYMPTOMS
When you move from cell to cell in the DataGrid control, the Leave event is triggered for a control that is either before or after the DataGrid control in the tab order. If code is associated with the Leave event of the adjacent control, that code is executed.



CAUSE
A bug in the DataGrid control causes the execution of the Leave event for the adjacent control.



RESOLUTION
To work around this problem, wrap the code in the Leave event of the adjacent control with a conditional statement that tests to determine if the control actually has focus: if (this.textBox1.ContainsFocus==true) //event code here



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
 In Visual C# .NET, create a new Windows Application project. By default, Form1 is created. Add a class to the project.  Replace the code in Class1.cs with the following: public class guitar {   private string make; private string model; private short year; public guitar {   }

public guitar(string Make, string Model, short Year) {       make=Make; model=Model; year=Year; }

public string Make {       get {            return make; }       set {           make = value; }   }    public string Model {       get {            return model; }       set {           model = value; }   }

public short Year {       get {            return year; }       set {           year = value; }   } }                     Close the Class1.cs code window, and then switch to the Form Designer. Add a DataGrid control to Form1. Size the DataGrid control to have four columns and three rows.</li>  Add the following code to the Form1 class: private ArrayList al = new ArrayList; </li> Switch to the Form Designer, right-click the form, and then click Properties.</li> Click the Events icon, and then double-click the Load event to add the Form1_Load event to your code.</li>  Paste the following code in the Form1_Load event: al.Add (new guitar(&quot;Gibson&quot;, &quot;Les Paul&quot;, 1958)); al.Add (new guitar(&quot;Fender&quot;, &quot;Jazz Bass&quot;, 1964)); al.Add (new guitar(&quot;Guild&quot;, &quot;Bluesbird&quot;, 1971)); dataGrid1.DataSource=al; </li> Add a TextBox to Form1.</li>  Add the following code to the Leave event handler for the TextBox: MessageBox.Show(&quot;text box&quot;); To do this, follow these steps:

<ol style="list-style-type: lower-alpha;"> Right-click the TextBox, and then click Properties.</li> Click the Events icon.</li> Type a function name in the Leave event.</li></ol> </li> Compile and run the application.</li> Tab through the grid cells. Note that the Leave event for the TextBox is executed each time that you change cells.</li></ol>

Keywords: kbbug kbfix KB320768

-

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

© Microsoft Corporation. All rights reserved.