Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Winforms在关注子控件时会失去焦点。。。有时_C#_Winforms_User Controls_Focus_Compact Framework - Fatal编程技术网

C#Winforms在关注子控件时会失去焦点。。。有时

C#Winforms在关注子控件时会失去焦点。。。有时,c#,winforms,user-controls,focus,compact-framework,C#,Winforms,User Controls,Focus,Compact Framework,我正在开发一个windows mobile 6.5应用程序,当关注添加到表单中的UserControl的子控件时,我的表单会失去焦点,这是一个问题。。。有时候 在我的应用程序中,我通过在主窗体控件集合中添加和删除用户控件来在屏幕之间导航 IView nextView = (IView)Activator.CreateInstance(map.View); this.Controls.Remove((UserControl)this.currentView); this.currentView

我正在开发一个windows mobile 6.5应用程序,当关注添加到表单中的UserControl的子控件时,我的表单会失去焦点,这是一个问题。。。有时候

在我的应用程序中,我通过在主窗体控件集合中添加和删除用户控件来在屏幕之间导航

IView nextView = (IView)Activator.CreateInstance(map.View);

this.Controls.Remove((UserControl)this.currentView);

this.currentView = nextView;

this.Controls.Add((UserControl)this.currentView);
因此,我在屏幕之间导航的基本流程是:

首次添加用户控件A

  • 初始化用户控件A
  • 将UserControl添加到Form.Controls
  • 将重点放在网格上
  • 导航到用户控件B

  • 初始化用户控件B
  • 从Form.Controls中删除UserControl A
  • 将UserControl B添加到Form.Controls
  • 导航回UserControl A

  • 初始化用户控件A
  • 从Form.Controls中删除UserControl B
  • 将UserControl添加到Form.Controls
  • 将重点放在网格上
  • 所有这些都可以很好地工作,在第一次添加UserControl A时,表单是聚焦的,子控件也是聚焦的

    但是,当我从UserControl B导航回UserControl A时,表单会失去焦点,但只有在最后一步中我尝试将焦点放在子控件上时,表单才会失去焦点。在我的应用程序中,这导致我无法使用手机硬件按钮在网格中导航


    为什么主窗体失去焦点?

    证明当我导航回UserControl A时,我通过触发方法中的导航事件来触发,UserControl B中的代码继续执行,从而防止对UserControl A.

    的关注。 我曾希望,当我从Form.Controls中删除UserControlB(然后处理它)时,该方法的其余部分不会被执行,就像Response.Redirect为web所做的那样


    我通过添加一个报税表解决了这个问题;在UserControl B中触发OnNavigation事件后的语句。

    为什么要删除/添加this.currentView?这只是一种预感,但可能在删除旧视图之前尝试添加新视图,以便表单上始终有一个可聚焦控件。您可以使用Remote SPY连接到应用程序,Remote SPY是Visual Studio 2008远程工具的一部分。此工具帮助您捕获WM_SETFOCUS和WM_KILLFOCUS消息。也许这有助于更好地了解情况。请给我们看一下焦点代码。