- Back to Home »
- Windows Applications »
- Form
Posted by :
Sudhir Chekuri
Friday, 12 February 2016
Every form is a class. You can create multiple forms in a single windows application. The namespace System.Windows.Forms contains components you will need for creating forms and controls.
Form load event is fired when the form is finished loading just before it is displayed.
MinimizeBox: Tells whether the minimize box located at the top right is displayed.
Name: The name of the form that is used to reference it in the code.
Text: The text that is shown in the caption bar of the form.
StartPosition: The starting position of the form when it is initially shown.
Right click on the root folder of the project in solution explorer.
Click on Add – New Item
Select Windows Form from the list of items
Enter form name with .cs as extension and click on Add. Example: SecondForm.cs
Navigation from one form to another form
Code in Button click of first form to navigate to second form on button click.
private void button1_Click(object sender, EventArgs e)
{
SecondForm f = new SecondForm();
f.Show();
this.Hide();
}
Show method is used to display the new form for which object is created.
Hide method is used to hide the existing form by using this keyword which refers to the current form in which object is created.
Every windows application will have a main method in Program class where execution of application starts. In main method Run method is used to call the start-up form.
Form1 is the Startup form name used in the above example.
Replace Form1 with new form name to display that new form when application is launched.
Form load event is fired when the form is finished loading just before it is displayed.
Properties of Form:
MaximizeBox: Tells whether the maximize box located at the top right is displayed.MinimizeBox: Tells whether the minimize box located at the top right is displayed.
Name: The name of the form that is used to reference it in the code.
Text: The text that is shown in the caption bar of the form.
StartPosition: The starting position of the form when it is initially shown.
Creating new form
Right click on the root folder of the project in solution explorer.
Click on Add – New Item
Select Windows Form from the list of items
Enter form name with .cs as extension and click on Add. Example: SecondForm.cs
Navigation from one form to another form
Code in Button click of first form to navigate to second form on button click.
private void button1_Click(object sender, EventArgs e)
{
SecondForm f = new SecondForm();
f.Show();
this.Hide();
}
Show method is used to display the new form for which object is created.
Hide method is used to hide the existing form by using this keyword which refers to the current form in which object is created.
Startup form
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Every windows application will have a main method in Program class where execution of application starts. In main method Run method is used to call the start-up form.
Form1 is the Startup form name used in the above example.
Replace Form1 with new form name to display that new form when application is launched.