Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,


Recently I saw this behavior in MS groupbox added over a User Control
The shift of focus is different and a non-existent or non visibile control is set as focussed.

Steps to reproduce:
1)Create a MS UserControl.

Class Form1 :Form
{
   Form1()
   {
      InitializeComponent();
      UserControl usr = new UserControl1();
      this.Controls.Add(usr);
   }
}

Class UserControl1: UserControl
{
   UserControl1()
   {
   }
}

2)Add a GroupBox to the above usercontrol

Class UserControl1: UserControl
{
   UserControl1()
   {
      GroupBox gpBox = new GroupBox();
      this.Controls.Add(gpBox);
   }
}


3)Add two checkBox on the above groupbox

Class UserControl1: UserControl
{
   UserControl1()
   {
      GroupBox gpBox = new GroupBox();
      CheckBox chkBox1 = new CheckBox();
      CheckBox chkBox2 = new CheckBox();
      chkBox1.CheckedChanged += new System.EventHandler      (this.checked1_CheckedChange);
      gpBox.Controls.Add(chkBox1);
      gpBox.Controls.Add(chkBox2);
      this.Controls.Add(gpBox);
   }

  private void checkBox1_CheckedChanged(object sender, EventArgs e)
  {
  }
}

4)Subscribe to the checkbox checkedchange event.
5)Clear the usercontrol (i.e is the parent control of all) )

private void checkBox1_CheckedChanged(object sender, EventArgs e)
 {
   this.Controls.Clear();
 }

6)Now print this Focussed control on the console.

C#
[DllImport("User32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr GetFocus();

private void checkBox1_CheckedChanged(object sender, EventArgs e)
 {
   this.Controls.Clear();
   IntPtr handle = GetFocus();
   Control focuseedControl = Control.FromHandle(handle);
   Console.WriteLine(focuseedControl.GetType());
 }


7) Output of the above code: "System.Windows.Forms.CheckBox" is focussed control.
Now the question is why is my check box still focussed when I have cleared it already from the UserControl collection.


8) In case I do the same thing with a slight change {like this}
Class UserControl1: UserControl
{
   UserControl1()
   {
      CheckBox chkBox1 = new CheckBox();
      CheckBox chkBox2 = new CheckBox();
      chkBox1.CheckedChanged += new System.EventHandler      (this.checked1_CheckedChange);
      this.Controls.Add(chkBox1);
      this.Controls.Add(chkBox2);
   }
  [DllImport("User32.dll", CharSet=CharSet.Auto)]
  public static extern IntPtr GetFocus();

 private void checkBox1_CheckedChanged(object sender, EventArgs e)
 {
   this.Controls.Clear();
   IntPtr handle = GetFocus();
   Control focuseedControl = Control.FromHandle(handle);
   Console.WriteLine(focuseedControl.GetType());
 }
}

Only difference is groupbox is removed from the Heirarchy. The output of the above is "System.Windows.Forms.UserControl" once the collection is cleared the focus comes to the parent itself and now parent has something what was missing in the first case


Now my Analysis:
1) GroupBox is not ContainerControl it is just derived from System.Windows.Forms.Control. So the focus handling is different as said by MSDN too.
2) .Net Parking window takes the control which has got focussed and then framework tries to shift the focusssss... by calling the SelectNextControl , AssignActiveControl.
3) Clear does not dispose the control.

Question:
I am still not able to understand the Parking Window concept and why is the focus not shifted before the clearing.


Please feel free to contact me if there is some confusion :) waiting for your replies.
Posted
Updated 1-Aug-10 7:26am
v2
Comments
koool.kabeer 1-Aug-10 13:09pm    
please dont use the words like "hell" or "kick" .....
please be polite and be cool......
hope you understand .......
peace of mind solves almost every problem

1 solution

I think that the control is not actually removed fully until it has been garbage collected and its dispose is called - This can't happen in the your example because checkBox1_CheckedChanged references the control checkbox1 in sender. Suffice to say calling clear in a message handler from a control being cleared is a risky strategy.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900