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.
[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.