简体   繁体   English

输入箭头键时如何防止MDI子窗体更改?

[英]How to prevent MDI child form changing when input arrow keys?

What I implement 我实施的内容

  • My application hold mdi child forms(don't close) when user opens other child to keep last input data. 当用户打开其他子项以保留最后输入数据时,我的应用程序保持mdi子表单(不要关闭)。
  • Child forms are shown as Maximized. 子表单显示为最大化。

What I want to stop 我想停下来

  • When user input arrow keys after over two child forms were opened(I think it's when child form focused), child forms are navigated(Up/Left : prev child open, Down/Right : next child) and shown as Maximized property is released(some case didn't). 当用户输入超过两个子表格后的箭头键(我认为是儿童表格聚焦时),导航子表格(上/左:上一个孩子打开,下/右:下一个孩子)并显示为最大化属性被释放(有些情况没有)。

I have searched for long time, there are some solutions about capturing keys but any solutions for stop this. 我已经搜索了很长时间,有一些关于捕获键的解决方案,但任何解决方案都可以阻止它。

Please help me. 请帮我。

+ conditions to reproduce this problem +重现此问题的条件

  • MDI parent has ToolStripPanel & ToolStrip docking at the right MDI父级在右侧有ToolStripPanel和ToolStrip对接
  • parent also have MenuStrip(Visible property set to false) docking at the top to hide child form's control box 父级还将MenuStrip(Visible属性设置为false)停靠在顶部以隐藏子窗体的控件框
  • ToolStripButton's Click Event Handler show child form using spaghetti function like next ToolStripButton的Click Event Handler使用spaghetti函数显示子窗体,如下一个
private void tsbChildForm1_Click(object sender, EventArgs e)
{
    Cursor.Current = Cursors.WaitCursor;                    
    if (m_frmChild != null)                                 
    {
        if (typeof(Form1) != this.ActiveMdiChild.GetType()) 
        {
            m_frmChild = new Form1();                       
            OpenChildForm(m_frmChild);                      
        }
        else
        {
            // do nothing. prevent memory increase
        }
    }
    else
    {
        m_frmChild = new Form1();                           
        OpenChildForm(m_frmChild);                          
    }
}

private void OpenChildForm(Form frmChild)
{
    if (LoadExistForm(frmChild))
    {
        // do nothing.
    }
    else
    {
        frmChild.MdiParent = this;
        frmChild.WindowState = FormWindowState.Maximized;
        frmChild.Show();
    }
}

private bool LoadExistForm(Form frmChild)
{
    foreach (Form frmEach in this.MdiChildren)
    {
        if (frmEach.Name.Equals(frmChild.Name, StringComparison.OrdinalIgnoreCase))
        {
            frmEach.Select();
            frmEach.WindowState = FormWindowState.Maximized;
            frmChild.Dispose();
            return true;
        }
    }
    return false;
}
  1. Load MDIParent 加载MDIParent 在此输入图像描述
  2. Click each ToolStripButton and Load Child Form 单击每个ToolStripButton和Load Child Form 在此输入图像描述
  3. Push 'Up' arrow key just one time 按“向上”箭头键一次 在此输入图像描述

Add PreviewKeyDown event handler to your MDI Child form. PreviewKeyDown事件处理程序添加到MDI子窗体。 It can filter keys you want your child form to handle. 它可以过滤您希望子表单处理的键。

private void Form_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Up:
        case Keys.Down:
        case Keys.Left:
        case Keys.Right:
            e.IsInputKey = true;
            break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM