c# - 在 Mdi 子项之间切换

标签 c# .net winforms mdi

我有一个 MDI 窗体,我在其中创建了两个 MDIChild 窗体,cf1 和 cf2。 我有一个按钮,单击该按钮时,应在 cf1 和 cf2 之间切换“焦点”(即哪个表单“在顶部”)。 在 MDI 父级的构造函数中,我分配了各自 cf1 和 cf2 的 MDIParent。然后我执行 cf1.Show(),然后执行 cf2.Show(),因此 cf2 最终“在顶部”或“集中”。 当我第一次按下切换按钮时,cf1 变为焦点,而 cf2 变为非事件状态。 在此之后更改 z 顺序的任何进一步尝试均告失败。

我试过 Activate、ActivateMdiChild、TopMost 和 BringToFront,都没有成功。

using System;
using System.Windows.Forms;

namespace MDITest
{
    public partial class Form1 : Form
    {
        private readonly ChildForm cf1 = new ChildForm();
        private readonly ChildForm cf2 = new ChildForm();

        public Form1()
        {
            InitializeComponent();
            cf1.MdiParent = this;
            cf2.MdiParent = this;
            cf1.Text = "Window 1";
            cf2.Text = "Window 2";
            cf1.Show();
            cf2.Show();
        }

        private void Child_WMSize(object sender, EventArgs e)
        {
            LblWindow1State.Text = $"Window 1 - {cf1.WindowState.ToString()}";
            LblWindow2State.Text = $"Window 2 = {cf2.WindowState.ToString()}";
        }

        private void BtnFocus_Click(object sender, EventArgs e)
        {
            //if (ActiveMdiChild == cf1) cf2.Activate();
            //if (ActiveMdiChild == cf2) cf1.Activate();

            //if (ActiveMdiChild == cf1) ActivateMdiChild(cf2);
            //if (ActiveMdiChild == cf2) ActivateMdiChild(cf1);

            //if (ActiveMdiChild == cf1) cf2.TopMost = true;
            //if (ActiveMdiChild == cf2) cf1.TopMost = true;

            if (ActiveMdiChild == cf1) cf2.BringToFront();
            if (ActiveMdiChild == cf2) cf1.BringToFront();
        }
    }
}

预期的结果是根据需要在两个表单之间切换焦点。实际结果是我只能从cf2改成cf1,不能反过来。

我什至尝试了霰弹枪方法:

    if (ActiveMdiChild == cf1)
    {
        cf2.BringToFront();
        cf2.Activate();
        ActivateMdiChild(cf2);
        cf2.TopMost = true;
        cf1.TopMost = false;
    }
    if (ActiveMdiChild == cf2)
    {
        cf1.BringToFront();
        cf1.Activate();
        ActivateMdiChild(cf1);
        cf1.TopMost = true;
        cf2.TopMost = false;
    }

我的结果没有变化。

最佳答案

您可以使用以下代码在任意数量的 Mdi 子窗口之间切换:

if (MdiChildren.Length == 0)
    return;
var i = Array.IndexOf(MdiChildren, ActiveMdiChild);
MdiChildren[(MdiChildren.Length + i + 1) % MdiChildren.Length].Activate();

或者

this.Controls.OfType<MdiClient>().First()
    .SelectNextControl(ActiveMdiChild, true,true, true, true);

或者

SendKeys.SendWait("^{TAB}"); // Control + TAB as well as Control + F6

enter image description here

关于c# - 在 Mdi 子项之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57311961/

相关文章:

c# - WPF DataGrid 性能问题

c# - EF 模型查询 SELECT WHERE NOT IN

.net - 如何访问 WIF 4.5 中的 SecurityTokenHandlers 集合?

c# - 当用户单击标题栏中的十字时,如何显示 MessageBox 提示

c# - 自动更新 C# Windows 窗体应用程序

c# - 带有 Mono 4.2.3 的 Web Api 2

c# - 使用通用基类作为方法的参数

c# - 在 foreach 循环中编辑字典值

c# - 错误 : Two output file names resolved to the same output path: "obj\Debug\Project1.Form1.resources"

c# - 在 flowLayout 中的控件之间设置更多空间