Escolar Documentos
Profissional Documentos
Cultura Documentos
Windows Programming
Generally, we should require user interface (UI) in order to communicate with
end users in developing any kind of application. For this, we have two
different types of User Interfaces available, which are below.
Tip: We can develop Windows Application using either with Notepad or with
'Visual Studio' Windows Form Application Project Template.
First, we will see how to develop Windows Application using Notepad
Just open a Notepad and write the below code in it and then Save - Compile Execute
using System;
using System.Windows.Forms;
public class MyForm:Form
{
static void Main()
{
MyForm f=new MyForm ();
Application.Run (f);
}
}
Partial Classes
Partial Classes is the concept of allowing splitting a class and defining it
under multiple files. Here, the advantage is that huge volumes of code can
be put on different files so that manageability becomes much easier.
It also allows many programmers to work on the same class at the same
time.
When we are defining partial classes, we can split them into any number of
files, but the condition is on each file the class name should be the same and
we should use the modifier, 'partial.'
The another advantage is if we want to inherit from any other class we can
do it in only one single file and it will take to the complete class. The reason
why is here the class is physically separated into multiple files, but logically it
is only one.
To check this, add a code file in the project naming it as "Part1.cs" and write
the following.
using System;
namespace OOPSProject
{
partial class Parts
{
public void Method1()
{
Console.WriteLine("Method1");
}
public void Method2()
{
Console.WriteLine("Method2");
}
}
}
Add a Code file 'Part2.cs' and the write the below code.
using System;
namespace OOPSProject
{
partial class Parts
{
public void Method3()
{
Console.WriteLine("Method3");
}
public void Method4()
{
Console.WriteLine ("Method4");
}
}
}
Add a class 'TestParts.cs' and write the below code.
using System;
namespace OOPSProject
{
class TestParts
{
static void Main()
{
Parts p = new Parts ();
p.Method1 (); p.Method2 ();
p.Method3 (); p.Method4 ();
Console.ReadLine ();
}
}
}
How to Develop a Windows Application using Visual Studio
'Form1' is the file in which the class 'Form1' is defined inheriting form
predefined class 'Form' and this is the class that has to be executed.
Eg:
public partial class Form1:Form
Properties
As we know that every control should have properties, methods and events.
To access properties and events, Visual Studio provides us property window,
which lists all properties corresponding to the control.
To open Properties window, select the control first and press F4, which will
display all properties which are available to that control. Please note that we
can change any property in the list like width, height, backcolor, etc and we
can see the change immediately.
Whenever we set value to any property of the control, Visual Studio will
internally write all the corresponding code referring to each property of the
control by assigning the values that we specified under property window. We
can easily view that code under a Method called "InitializeComponent",
which gets called from the constructor.
Events
Events are time periods, which will tell when an action should be performed
i.e. when exactly we want to execute the code. Each and every control has a
number of events and each event occurs on a particular time period.
We can access the events of a control in property window. To view them in
property window, choose 'Events' option on top of the window.
We can write any code under an event by double clicking the required event,
which will take us to the Code View and provide a method in which we can
write the code.
In the project, which we have opened, go to Events, double click on 'Load
Event' then write the below code under 'Form1_Load' method generated in
CodeView.
MessageBox.Show("Welcome to Windows Applications");
Again, go to Design View -> select Events -> double click on 'Click' event ->
then write the below code under 'Form1_Click' method.
MessageBox.Show("You have clicked on Form");
Event Procedures
Event procedure is a special method that is available to Windows application,
which gets generated when we double click on an event in the property
window.
An Event Procedure is also called a method, but these are called by us
explicitly. They get bound with an event of control and execute whenever
the event occurs.
To execute this kind of methods, events will take help of a 'delegate.' So
whenever event occurs, it calls the delegate and delegate executes the
event procedure that is bound with the event.
Events and delegates are pre-defined. Here, events are defined under
controls and delegates are defined under System as well as
System.Windows.Forms namespaces.
Event procedures are user defined which are explicitly defined in our code
(Form class).
After defining the event to bind event delegate and event procedure, Visual
studio writes a statement binding all the three i.e. event, delegate and event
procedure as below.
Syntax:
<control>.<event> += new <delegate>(<event procedure>)
Eg:
this.Load += new EventHandler(Form1_Load);
this.Click += new EventHandler(Form1_Click);
Note: We can view the code under 'InitializeComponent' method
One delegate can be used by multiple events to execute event procedures,
which does not mean that same delegate will be used for all events.
Different events can use different delegates in order to execute event
procedures.
How to Define Event Procedures Manually
We can define an event procedure manually as below.
Syntax
Define a 'Click' event procedure to Button and bind the event procedure with
remaining two TextBoxes and also the other Button.
Note: Check the Event Procedure that is bound with all five controls which
are two Buttons, two TextBoxes and a Form.
Now under the Event Procedure, write the below code.
if (sender.GetType().Name == "Button")
{
Button b = (Button)sender;
if (b.Name == "button1")
MessageBox.Show("Button1 clicked");
else if (b.Name == "button2")
MessageBox.Show("Button2 is clicked");
}
else if (sender.GetType().Name == "TextBox")
{
TextBox tb = sender as TextBox;
if (tb.Name == "textBox1")
MessageBox.Show("textbox1 is clicked");
else if (tb.Name == "textBox2")
MessageBox.Show("textbox2 is clicked");
}
else
MessageBox.Show("Form3 is clicked");
In above case, each control's object which is raising the event is sent
to the event procedure in runtime and captured under the parameter
"sender", where sender can hold any object in it because it is of type
object.
Happy Learning.!!!!!