BUG: DataGrid Leave Event Fires Leave Event in Adjacent Controls (320768)



The information in this article applies to:

  • Microsoft .NET Framework 1.0
  • Microsoft Windows .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.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. In Visual C# .NET, create a new Windows Application project. By default, Form1 is created.
  2. Add a class to the project.
  3. 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; 
    		}
    	}
    }
    					
  4. Close the Class1.cs code window, and then switch to the Form Designer.
  5. Add a DataGrid control to Form1. Size the DataGrid control to have four columns and three rows.
  6. Add the following code to the Form1 class:
    private ArrayList al = new ArrayList();	
    					
  7. Switch to the Form Designer, right-click the form, and then click Properties.
  8. Click the Events icon, and then double-click the Load event to add the Form1_Load event to your code.
  9. Paste the following code in the Form1_Load event:
    al.Add (new guitar("Gibson", "Les Paul", 1958));
    al.Add (new guitar("Fender", "Jazz Bass", 1964));
    al.Add (new guitar("Guild", "Bluesbird", 1971));
    	
    dataGrid1.DataSource=al;
    					
  10. Add a TextBox to Form1.
  11. Add the following code to the Leave event handler for the TextBox:
    MessageBox.Show("text box");
    To do this, follow these steps:

    1. Right-click the TextBox, and then click Properties.
    2. Click the Events icon.
    3. Type a function name in the Leave event.
  12. Compile and run the application.
  13. Tab through the grid cells. Note that the Leave event for the TextBox is executed each time that you change cells.

Modification Type:MinorLast Reviewed:5/28/2003
Keywords:kbbug kbfix KB320768 kbAudDeveloper