Features Of Event Driven Programming

Print   

02 Nov 2017

Disclaimer:
This essay has been written and submitted by students and is not an example of our work. Please click this link to view samples of our professional work witten by our professional essay writers. Any opinions, findings, conclusions or recommendations expressed in this material are those of the authors and do not necessarily reflect the views of EssayCompany.

Windows Applications Using Visual Studio

Sara Phypers

Contents Page

The development of a windows form using the features of visual studio 3

The Operation of an Event Driven Program:

A. The programs start up sequence 5

B. A variety of different events and the types of objects which can create them 7

C. Importance of idle time 8

How C# supports the concept of Object Oriented Programming:

Data encapsulation 8

Inheritance 8

Polymorphism 8

Discuss the benefits of structured exception handling 11

Discuss the built in debug facilities of Visual Studio, which can assist in testing

and debugging an event driven program developed using Visual Studio and C# 13

References 16

The development of a windows form using the features of visual studio

In Visual Studio is an Integrated Development Environment where you can create programs, one of the main features is the toolbox where you can place different controls and change their properties on a form within Visual Studio.

Here are some examples of the controls and their properties:

Checkbox:

A checkbox can be selected or deselected within a program; this helps the user make choices. I.E. if a user is buying a product on a website, they have to agree to the website’s terms and conditions at the point of sale, the user normally clicks on a "checkbox" if they agree it leave it blank if they do not agree.

Properties the checkbox uses are bool value and text to indicate what the checkbox is for.

Button:

A Button control works when a user clicks on it. When the button is clicked the program performs an action which is written within the program. I.E. When a "Calculator" programme is open on your computer each of the numbers and symbols are button so when you click on the number 3, the program actives the "Click" event and a 3 appears on the screen.

Properties mainly used are text, Enabled which may be changed if the button replies on the state another control within the form. You can also change the font, size and other characteristics of the button within the button’s properties.

Text Box:

A text box is a control where users can enter data into the program. There characters within the textbox are representing as a string in the program. The properties let the programmer hold text within the textbox, you can change the property of the textbox to "Multiline", the "Font" of the text, you can "Locked" the textbox so that users can not alter the text within textbox, make the textbox "ReadOnly", "ScrollBars" which gives the user to scroll which scross across the page or up and down, and change the "BackColor" (back ground colour) of the textbox. The methods which are associated with textbox are "AppendText":- this method joins new lines to the textbox which the textbox property is normally in Multiline. Also "Clear" method is used in this control to empty the text box.

Here is an example of a program using some of these controls- To Show Pie:

Inside the form create a button and a label.

Control

Property

Property Setting

Button

Name

btnCalculator

 

Text

To Show Pie

Label

Name

lblResults

 

AutoSize

FALSE

 

BorderStyle

Fixed3D

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 Practise_Exercise

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void btnCalculator_Click(object sender, EventArgs e)

{

/* "lblResults.Text" shows the answer in the label - lblResults on the form.

"Math" accesses the Math class.

".PI' accesses the Pie method within the Math Class. */

lblResults.Text = Math.PI.ToString();

}

private void Form1_Load(object sender, EventArgs e)

{

}

}

}

The Operation of an Event Driven Program

The programs start up sequence:

An Event Driven program is started up by the user clicking on a control within the form i.e. clicking on a button which starts an event. These events have procedures which the programmer has written so that methods know what to do when the event has been clicked on. I.E. if the user clicks on a button called ‘display’, the program will draw a circle in a picture box control within the form.

The events are not written in any particular order, only when an event has been trigger by a user will determine which lines of code will be executed, if a user does not click a button or any other control within the program then that event relating to that control will be executed.

Within start up procedures within an Event Driven Program you have to use constructors so that objects are fully formed which means that the instance variables have a value. These constructors may be a default constructor which means they have no parameters so that numbers will default to zero, bool defaults back to false etc but if you have defined what parameters you want in the constructor then these will over write the default constructor. All constructors are have an access of "public" as that they can access the constructor in other classes.

Constructor Example: (The following program shows the code in the form choosing the correct constructor from the NewConstructors class then showing the result in a message box)

Form1 coding:

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 Constructor_Example

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

//To tell the program what contructor to use, which done by putting '8' in the brackets.

NewConstructors showNumberOrName = new NewConstructors(8);

//brings up the message box with the value I have pass above.

MessageBox.Show(showNumberOrName.ToString());

}

}

}

NewConstructor Class:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Constructor_Example

{

// string and int constructor

class NewConstructors

{

String Name;

public NewConstructors(string name)

{

Name = name;

}

int Number;

public NewConstructors(int number)

{

Number = number;

}

//method to return a value of it exists.

public override string ToString()

{

if (Name != null)

{

return Name;

}

else

{

return Number.ToString();

}

}

}

}

