简体   繁体   English

如何保留所有集中的表格?

[英]How can I keep all the focused forms?

I've an application that use two forms: Form1 (main) and Form2 (secondary). 我有一个使用两种形式的应用程序:Form1(主要)和Form2(次要)。 I show the Form2 with the following code: 我用以下代码显示Form2:

Form2 frm = new Form2();
frm.TopMost = true;
frm.Show();

When the Form2 is visible, it hasn't the focus. 当Form2可见时,它没有焦点。 How can I do the focus at the Form2 and keep the focus at the Form1? 如何将焦点集中在Form2上并保持焦点在Form1上? Sorry for my bad english! 对不起,我的英语不好!

try bellow code within MDI Parent Form (Main Form) 在MDI父表单(主表单)中尝试以下代码

private Form2 _form2;

#region UtilOpenForm
    /// <summary>
    /// UtilOpenForm
    /// </summary>
    /// <param name="appContainer"></param>
    /// <param name="childForm"></param>
    private void UtilOpenForm(Form appContainer, Form childForm)
    {
        this.Cursor = Cursors.WaitCursor;
        if (childForm == null)
        {
            throw new ArgumentNullException("childForm");
        }
        childForm.MdiParent = appContainer;
        childForm.StartPosition = FormStartPosition.CenterScreen;
        childForm.MaximizeBox = false;
        childForm.MinimizeBox = false;
        childForm.Closed += new EventHandler(childForm_Closed);
        childForm.Show();
        this.Cursor = Cursors.Default;
    }

Now from Button / Menu click within MDI Parent 现在从按钮/菜单中单击MDI父级

    if (_form2 == null)
        {
            UtilOpenForm(this, _form2 = new Form2());
        }

Now child form close Function within MDI Parent 现在,子窗体在MDI父窗体中关闭功能

   #region childForm_Closed
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void childForm_Closed(object sender, EventArgs e)
    {
        if (sender.GetType() == typeof(Form2))
        {
            _form2.Dispose();

            if (_form2 != null)
            {
                _form2 = null;
            }
        }

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

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