单击子项时清除UserControl集合. [英] Clearing the UserControl Collection while clicking on the child.

查看:94
本文介绍了单击子项时清除UserControl集合.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


最近,我在通过用户控件添加的MS groupbox中看到了此行为
焦点的移动不同,将不存在或不可见的控件设置为焦点.

复制步骤:
1)创建一个MS UserControl.

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)在上面的用户控件中添加一个GroupBox


2)Add a GroupBox to the above usercontrol

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



3)在上面的组框上添加两个复选框



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)订阅复选框checkedchange事件.
5)清除用户控件(即所有控件的父控件))


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)现在在控制台上打印此Focusseded控件.


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)上面代码的输出:"System.Windows.Forms.CheckBox"是焦点控件.
现在的问题是,当我已经从UserControl集合中清除它时,为什么我的复选框仍然集中显示.


8)如果我做同样的事情但稍有改变{像这样}



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());
 }
}


唯一的区别是,组框已从继承关系中删除.清除集合后,上面的输出是"System.Windows.Forms.UserControl".焦点集中在父级本身上,现在父级在第一种情况下缺少了某些内容


现在我的分析:
1)GroupBox不是ContainerControl,它只是派生自System.Windows.Forms.Control.因此,焦点处理也与MSDN所说的有所不同.
2).Net Parking窗口获取已获得焦点的控件,然后框架通过调用SelectNextControl,AssignActiveControl尝试转移焦点.
3)清除不会配置控件.

问题:
我仍然无法理解停车窗口"的概念,为什么在清理之前焦点没有转移.


如果有任何混淆,请随时与我联系:)等待您的答复.


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.

推荐答案

我认为该控件直到被垃圾回收后才真正被完全删除.收集并调用其处置-在您的示例中不会发生这种情况,因为checkBox1_CheckedChanged引用了发件人中的控件checkbox1.只需说从清除的控件中的消息处理程序中调用清除是一种冒险的策略.
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.


这篇关于单击子项时清除UserControl集合.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