15

Click here to load reader

CSharp.net Tutorial – Day 15

Embed Size (px)

Citation preview

Page 1: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 1/15

CSharp.NET Tutorial – Day 15

In previous article, we have discussed what are Exceptions, what is Exception

Handling mechanism, what are Compile time errors, what are Runtime errors,

what is Abnormal Termination of the Program, what is tr, catch, and !nall

bloc"s and interrelation in between tr, catch and !nall, what are #stem

Exceptions, what are Application Exceptions, who to throw an exception

along with sntaxes and examples, etc

Please !nd below the lin" for accessing the article

C#harp$%ET Tutorial & 'a ()

%ow, in this article we will discuss what is *indows Programming, what isCharacter +ser Interface C+I-, what is .raphical +ser Interface .+I-,di/erence between C+I and .+I, How to 'evelop *indows Applications, whatare Partial Classes, How to 'evelop a *indows Application using 0isual

#tudio, what are Properties, what are Events, what are Event Procedures,How to 'e!ne Event Procedures 1anuall, How to #et Properties and 'e!neEvent Procedure 1anuall, How to Add a %ew 2orm, How to 3ind an EventProcedure with 1ultiple Events of a Control in 0isual #tudio, How to PlaceControls on the 2orm, How to 3ind an Event Procedure with 1ultiple Controls,How does a 2orm gets created, How does a control gets placed on the 2orm,what are tpes of code In *indows application, de!nitions of 'esigner Codeand 3usiness 4ogic along with sntaxes and examples, etc

Windows Programming

.enerall, we should re5uire user interface +I- in order to communicate withend users in developing an "ind of application$ 2or this, we have twodi/erent tpes of +ser Interfaces available, which are below$

• Character +ser Interface C+I-

Page 2: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 2/15

• .raphical +ser Interface .+I-

 Traditionall, we have onl Character +ser Interface C+I- available

Eg6 '7#, +%I8 7perating #stem, etc

3ut, these applications su/er from few criticisms li"e•  The are not user friendl because we need to learn the commands

!rst in order to use them• Also, the do not allow to navigate from one place to other place

 To solve the above problems, in earl 9:;s .raphical +ser Interface .+I-applications were introduced b 1icrosoft with its *indows 7perating#stem$ It has beautiful features "nown as <4oo" and 2eel$< In order todevelop those "ind of features, 1icrosoft has introduced a language into themar"et in 9:;s which is 0isual 3asic 03-$ Afterwards, when $%ET was

introduced the support for .+I has been given in all $%ET languages$

How to Deelop Windows !ppli"ationsIn order to develop windows application, we re5uire a set of componentscalled as ;controls$; 2or us, the controls were provided in $%ET in form of ;classes,; where ever control acts li"e a ;class; under the namespace of <#stem$*indows$2orms<$ Here, Controls available to us are divided intodi/erent categories li"e ;Container Controls,= ;1enu and Toolbar Controls,;;'ata Controls,= ;Printing Controls=, ;Reporting Controls,; etc for eas identit$

Note6 Here, each and ever ;Control; available is a #ubclass of a Class called

;Control; and it will provide the common features which are necessar foreach control$

 There are mainl three things in common for ever Control that we use in*indows application, which are as follows6

