在 C# 表单中,我需要代码来将第二个表单添加到我现有的表单中。这是我尝试过的:
第一种形式:
public partial class frmMain : Form
{
    public frmMain()
    {
        InitializeComponent();
    }
    private void frmMain_Load(object sender, EventArgs e)
    {
        frmMain fM = new frmMain();
        fM.KeyPress += new KeyPressEventHandler(MMForm);
    }
    private void MMForm(object sender, KeyPressEventArgs e)
    {
        Keys KP; KP = (Keys)sender;
        if (KP == Keys.Escape) { frm2 fM2 = new frm2(); fM2.Show(); }
    }
}
这是第二种形式:
public class frm2 : Form
{
    public frm2()
    {
        frm2 fM2 = new frm2();
        fM2.Height = 200; fM2.Width = 200;
        Controls.AddRange(new System.Windows.Forms.Form[] { fM2 });
    }
}
我错过了什么?
编辑:暂时忘记这一切。即使我按照那里的建议进行操作,当我按下键时也会出现错误。
An unhandled exception of type 'System.InvalidCastException' occurred in Project 09.exe
Additional information: Specified cast is not valid.