Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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# 激活的表单正在关闭程序_C# - Fatal编程技术网

C# 激活的表单正在关闭程序

C# 激活的表单正在关闭程序,c#,C#,当我从任务栏或通过Alt+Tab激活两个表单时,我正在尝试将它们放在最前面。问题是,如果存在第二个主窗体,我无法关闭主窗体: 这是我使用的代码 private void Haupt_Activated(object sender, EventArgs e) { cardLibrary.Focus(); //Focus the second form this.Focus(); //refocus the first one } 我在做什么

当我从任务栏或通过Alt+Tab激活两个表单时,我正在尝试将它们放在最前面。问题是,如果存在第二个主窗体,我无法关闭主窗体:

这是我使用的代码

   private void Haupt_Activated(object sender, EventArgs e)
    {
        cardLibrary.Focus(); //Focus the second form
        this.Focus(); //refocus the first one
    }
我在做什么

编辑: Haupt是主要形式。 CardLib在Haupt中是可变的 CardLib的名称如下:

    private void cMenuOpenLibrary_Click(object sender, EventArgs e)
    {
        if (!Config.libOpen)
        {
            if (cardLibrary == null) cardLibrary = new CardLib(this);
            cardLibrary.Show();
            cardLibrary.Left = this.Left - cardLibrary.Width - 5;
            cardLibrary.Top = this.Top;
        }
    }

编辑2:我是在装傻。添加通过Haupt_FormClosed关闭CardLib修复了它。

如果您是从

Second.ShowDialog();
然后,当对话框保持焦点直到关闭时,会产生此问题

而不是

Second.ShowDialog();
使用


第二种形式的聚焦如何聚焦第一种形式

  this.Focus(); //refocus the first one

也许最好显示一些打开第二个表单的代码?您正在使用
ShowDialog()
?这将解释为什么不能关闭“主窗体”。为什么用户不能先关闭子窗体,然后关闭主窗体?请改用cardLibrary.Show(此)。
  this.Focus(); //refocus the first one