(- Properties>- 1ethods?- Events

%ow, we will see one b one

Properties6 Here, Properties are the attributes of a Control, which havetheir impact on loo" of a Control$

Eg6 *idth, Height, 3ac"Color, 2oreColor, etc

#ethods6 1ethods are nothing but Actions performed b Controls$

Page 3: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 3/15

Eg6 Clear, Close, 2ocus, etc

Eents6 Events tells when an appropriate action has to be performed$

Eg6 Clic", 4oad, @ePress, 1ouse7ver, etc

%ow, we clear what are the things we need to remember when developing awindows application$

 To develop a *indows application, !rst we need to create the 3ase controli$e$ ;2orm;$

 To create 2orm, we need to adopt the below process$

• 'e!ne a Class which is inheriting from prede!ned class ;2orm; so thatthe new class will also act li"e a ;2orm;$

Eg6 public class 12orm 6 2orm

•  To run the 2orm that we created, !rst we need to create obect of ;2orm; and it should pass it as a parameter to ;Run; method which ispresent under Application Class$ Here, Application Class is responsiblefor execution of the ;2orm;$

Eg612orm f B new 12orm -Application$Runf-

or

Application$Runnew 12orm --

Tip6 *e can develop *indows Application using either with %otepad or with;0isual #tudio; *indows 2orm Application Proect Template$

2irst, we will see how to develop *indows Application using %otepad Dust open a %otepad and write the below code in it and then #ave & Compile &Execute

using #stemusing #stem$*indows$2ormspublic class 12orm62orm

static void 1ain-

12orm fBnew 12orm -Application$Run f-

Page 4: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 4/15

FF

Partial Classes

Partial Classes is the concept of allowing splitting a class and de!ning itunder multiple !les$ Here, the advantage is that huge volumes of code canbe put on di/erent !les so that manageabilit becomes much easier$

It also allows man programmers to wor" on the same class at the sametime$

*hen we are de!ning partial classes, we can split them into an number of 

!les, but the condition is on each !le the class name should be the same andwe should use the modi!er, ;partial$;

 The another advantage is if we want to inherit from an other class we cando it in onl one single !le and it will ta"e to the complete class$ The reasonwh is here the class is phsicall separated into multiple !les, but logicall itis onl one$

 To chec" this, add a code !le in the proect naming it as <Part($cs< and writethe following$

using #stemnamespace 77P#Proectpartial class Partspublic void 1ethod(-Console$*rite4ine<1ethod(<-

Page 5: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 5/15

Fpublic void 1ethod>-Console$*rite4ine<1ethod><-F

FF

Add a Code !le ;Part>$cs; and the write the below code$

using #stemnamespace 77P#Proectpartial class Partspublic void 1ethod?-

Console$*rite4ine<1ethod?<-Fpublic void 1ethod)-Console$*rite4ine <1ethod)<-FFF

Add a class ;TestParts$cs; and write the below code$

using #stemnamespace 77P#Proectclass TestPartsstatic void 1ain-Parts p B new Parts -p$1ethod( - p$1ethod> -p$1ethod? - p$1ethod) -

Console$Read4ine -FFF

How to Deelop a Windows !ppli"ation using $isual Studio

Page 6: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 6/15

•  To develop a *indows Application using 0isual #tudio, open newproect window &G select ;*indows 2orms Application; template andspecif a name to the Proect$Eg6& *indowsProect-