A variety of different events and the types of objects which can create them and the way they are handled within the program:

When you double click on a control in the form designer tab in visual studios it creates a method which responds when someone clicks on that control. Here are some of those Events:

Button control creates the ‘Click’ event on the form’s coding page, it deals with when the button is clicked on.

CheckBox control creates ‘CheckedChanged’ event on the form’s coding page, it deals the unticking or ticking of the checkbox.

ListBox control creates ‘SelectedIndexChanged’ event which is used to find out if a selected index in the listBox has changed or a ‘DoubleClick’ event may be used.

DateTimerPicker control creates a ‘ValueChanged’ event is used when a date is changed on the month calendar object.

Here is example of the coding for an CheckBox event:

I have placed a CheckBox control on the form below:

Form1: (You can see the method which comes up in the form’s coding tab when you double click on the control)

The importance of idle time:

The importance of idle time within a programming language C#, is for the reason that it uses its idle time to help the program to do other tasks as C# can be multithreaded. There are two types of multi threading: "Process" based which executes two or more programs at once and "threads" lets programs perform two or more tasks at once. C# is special as it synchronizes these two different threads and coordinates them in defined way. To do this C# has a whole subsystem devoted to synchronization. To show that the a program is using multithreading, there is a namespace called "using System.Threading;".

Describe how C# supports the concept of Object Oriented Programming

Data encapsulation:

C# uses data encapsulation to make variables (data) private and methods (actions) public which creates an object and helps hide data from other classes. This helps the misuse and interference of the code. Within Encapsulation there are keywords: data and code in class are known as "members", which creates "member variables" and also you can get "instance variables", data defined in a class is referred as "fields", within C# "methods" are subroutines (function members, properties, events and constructors). This basically means that methods (actions) of a class contain code that acts on fields (data) defined by that class.

Inheritance:

Inheritance means that one object can obtain properties of another object. This done by hierarchical classification, an object can define its own qualities that made it unique within the class – "subclass" and inherit the general attributes (methods) from another class, its parent – "Superclass".

To inherit from the superclass into the subclass, use the words "protected" and "virtual" in the class. Protected is used at the instance variable stage of the coding.

To call the methods into the subclass from the superclass, use the keyword "base" in the method name. I.e. base.Display(drawArea).

You need to the keyword "abstract" at the end of the method so that other classe’s inherits the features from thd superclass. I.e. public abstract void Display(Graphics drawArea);.

Polymorphism:

C# helps reduce complexity in program by using polymorphism, it is done by using a generic interface which can identify a different "general class of actions" (methods) which the complier selects one method and applies it the situation.

The following program shows inheritance from taking parameters from Shape and using them in Triangle class.

Form1:

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;

using System.Drawing;

namespace Triangle_Inheritance

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

// code to show when you hit the click event the right Triangle is writen.

private void btnDrawSmallTriangle_Click(object sender, EventArgs e)

{

Graphics paper = pictureBox1.CreateGraphics();

Triangle triangle = new Triangle();

//parameters for this triangle:

triangle.Display(paper,Color.Blue, "small");

}

private void btnDrawBigTriangle_Click(object sender, EventArgs e)

{

Graphics paper = pictureBox1.CreateGraphics();

Triangle triangle = new Triangle();

//parameters for this triangle:

triangle.Display(paper, Color.Fuchsia, "big");

}

}

}

Abstract Class Shape:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

namespace Triangle_Inheritance

{

//Abstract class Shape which is inherited in the Triangle class.

public abstract class Shape

{

//Creating the pen variable.

protected Pen pen;

//Each inherited class overrides this method with the same parameters but different code.

public abstract void Display(Graphics paper, Color colour, String size);

}

}

Triangle Class:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

namespace Triangle_Inheritance

{

//Inherits the Shape class

public class Triangle : Shape

{

//parameters for each Triangle.

public override void Display(Graphics paper, Color colour, String size)

{

pen = new Pen(colour);

//code for the size of the triangle in an IF statement.

if (size == "small")

{

//Left Line

paper.DrawLine(pen, 150, 80, 185, 30);

//RightLine

paper.DrawLine(pen, 185, 30, 220, 80);

//Bottom Line

paper.DrawLine(pen, 150, 80, 220, 80);

}

else if (size == "big")

{

//Left Line

paper.DrawLine(pen, 130, 100, 205, 20);

//RightLine

paper.DrawLine(pen, 205, 20, 280, 100);

//Bottom Line

paper.DrawLine(pen, 130, 100, 280, 100);

}

}

}

}

Discuss the benefits of structured Exception Handling

