我怎样才能保留所有重点表格?

时间:2013-02-12 11:11:15

标签: c# .net focus

我有一个使用两种形式的应用程序:Form1(main)和Form2(secondary)。我使用以下代码显示Form2:

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

当Form2可见时,它没有焦点。如何在Form2上进行焦点并将焦点保持在Form1?抱歉我的英文不好!

1 个答案:

答案 0 :(得分:0)

在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;
    }

现在从按钮/菜单中单击MDI Parent

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

现在,儿童形式关闭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;
            }
        }