Form


  • It is also called as a “window”.
  • A form is a container, which can hold all other controls within it
  • In C#.NET every form is treated as a class and is derived from System.Windows.Forms.Form class
  • In C#.NET windows Form we can place various controls like Text Box, Button, List Box, Radio Button, Check Box etc
  • In C#.NET Windows Forms Applications every control has a special class and is present in System.Windows.Forms namespace .
  • Whenever a windows application is created, automatically Visual Studio creates the “Form1.
Rules for the Form class definition:
  • It should be the sub class of "System.Windows.Forms.Form" class.
  • It should be defined as a public class.
  • It should be defined as "partial" class, as its definition should be written in the following two files.
1)Form1.Designer.cs
  • Contains the code for designing (Automatically generated code by Visual Studio).
2)Form1.cs
  • Contains the actual functionality code (Written by the Programmer).
  • It should contain a constructor, with a statement called “InitializeComponent();”. This method is created automatically in “Form1.Designer.cs”. This method contains necessary code for creating the controls on the form at run time.
Form Class Definition Syntax:

namespace WABasics
{
    public partial class Form1 : Form
    {
        public Form1( )
        {
            InitializeComponent();
        }
    }
}


  • Whenever a windows application is created, automatically Visual Studio 2010 creates the “Form1 .It looks like…
Form1.Designer.cs [Design]


To open Form1.cs  follow the following steps

1.     Click on View
2.     Select Code




To open Form1.Designer.cs  follow the following steps

1.     Goto Solution Explorer
2.     Click on Form1.cs
3.     This will display Form1.Designer.cs
4.     Double click on Form1.Designer.cs
5.     This will open the Code like….


 Events :-
  • An event is a member of a class, used to perform required action
  • Event is similar to function but is pointed by a Delegate or function pointer
  • Every event will have particular period of time that is to be called known as Event raising/Firing
  • Delegates are completely backbone for the Events
Event Handler :-
  • An Event Handler is a method ,that gets executed automatically whenever the user performs certain event at runtime.
  • The event handler should be defined in the form class
Syntax :-

Private void ControlName_EventName( object sender,EventArgs )
{
        Statements..
        …………..
        …………..
}

Example :-
private void Form1_Load(object sender, EventArgs e)
{
            MessageBox.Show("from Default Event of the Form");
}


In the above Example Form1_Load is a function Called from Click Event ,this Function internally pointed by using a delegate from the respective Event( Click Event) like….



Implementation of Event Handler :-
1.     First, select the Control for which we want to create the Event Handler.
2.     Right Click on Control
3.     Click on Properties
4.     This will open “Properties” window
5.     In “’Properties” Window Select “Events” option.
6.     Select the required Event for which we want to create the Event Handler.
7.     Select the Event Press Enter or Double on Event.
8.     Then the Event Handler will be created in the code window
                
Examle for Implementation of Event Handler :-
1.     First, select the Form1
2.     Right Click on Form1,this will shows some options
3.     In that Click on Properties 


4.     This will open “Properties” window on the Right Side
5.     In “’Properties” Window Select “Events” option.
6.     This will shows Events


7.     Selected the Load Event Press Enter or Double Click on it


8.     This will create the Event Handler in the [ form1. cs ]



To Set the Properties to the  Form :-


Follow the following Steps ……
1.     Select the Form1
2.     Right click on it
3.     Select properties


4.     This will open properties window on Right side
5.     Set the Required properties


 

No comments:

Post a Comment