An Exception is when an error has occurred in a program at run time, handling the exceptions will help programmers find these errors... This is done by using try-catch or try with multi catches and throwing an ‘if’ statement.

You would monitor exceptions in try blocks, if an exception is found within these "try" blocks, the exception is "thrown" and the code which you wrote to handle these exceptions would "catch" the exception and tell you in a logical manner what the exception is so then you can deal with it.

If you to want to manually create an exception you would use the keyword "throw", which normally used with the "if" statement. At the end of the try-catch statements the programmer will write a "finally" statement so that they know that all the exceptions have executed or not.

Program to showing how Exception Handling works:

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 Multiple_Catch_Block

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void btnProcess_Click(object sender, EventArgs e)

{

/*As you can see in the button click event, there is a "try" block below, which has the block of code for when the user presses the button, if the uses enters in the wrong answer so the program throws the exception and one of the catch block might catch it*/

try

{

int bottom, top = 100;

bottom = Convert.ToInt32(txtNumber.Text);

MessageBox.Show("Dividing into 100 gives " + Convert.ToString(top / bottom));

}

/*This catch block lets the user know that the zero can not be divided by, this is done by writing "DivideByZeroException" class*/

catch (DivideByZeroException exc)

{

MessageBox.Show(exc.Message + "\n\nError - zero: " + "re-enter data");

}

/*This exception will come up if the format of the answer is wrong */

catch (FormatException exc)

{

MessageBox.Show(exc.ToString() + "\n\nError in number: " + "re-enter");

}

/*This exception handler tells the programer that there is an error with what the answer which was given but can not tell you in detail what the error is*/

catch (Exception exc)

{

MessageBox.Show(exc.ToString());

}

/*This exception will always execute at the end of the try-catch code as it tells the programmer that it has gone through it*/

finally

{

MessageBox.Show("This is the end of Exception Handling");

}

}

private void Form1_Load(object sender, EventArgs e)

{

}

}

}

Discuss the built in debug facilities of Visual Studio, which can assist in testing and debugging an

Event driven program developed using Visual Studio and C#

Visual studio’s has three types of debugging facilities which you can use:

"Breakpoint" For this you have to set a dot on the gray bar next to the code. You put the dot where you want the program to stop. When you run the program, it will stop at the point where you put the dot, the code will change to yellow. After this if you put the cursor over the variable or property, or you can show these values in a new window called watch window by right clicking on the variable name, by doing this you will see what the program is showing for that particular variable or property then you can analysis it against what the actual output should be. You can add as many break points into the program as you like, once you have come to a breakpoint and want to go to the next variable or property just click on "Continue" on the toolbar.

"Add Watch" If you right click over a variable name, visual studio will bring up a new window to show the variable name and its value while you run the program. You can add as many add watches as you like in the program.

"Single Stepping" In this debugging technique, visual studio only runs a line of code at one time so the programmer can see what is happening to the methods and see if there is a different from what is meant to happen and is actually happening in the code. F10 will "Step over" the methods and go to the next method which has you have selected to "Step Into" and F11 will step into the methods. You can select this debugging technique from the top toolbar.

I have used a program from C# for Students book, which I can use to show how debugging works in Visual Studio’s:

The program should swap both names around but it does not work, I will go through the above procedures to find out why:

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 Debugging_example

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

//Click event for the button to swaps the names around:

private void btnSwap_Click(object sender, EventArgs e)

{

string temp;

temp = lblLeft.Text;

lblRight.Text = lblRight.Text;

lblRight.Text = temp;

}

}

}

Break point:

I clicked on the gray line and the visual studio’s placed a break point on the code:

I next run the program and it stopped when it got to this breakpoint and the line turned yellow:

I have now added an "Add Watch" to both label properties:

The outcome of the "Add Watch" window to label properties:

B

After reviewing the information I have got from the Breakpoint and the Add Watch window, I have found out that I have added a lblRight to the line where I should off added the lblLeft on the Breakpoint line. I Notice the problem in the Add Watch window because both property outcomes where the same when you run the program.



rev

Our Service Portfolio

jb

Want To Place An Order Quickly?

Then shoot us a message on Whatsapp, WeChat or Gmail. We are available 24/7 to assist you.

whatsapp

Do not panic, you are at the right place

jb

Visit Our essay writting help page to get all the details and guidence on availing our assiatance service.

Get 20% Discount, Now
£19 £14/ Per Page
14 days delivery time

Our writting assistance service is undoubtedly one of the most affordable writting assistance services and we have highly qualified professionls to help you with your work. So what are you waiting for, click below to order now.

Get An Instant Quote

ORDER TODAY!

Our experts are ready to assist you, call us to get a free quote or order now to get succeed in your academics writing.

Get a Free Quote Order Now