• 3 default, the proect will come with two classes, which areProgram$cs and 2orm($cs

•  The execution of *indows Application starts from the class ;Program;,which is ;static$; +nder this class, we !nd the 1ain method creatingthe obect of ;2orm; class which has to be executed$

Eg6Application$Runnew 2orm( --

• ;2orm(; is the !le in which the class ;2orm(; is de!ned inheriting formprede!ned class ;2orm; and this is the class that has to be executed$

Eg6public partial class 2orm(62orm

• *indows applications developed using 0isual #tudio has two places wecan wor" with

(- 'esign 0iew>- Code 0iew

Here, 'esign 0iew is the place, where we can design the application which isaccessible to both programmer and end user as well$

And Code 0iew is the place, where we can write the code for execution of application which is accessible onl to the Programmers$

PropertiesAs we "now that ever control should have properties, methods and events$ To access properties and events, 0isual #tudio provides us propert window,which lists all properties corresponding to the control$

 To open Properties window, select the control !rst and press 2), which willdispla all properties which are available to that control$ Please note that wecan change an propert in the list li"e width, height, bac"color, etc and we

can see the change immediatel$

*henever we set value to an propert of the control, 0isual #tudio willinternall write all the corresponding code referring to each propert of thecontrol b assigning the values that we speci!ed under propert window$ *ecan easil view that code under a 1ethod called <InitialieComponent<,which gets called from the constructor$

Page 7: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 7/15

Note6 *e can view the code under <InitialieComponent< b going toCode0iew &G right clic" on the 1ethod and select ;2orm($'esigner$cs; andhere also we can !nd the same class ;2orm(; which is partial as well$

Eg6

this$TextB<2irst 2orm<this$3ac"ColorBColor$3luethis$#ie B new #ie?:,?:-

EentsEvents are time periods, which will tell when an action should be performedi$e$ when exactl we want to execute the code$ Each and ever control has anumber of events and each event occurs on a particular time period$

*e can access the events of a control in propert window$ To view them inpropert window, choose ;Events; option on top of the window$

*e can write an code under an event b double clic"ing the re5uired event,which will ta"e us to the Code 0iew and provide a method in which we canwrite the code$

In the proect, which we have opened, go to Events, double clic" on ;4oadEvent; then write the below code under ;2orm(J4oad; method generated inCode0iew$

1essage3ox$#how<*elcome to *indows Applications<-

Again, go to 'esign 0iew &G select Events &G double clic" on ;Clic"; event &Gthen write the below code under ;2orm(JClic"; method$

1essage3ox$#how<Kou have clic"ed on 2orm<-

Eent Pro"eduresEvent procedure is a special method that is available to *indows application,which gets generated when we double clic" on an event in the propertwindow$

An Event Procedure is also called a method, but these are called b us

explicitl$ The get bound with an event of control and execute wheneverthe event occurs$

 To execute this "ind of methods, events will ta"e help of a ;delegate$; #owhenever event occurs, it calls the delegate and delegate executes theevent procedure that is bound with the event$

Page 8: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 8/15

Events and delegates are pre&de!ned$ Here, events are de!ned undercontrols and delegates are de!ned under #stem as well as#stem$*indows$2orms namespaces$

Event procedures are user de!ned which are explicitl de!ned in our code2orm class-$

After de!ning the event to bind event delegate and event procedure, 0isualstudio writes a statement binding all the three i$e$ event, delegate and eventprocedure as below$

Synta%&LcontrolG$LeventG MB new LdelegateGLevent procedureG-

Eg6this$4oad MB new EventHandler2orm(J4oad-this$Clic" MB new EventHandler2orm(JClic"-

Note6 *e can view the code under ;InitialieComponent; method

7ne delegate can be used b multiple events to execute event procedures,

which does not mean that same delegate will be used for all events$'i/erent events can use di/erent delegates in order to execute eventprocedures$

How to De'ne Eent Pro"edures #anually*e can de!ne an event procedure manuall as below$

Synta%

Page 9: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 9/15

NLmodi!ersGO void L%ameG obect sender, Event Args e-

stmtsF

•Event procedures are non&value returning methods$

•  The name of event procedure can be anthing and it is purel user&de!ned$

• Ever event procedure ta"es two mandator parameters which areobect sender and Event Args e

How to Set Properties and De'ne Eent Pro"edure #anually*rite the below code in the notepad and execute$

using #stemusing #stem$'rawing

using #stem$*indows$2ormspublic class 12orm>62ormspublic 12orm>-InitialieComponent -Fprivate void InitialieComponent-this$TextB<12orm<this$3ac"ColorBcolor$Pin"

this$#ieBnew sie ?:,?:-this$load MBnew EventHandler Test-this$clic" MBnew EventHndler Test-Fprivate void Testobect sender, EventArgs e-static void 1ain-12orm> fBnew 12orm> -Application$Run f-F

Note6 .enerall, the concept of event and event procedure is derived fromclassical 0isual 3asic$ In 03, one event procedure can be bound with onlone event of ever control, whereas in $%ET one event procedure can bebound with multiple events of a single control as well as with multiplecontrols also$

Page 10: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 10/15

How to !dd a New (orm

•  To add a new form under a proect, 7pen %ew Item window &G select*indows 2orm which will add new form called 2orm>$cs$

• In order to run the %ew 2orm, we need to open ;Programs$cs; class andmodif the value under ;Application$Run;< method as 2orm> li"e below$

Application$Run new 2orm> --

How to )ind an Eent Pro"edure with #ultiple Eents o* a Control in$isual StudioIn the 2orm we added, de!ne a load event b double clic"ing on 4oad Eventprocedure$ To bind the same event procedure to clic" event also, go toevents of our 2orm and under clic" event, select the event procedure 2orm>load which was de!ned previousl$

%ow under the Event Procedure, write the below code$

1essage3ox$#how3ound with multiple Events of ControlQ-

How to Pla"e Controls on the (orm$%ET provides us a number of controls in the form of classes where evercontrol acts li"e a class$ All these controls are available under the Tool3oxwhich is present in the 4eft Hand #ide$ *e can use an control b eitherdouble clic"ing on it which will add the control to the form or select control in Tool3ox and clic" on the form which will add the control at desired location$

How to )ind an Eent Pro"edure with #ultiple Controls

Add a %ew 2orm in proect and design it as below$

Page 11: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 11/15

'e!ne a ;Clic"; event procedure to 3utton and bind the event procedure with

remaining two Text3oxes and also the other 3utton$

Note6 Chec" the Event Procedure that is bound with all !ve controls whichare two 3uttons, two Text3oxes and a 2orm$

%ow under the Event Procedure, write the below code$

if sender$.etTpe-$%ame BB <3utton<-3utton b B 3utton-senderif b$%ame BB <button(<-

1essage3ox$#how<3utton( clic"ed<-else if b$%ame BB <button><-1essage3ox$#how<3utton> is clic"ed<-Felse if sender$.etTpe-$%ame BB <Text3ox<- Text3ox tb B sender as Text3oxif tb$%ame BB <text3ox(<-

Page 12: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 12/15

1essage3ox$#how<textbox( is clic"ed<-else if tb$%ame BB <text3ox><-1essage3ox$#how<textbox> is clic"ed<-Felse

1essage3ox$#how<2orm? is clic"ed<-

• If an event procedure is bound with multiple controls, to recognie thecontrol that has raised the event, we can ma"e use of the parameter;sender; under the event procedure$

• In above case, event procedure is bound with !ve controls$ #o in theruntime the obect of the control which has raised the event will besent to the event procedure which gets captured under the parameter<sender< as below$

• In above case, each control;s obect which is raising the event is sentto the event procedure in runtime and captured under the parameter<sender<, where sender can hold an obect in it because it is of tpeobect$

3ecause <sender< is holding obects of di/erent control classes$ *e caneasil !nd out which control exactl raised the event b using ;.etTpe;method of obect class$

After using ;.etTpe; method to identif the tpe of control, now if we havemultiple controls of the same tpe to identif each and ever control weneed to !nd out the name of the control$ 3ut the propert name is availableunder the child class using ;sender; we cannot identif the name$ #o !rst

Page 13: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 13/15

;sender; needs to be converted into the appropriate control tpe b explicittpe casting as below$

ifsender$.etTpe-$%ame BB <3utton<-

3utton bB 3utton- senderFelse ifsender$.etTpe-$%ameBB<Text3ox<- Text3ox tbBsender as Text3oxF

How does a (orm gets "reated+*hen a 2orm is added to the proect internall below things happened

(- Creates a class inheriting from prede!ned class, 2orm so that new classis also acts li"e 2orm$

Eg6 public partial class 2orm(62orm>- #ets same Initialiation properties li"e %ame, Text, etc under

InitialieComponent method$Eg6 this$%ameB<2orm(<

this$TextB<2orm(<

How does a "ontrol gets pla"ed on the (orm+*hen a controls placed on the 2orm, below things ta"es place$

(- Creates obect of appropriate control classEg6 3utton button( B new 3utton-

 Text3ox text3ox( B new Text3ox-

>- #ets same Initialiation properties li"e %ame, Text, #ie, 4ocation, etcEg6 button($%ameB<button(<

button($TextB<button(<button($4ocationBnew pointx,-button($#ieBnew siewidth,height-

?- %ow the control gets added to form b calling control$Add1ethod oncurrent form$

Eg6 this$Controls$Add button(-

Note6 All the above code will be generated b 0isual #tudio underInitialieComponent 1ethod$

In *indows application, code is of basicall two tpes$(- 'esigner Code>- 3usiness 4ogic

Page 14: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 14/15

'esigner Code6 Code which is responsible for construction of the form is"nown as 'esigner Code$3usiness 4ogic6 Code which is responsible for execution of the form is "nownas 3usiness 4ogic$

'esigner Code is generated b 0isual #tudio under the 1ethod<InitialieComponent< and 3usiness 4ogic is de!ned b programmers in theform of ;Event Procedures<$

3efore $%ET >$:, 'esigner code and 3usiness 4ogic were de!ned in a classpresent under a !le li"e below$

3efore >$:&&&&&&&&&&2orm($cs&&&&&&&&

public class 2orm(62orm&'esigner Code&3usiness 4ogicF

3ut from $%ET >$: with the introduction of partial classes, 'esigner code and3usiness 4ogic are separated into two di/erent !les but of same class onlli"e below$

2orm($cs6

&&&&&&&&&public partial class 2rom(62rom&3usiness 4ogicF2orm($'esigner$cs6&&&&&&&&&&&&&&&&&&partial class 2orm(&'esigner 4ogic

F

Happy ,earning-.

Page 15: CSharp.net Tutorial – Day 15

7/23/2019 CSharp.net Tutorial – Day 15

http://slidepdf.com/reader/full/csharpnet-tutorial-day-15 15/15

*indows Programming, C+I, .+I, Partial Classes, Properties, Events, EventProcedures, Adding a %ew 2orm, 'esigner Code, 3usiness 4ogic