WinAPC#_01

Embed Size (px)

Citation preview

  • 8/6/2019 WinAPC#_01

    1/36

    @OOLA Creating Windows Applications/1

    Creating Windows ApplicationsCreating Windows Applications

    ObjectivesIn this lesson, you will learn to:

    Identify various namespacesIdentify the Types of Windows ApplicationsIdentify Common Windows Forms Control PropertiesIdentify Some Windows Forms Events and MethodsIdentify Various Windows Forms ControlsImplement the CommonDialog classes

  • 8/6/2019 WinAPC#_01

    2/36

    @OOLA Creating Windows Applications/2

    C# Applications

    System Namespace

    Class A:If{Method1();Method2();--------}----

    }

    Class A:If{Method1();Method2();--------}

    Class A:If{Method1();Method2();--------}

    Interface:If{Method1();Method2();--------}

    Interface:If{Method1();Method2();--------}

    Interface:If{Method1();Method2();--------}

    Objects

    Classes

    Interfaces

  • 8/6/2019 WinAPC#_01

    3/36

    @OOLA Creating Windows Applications/3

    Windows Applications NamespacesNamespaces needed by a C# Windows project are:

    System-A fundamental core namespace used by all C# projects.Provides the required classes, interfaces, structures, delegates,

    and enumerations required of all C# applications.System.Drawing-

    Provides access to a number of drawing tools. For example, thenamespace classes include Brushes, Cursor, Fonts, Pens, and soon.The namespace structures include Color, Point, and Rectangle.

    System.Collections-The System.Collections namespace contains the ArrayList,BitArray, Hashtable, Stack, StringCollection, and StringTableclasses.

  • 8/6/2019 WinAPC#_01

    4/36

    @OOLA Creating Windows Applications/4

    Windows Applications Namespaces (contd)Namespaces needed...:

    System.ComponentModel-Provides support for the following classes; ArrayConverter,ByteConverter, DateTimeConverter, Int16Converter,Int32Converter, Int64Converter, and so on.Also provides delegate support for a variety of event handlerdelegates.

    System.Windows.Forms-Provides class support for a variety of forms and controls likeBorder, Button, CheckBox, CommonDialog, Forms, and ListBox.

    Also provides delegate support for both forms and controls.System.Data-

    Provides class support for handling data.Allows various actions like sorting, filtering etc. on data

  • 8/6/2019 WinAPC#_01

    5/36

    @OOLA Creating Windows Applications/5

    Windows Applications FilesWhen a Windows Application, say WindowsApp iscreated, following files are created:

    WindowsApp.csproj- A C Sharp project.App.icon- Applications icon file.

    AssemblyInfo.cs- General information about anassembly including version information.Form1.cs- A forms code file.Form1.resx- A XML based resource template.

    bin- Directory for binary executable.

    obj- Directory for debugging binaries.

  • 8/6/2019 WinAPC#_01

    6/36

    @OOLA Creating Windows Applications/6

    Windows FormsProvides the following controls derived from Controlclass :

    FormLabel

    TextBoxButton

    Provides many types of user interfaces:the single-document interface ( SDI )the multiple-document interface ( MDI )the Explorer-style interface

  • 8/6/2019 WinAPC#_01

    7/36

    @OOLA Creating Windows Applications/7

    SDI ApplicationOnly a single document can be open. For example theWordPad application in Microsoft Windows.

  • 8/6/2019 WinAPC#_01

    8/36

    @OOLA Creating Windows Applications/8

    MDI ApplicationAllows to display multiple documents at the sametime. For example, Microsoft Excel.

  • 8/6/2019 WinAPC#_01

    9/36

    @OOLA Creating Windows Applications/9

    Explorer-styleIs a single window containing two panes or regions.For example, Microsoft Windows Explorer.

  • 8/6/2019 WinAPC#_01

    10/36

    @OOLA Creating Windows Applications/10

    Creating Windows FormsThe Windows Forms Designer provides a rapiddevelopment solution for creating Windowsapplications.The first step in designing a form is to set its

    properties.At design time, one an do so in the Propertieswindow.

  • 8/6/2019 WinAPC#_01

    11/36

    @OOLA Creating Windows Applications/11

    Windows Forms Control PropertiesA Windows Forms control inherits many propertiesform the base class System.Windows.Forms.Control.

    Some properties inherited from Control class such as:Font - The Font object to apply to the text displayed by

    the control. It is an ambient property.ForeColor - The foreground Color of the control

    BackColor - The background Color of the controlBounds - The bounds of the control includes thenonclient elements such as scroll bars , borders , title

    bars , and menus . The SetBoundsCore method is calledto set the Bounds property.ClientRectangle - A Rectangle that represents the clientarea of the control.

  • 8/6/2019 WinAPC#_01

    12/36

    @OOLA Creating Windows Applications/12

    Windows Forms Control Properties (contd)Some properties inherited from Control class . . . . .DisplayRectangle - A Rectangle that represents thedisplay area of the control.RightToLeft - Gets or sets a value indicating whethercontrol's elements are aligned to support locales using

    right-to-left fonts.Focused - Get a true value if the control has focus;otherwise, false .HeightWidthAnd many others

    Some Form properties:ImeMode - One of the ImeMode values. An Input MethodEditor (IME) allows users to enter and edit Chinese,Japanese, and Korean characters.

  • 8/6/2019 WinAPC#_01

    13/36

    @OOLA Creating Windows Applications/13

    Windows Forms Control Properties (contd)Some Form properties . . . . .IsMdiChild - Gets a value indicating whether the form isa multiple document interface (MDI) child form.IsMdiContainer - Gets or sets a value indicating whether

    the form is a container for multiple document interface(MDI) child forms.KeyPreview - Gets or sets a value indicating whether theform will receive key events before the event is passedto the control that has focus.ShowInTaskbar - Gets or sets a value indicating whetherthe form is displayed in the Windows taskbar.SizeGripStyle - Gets or sets the style of the size grip todisplay in the lower-right corner of the form.

  • 8/6/2019 WinAPC#_01

    14/36

    @OOLA Creating Windows Applications/14

    Windows Forms Control Properties (contd)Some Form properties . . . . .SnapToGrid - If set to true all controls now alignthemselves along the points on the grid.TopMost - Gets or sets a value indicating whether the

    form should be displayed as the top-most form of yourapplication.TransparencyKey - When the TransparencyKey propertyis assigned a Color, the areas of the form that have thesame BackColor will be displayed transparently.

    An ambient property is a control property that, if notset, is retrieved from the parent control. Forexample, a Button will have the same BackColor asits parent Form by default.

  • 8/6/2019 WinAPC#_01

    15/36

    @OOLA Creating Windows Applications/15

    Event HandlingAn event handler is a procedure that determines theactions to be performed when an event occurs, suchas the user clicking a button or a message queuereceiving a message.

    When an event is raised, the event handler (orhandlers) that receives the event is executed.Events can be assigned to multiple handlers, and themethods that handle particular events can bechanged dynamically.

    One can use the Windows Forms Designer to createevent handlers.

  • 8/6/2019 WinAPC#_01

    16/36

    @OOLA Creating Windows Applications/16

    Windows Form EventsSome of the events associated with Windows Forms:Click- Occurs when user clicks anywhere on a form.Closed- Occurs when a form is closed.Deactivate- Occurs when a form loses focus and is no

    longer active.Load- Occurs when a form is loaded into memory firsttime.MouseMove- Occurs when mouse is moved over a form.

    MouseDown- Occurs when left button is pressed on a

    form.MouseUp- Occurs when the mouse button is released.

  • 8/6/2019 WinAPC#_01

    17/36

    @OOLA Creating Windows Applications/17

    Windows Form MethodsSome of the events associated with Windows Forms:Show()- Used to display a form.Activate()- Used to activate a form and set the focus onit.

    Close()- Used to close a form.SetDesktopLocation(x, y)- Used to set desktop locationof a form at run time.

  • 8/6/2019 WinAPC#_01

    18/36

  • 8/6/2019 WinAPC#_01

    19/36

    @OOLA Creating Windows Applications/19

    Problem Statement 8.D.1For the Call Center application, a startup screenneeds to be provided to accept the user name andpassword. The application should allow the user toenter the user name and password for a maximum of three times. If in all of the three attempts, a wronguser name and password is entered, the applicationshould display an error and close.

  • 8/6/2019 WinAPC#_01

    20/36

    @OOLA Creating Windows Applications/20

    StepsIdentify the variables required to store the values inthe application.

    Identify the controls to be used for accepting inputfrom the user.

    Identify the mechanism to validate the user input.Create a Windows Form.Add controls to the Windows Form.Write the code to validate the user input.

    Execute the application.

  • 8/6/2019 WinAPC#_01

    21/36

  • 8/6/2019 WinAPC#_01

    22/36

    @OOLA Creating Windows Applications/22

    Step 2: Identify the input controls (Contd)C# .NET provides a number of controls that can beadded to a form:

    TextBox- Is used to display text to a user or acceptinput from a user. Has the following properties:

    TextMultilinePasswordchar

    Label- Is used to provide information or description of another control on the Windows Form. Has thefollowing properties:

    TextAutoSize

  • 8/6/2019 WinAPC#_01

    23/36

    @OOLA Creating Windows Applications/23

    Step 2: Identify the input controls (Contd)C# .NET provides a number of controls :LinkLabel- Is used to display the text as a link. Has thefollowing properties:

    LinkColor

    ActiveLinkColorDisabledLinkColorLinkVisited

    ListBox- Is used to display a list of items to a user. Canbe populated by using the Add() method of the Itemscollection. Has the following properties:

    SelectionModeSorted

    SelectedIndexSelectedItem

  • 8/6/2019 WinAPC#_01

    24/36

    @OOLA Creating Windows Applications/24

    Step 2: Identify the input controls (Contd)C# .NET provides a number of controls :ComboBox- Is used to display a drop-down list of items.Can be populated by using the Add() method of theItems collection. Has the following properties:

    TextSortedSelectedIndex

    SelectedItem

    CheckBox- Is used to set Yes/No or True/false options.Has the following properties:

    TextChecked

  • 8/6/2019 WinAPC#_01

    25/36

    @OOLA Creating Windows Applications/25

    Step 2: Identify the input controls (Contd)C# .NET provides a number of controls :RadioButton- Is used to provide a set of mutuallyexclusive options to the user. Has the followingproperties:

    TextChecked

    GroupBox- Is used to group related controls.

    Button- Is used to perform an action when a user clicksit. Has the Text property, which is used to set the textdisplayed on the button.

    StatusBar- Is used to display the status information.Has the following properties:

    ShowPanels

    Text

  • 8/6/2019 WinAPC#_01

    26/36

    @OOLA Creating Windows Applications/26

    Step 2: Identify the input controls (Contd)Some common properties found in most of thecontrols are:

    NameVisible

    LocationEnabled

  • 8/6/2019 WinAPC#_01

    27/36

    @OOLA Creating Windows Applications/27

    Step 2: Identify the input controls (Contd)Some common events found in most of the controlsare:

    KeyDownKeyUp

    KeyPressMouseDownMouseUpMouseMoveReSize

    VisibleChanged

  • 8/6/2019 WinAPC#_01

    28/36

    @OOLA Creating Windows Applications/28

    Step 2: Identify the input controls (Contd)Result: We will use the following controls for thegiven problem statement:

    LabelTextBoxButton

  • 8/6/2019 WinAPC#_01

    29/36

    @OOLA Creating Windows Applications/29

    Step 3: Validating the user input.Result: The program should use the if construct tovalidate the user name and the password. The if construct can also be used to ensure that value of the counter is not greater than 3.Define a counter variable in frmLogin class after allcontrol variables declaration (outside all methods).

    if (counter < 3) { .

    }else {

    MessageBox.Show("Unauthorized Access! Aborting");Application.Exit();

    }

  • 8/6/2019 WinAPC#_01

    30/36

    @OOLA Creating Windows Applications/30

    Step 4: Create a Windows Form.Create a form named frmLogin .Step 5: Add controls to the Windows Form.

    Add three labels lblUserName , lblPassword andlblMessage with following Text property:

    lblUserName with "User Name"lblPassword with "Password lblMessage with Login Message"

    Add two text boxes txtUserName and txtPasswordwith blank as their Text property.Add button btnLogin with "Login" as Text property.

  • 8/6/2019 WinAPC#_01

    31/36

    @OOLA Creating Windows Applications/31

    Step 6: Write the code for validationEnter the following code in Click event of btnLogincounter++;if (counter < 3) {

    if (txtUserName.Text=="sa" && txtPassword.Text=="callcenter"lblMessage.Text="Welcome! You are logged in";

    elselblMessage.Text= Wrong user name and password. Try again";}else {

    MessageBox.Show("Unauthorized Access! Aborting ");Application.Exit();

    }

    Step 7: Execute the application.

  • 8/6/2019 WinAPC#_01

    32/36

    @OOLA Creating Windows Applications/32

    Common Dialog ClassesAre used to access the default Font dialog box tochange the font of the text or used to open a file byusing the Open dialog box and display the contents.Are of the following types:

    ColorDialog

    FontDialogFileDialogPrintDialogPageSetupDialog

    Are displayed with ShowDialog() function. Forexample if colorDialog1 is an instance of ColorDialogclass, it can be displayed with following code:colorDialog1. ShowDialog() ;

  • 8/6/2019 WinAPC#_01

    33/36

    @OOLA Creating Windows Applications/33

    Common Dialog ClassesColorDialog ClassTo change the background and the foreground color of text.

    FontDialog ClassTo change the font, the font style, and the size of text.

    FileDialog ClassIs an abstract class that is inherited from theCommonDialog class.It cannot be instantiated directly.One can use the OpenFileDialog or SaveFileDialog classto open a file or save an existing file.

    PrintDialog ClassTo print text or graphics.

    PageSetupDialog ClassTo set the page details for printing in Windowsapplications.

  • 8/6/2019 WinAPC#_01

    34/36

    @OOLA Creating Windows Applications/34

    Just a MinuteYou have created two forms named Form1 andForm2. Write the code so that when a user clicks theOK button in Form1, Form2 should be displayed.

  • 8/6/2019 WinAPC#_01

    35/36

  • 8/6/2019 WinAPC#_01

    36/36

    @OOLA Creating Windows Applications/36

    SummaryIn this lesson, you learned that:

    A form is used to accept input from the user andpresent information to the user.An event gets generated on performing an actionsuch as clicking the mouse or pressing a key from

    the keyboard.When an event is raised, the code within the eventhandler is executed.Controls can be added to a form to accept input fromthe user or display some information on the form.Some commonly used controls are TextBox, Label,CheckBox, RadioButton, and Button.The CommonDialog class is the base class for themost commonly used dialog boxes, such as, Font,File, Print, and Page Setup.