Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7839087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:19:19+00:00 2026-06-02T15:19:19+00:00

in C# forms I need code to add a second form to my existing.

  • 0

in C# forms I need code to add a second form to my existing. this is what I’ve tried:

First form:

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(); }

    }
}

And this is Second form:

public class frm2 : Form
{
    public frm2()
    {
        frm2 fM2 = new frm2();
        fM2.Height = 200; fM2.Width = 200;
        Controls.AddRange(new System.Windows.Forms.Form[] { fM2 });
    }

}

What am I missing?

EDIT: forget all this for a moment. Even if I do it as suggested down there I get an error when I press the key.

An unhandled exception of type 'System.InvalidCastException' occurred in Project 09.exe
Additional information: Specified cast is not valid.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:19:20+00:00Added an answer on June 2, 2026 at 3:19 pm

    You could do this:

    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            this.private void MMForm(object sender, KeyPressEventArgs e)
        }
        private void MMForm(object sender, KeyPressEventArgs e)
         {
            if (e.KeyChar == Convert.ToChar(((int)Keys.Escape)))
            {
                frm2 fM2 = new frm2(); fm2.Height=200; fm2.Width=200; fM2.Show(); 
            }
    }
    
    public class frm2 : Form 
    { 
        public frm2() 
        {  
            InitializeComponent();
        } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSF page with two forms (pseudo-code below). First form is for
I have this html <form method=POST action= id=order class=forms> <div> <span class=col1>Ref.</span> <span class=col2>Number</span>
I have two forms, one with a button 'Add' which loads the second form
I need to have a checkbox which ajax-submits a form. The following code throws
I have a form MyForm : System.Windows.Forms.Form {} and I need to call Site.GetService(..)
I need TextBox which will reflect changes in databound string. I tried following code:
I engaged a problem with inherited Controls in Windows Forms and need some advice
I have a few large forms that I need to provide visual cues about
I am a newbie in windows forms and I need to store a string
I need to deploy a Windows Forms application using ClickOnce deployment. (VS2008, .NET 3.5)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.