Multiple Forms in C#
This tutorial, you will learn how to create a program that will use mulitple forms. Basically, many programmers have many forms that are accessible from the main form that loads at start up. At this time, we’re ging to create a very simple application that has a single button. And hen this button is clicked, it loads the log in form. And this form looks like as shown below.
Next, we’re going to add another form. To do this, here’s the following steps:
1.Go to Solution Explorer
2.Right click project and select Add
3.Then Choose “Windows Form”
4.And it will automatically add a new form to your project.
The following steps looks like as shown below.
On the second form, let’s design looks like as shown below.
Next, let’s go back to our Form1 which is the first form that loads at start up.then double click the “Login” button and add the following code.
- Form2 loginForm;
In this code, we simply declare a variable of Type Form2. Then add this next line of code.
- loginForm = new Form2();
this next line of code, we create a new object. And if you prefer to shorten this two line of code, to make it in a single line. Here’s the following code.
Next, to get the login form to appear, you can use the method of the object called “Show()”. So the code now should look like this:
at this time, let’s run the application and test it out. Click the “Login” button and the login form should appear. And this look like as shown below.
And here’s all the code used in this application:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsFormsApplication3
- public partial class Form1 : Form
- public Form1()
- private void button1_Click(object sender, EventArgs e)