Multiple Forms in C#
Multiple Forms in C#
Outline: Multiple Forms in C#
Multiple forms in computer programming languages make it easier for programmers to create complex applications without writing repetitive code modules. C# supports named parameters in several places— among them are method declarations and LINQ queries— which makes it easy to implement different ways of assigning values to parameters in your application.
In this tutorial, we will illustrate creating a program that utilizes multiple forms. Many methods can be adapted by programmers to load the main structure at startup. Considering this post, we are going to create a basic application with a single button that loads the login form when clicked. The interface looks like the image below.
To proceed, we add a form by following the instructions highlighted below;
1 – Navigate to Solution Explorer
2 – Right-click on Project and choose Add
3 – Now, Select “Windows Form”
4 – This will add a new form automatically to the project.
The image below shows the aforementioned steps.
We can design a second form like the below interface.
Proceed to the first form, Form1, double click on “Login” and add the following code to enable us to load at start up.
Form2 loginForm;
The variable of type was declared in Form2 with the code. We can use the below code to create a new object.
Form2 loginForm = new Form2();
We then proceed to make the form appear by using the method of the object known as “Show()”. The code underneath works for it.
Form2 loginForm = new Form2();
loginForm.Show();
Now we can test the application by running it. Proceed to click on the “Login” button to display the login form as shown in the image below.
Find below the code we utilized in the 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()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 loginForm = new Form2();
loginForm.Show();
}
}
}