Example on Form Properties & Events:-


1)    Create a new windows forms application
2)    This will by default creates Form1
3)    Generate Event Handlers for the Form1’s Load & Click Events
4)    Goto Source [Form1.cs] and write the following code

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 WABasics
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("This is Load Event");
        }

        private void Form1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("This is Click Event");
        }






1.    To Run a Windows Forms Application Click on Start Debugging (F5) Button


2.    To Stop Running the Windowsforms ApplicationClick on Stop Debugging ( Shift+F5) Button
3.    To Restart the Windowsforms ApplicationClick on Restart (Ctrl +Shift+F5) Button

 



Run the Application & Check

Add a New Form to Windows Forms Application

1)    Goto Solution Explorer
2)    Select Solution or Project  Right Click on it
3)    Select Add
4)    Click on Windows Form….


5)    This will open Add New Item Window
6)    In this window select Windows Form Template
7)    Enter Name for Form As Form2.cs
8)    Click on Ok


This will add a New Form i.e., Form2 to the Application 

Example on Form Properties & Events:-
 
Add an New form i.e., Form 2 to the Application
Generate Event Handler for the Form2’s Click Event
Design:-

 
Goto source[Form2.cs] & Write the following code

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 WABasics
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            this.BackColor = Color.Blue;
            this.Text="You have clicked on Form2";
        }
    }
}




How to Change the Startup Form :-

1.     Even ,if we add multiple forms to the project when the project is started the ‘ Form1’ will be opened by Default .This is called as “ Startup Form

2.     To Change the startup Form ,change the following statement with the required class Name in the  program.cs  class’s  Main( ) method.

        Application.Run(new Form1( ))

     3. If we created a new form with name Form2 then change the form            Name to Form2( ) in program.cs before Running the Application like..

                  Application.Run(new Form2( ))

Steps :-

1) Fist open the Solution Explorer

  To open the Solution Explorer follow the following Step

   Click on View Menu Select Solution Explorer option



2) In Solution Explorer double click on program.cs


3)In program.cs Change the Startup Form Name from Form1( ) to Form2( )

          Application.Run(new Form2( ))

                                    



 

Run the Form2 this will shows  Form2


Output : -


  Click on Form2
 


No comments:

Post a Comment