synapseindia dot net development windows programming

Upload: synapseindiaappsdevelopment

Post on 02-Jun-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    1/53

    Windows Programming

    Using C#

    Forms Programming I

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    2/53

    2

    Contents

    System.Drawing Namespace

    System.Windows.Forms Namespace

    Creating forms applications by hand

    Creating forms applications using Visual Studio

    designer

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    3/53

    3

    Forms Programming

    Forms programming allows you to create stand-aloneWindows GUI applications

    The API has been modernized for .NET

    It now supports a modern delegate-basedprogramming model

    Forms programming uses System.Drawing

    Basic GDI+ functionality

    System.Windows.Forms Higher-level controls

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    4/53

    4

    System.Drawing

    This namespace provides many graphic datastructures which are used throughout the GUI

    programming model

    It also provides support for low-level drawingoperations

    These can be used to draw anything, not just what

    is offered by the pre-built controls

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    5/53

    5

    System.Drawing.Point

    Structure which represents a 2-D point

    Constructor

    Point(int x, int y)

    Properties

    Xget/set of X coordinate

    Yget/set of Y coordinate

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    6/53

    6

    System.Drawing.Size

    Structure which stores the width and height of

    something

    Constructor Size(int width, int height)

    Properties

    Widthget/set width

    Heightget/set height

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    7/53

    7

    System.Drawing.Rectangle

    Structure representing a rectangle as the point of the top-left-corner, width andheight

    Constructor Rectangle(Point tlc, Size sz)

    Rectangle(int tlx, int tly, int wd, int ht) Properties

    Xget/set top left X coordinate

    Yget/set top left Y coordinate

    Heightget/set height

    Widthget/set width

    Bottomget Y coordinate of rectangle bottom

    Topget Y coordinate of rectangle top

    Leftget X coordinate of right of rectangle

    Rightget X coordinate of left of rectangle

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    8/53

    8

    System.Drawing.Color

    Structure representing an alpha-RGB color

    Methods Color FromARGB(int r, int g, int b)

    Color FromARGB(int alpha, int r, int g, int b)

    Properties Aget alpha value

    Rget red value

    Gget green value Bget blue value

    Black, White, Red, Green, Yellow, Cyan, Coral,Blue, etc.get values of pre-defined colors

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    9/53

    9

    System.Drawing.Font

    Class representing a font, size and style

    Constructor Font(string family, int points,FontStyle style)

    Properties FontFamilyget the FontFamily value

    Styleget the FontStyle Value

    Sizeget the font size

    Boldget true if bold

    Italicget true if italic

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    10/53

    10

    System.Drawing.FontStyle

    An enumeration with members

    Bold, Italic, Regular, Strikeout, Underline

    The values can be ORed together to indicate thatmore than one style should apply at once

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    11/53

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    12/53

    12

    Form Class

    This is the top-level window class

    This class contains all other controls

    Normally, your top-level form inherits from the

    Form class

    Although the class has numerous methods, most of

    the time you interact with it via properties anddelegates

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    13/53

    13

    Form Properties

    Property Description

    Location Point of to left corner

    Size Size of form in pixels

    Text Text displayed or caption

    AutoScaleDimensions DPI resolution of display it was built for. Will be scaled tolook correct on other displays.

    BackColor Background color

    ForeColor Foreground or drawing color

    ClientSize Size of drawing area without borders or scrollbars

    Controls A collection of controls owned by the form

    WindowState Whether maximized, minimized or normal

    DefaultSize Size when initially created

    MinimumSize Minimum size window can be resized to

    MaximumSize Maximum size window can be resized to

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    14/53

    14

    Form Events

    Forms provide support for a large number of events

    You add one or more delegates to these events

    When the event happens, the delegates are invoked The delegates must have the signature of an event

    handlervoid EventHandler(object sender,

    EventArgs e)

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    15/53

    15

    Form Events

    Event Description

    Load Just before form is loaded the first time

    Closing Just before the form is closed

    Closed When the form is actually closed

    Shown Occurs when a form is first displayed

    ResizeBegin Resize operation has begun

    ResizeEnd Resize operation has ended

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    16/53

    16

    Form Methods

    Method Description

    Activate Activates the window and gives it focus

    Close Closes the form

    Show Makes the form visible

    BringToFront Moves to top of stacking order

    Hide Makes the form invisible

    Focus Gives the form focus

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    17/53

    17

    Creating Windows Applications

    We will demonstrate how to build a simple GUIinterface using a text editor

    Most of the time, the designer in Visual Studio willbe used

    Doing it by hand

    Shows how it works under the hood

    Lets you make modifications by hand Provides an understanding so you can create your own

    controls

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    18/53

    18

    Creating Windows Applications

    In creating a GUI application we will use

    Applicationa class with static methods to control

    operation of an application Labela widget that can display static text or an image

    Buttona push button with a textual or image

    displayed. Able to respond to mouse clicks.

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    19/53

    19

    Creating Windows Applications

    The first step is to create a class which

    Inherits from Form

    Declares the widgets within it

    public class GreetingForm : Form {

    Label greetingLabel;

    Button cancelButton;

    }

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    20/53

    20

    Creating Windows Applications

    Next, create the label and set its properties

    greetingLabel = new Label();

    greetingLabel.Location = new Point(16, 24);

    greetingLabel.Text = "Hello, World";

    greetingLabel.Size = new Size(216, 24);

    greetingLabel.ForeColor = Color.Black;

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    21/53

    21

    Creating Windows Applications

    Create the cancel button and set its properties

    cancelButton = new Button();

    cancelButton.Location = new Point(150,200);

    cancelButton.Size = new Size(112, 32);

    cancelButton.Text = "&Cancel";

    cancelButton.Click += newEventHandler(cancelButton_Click);

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    22/53

    22

    Creating Windows Applications

    Set the properties of the main form

    this.AutoScaleDimensions = newSizeF(95.0f, 95.0f);

    this.ClientSize = new Size(300, 300);

    this.Text = "Hello, World";

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    23/53

    23

    Creating Windows Applications

    Add the controls to the formthis.Controls.Add(cancelButton);

    this.Controls.Add(greetingLabel);

    And provide the event handler

    protected void cancelButton_Click(

    object sender, EventArgs e) {

    Application.Exit();}

    * see HelloForm

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    24/53

    24

    Visual Studio Designer

    This is a drag and drop interface for drawing a GUI

    The code is automatically generated

    You can hook event handlers onto the events andwrite the code for them

    It speeds writing code

    You cannot make major modifications to the code itgenerates

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    25/53

    25

    CheckBoxes

    Labeled boxes which can be checked or unchecked

    Checkedget/set Boolean to determine if box ischecked

    CheckedChangeddelegate called when the box is

    checked or unchecked

    * see ListBoxDemo

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    26/53

    26

    GroupBox

    Displays a border around a group of controls

    Can have optional label controlled by Text property Controls can be added by

    Placing them within the group box in the designer

    Adding to the Controls list programmatically

    * see TextBoxDemo

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    27/53

    27

    Panels

    A panel is like a group box but does not have a textlabel

    It contains a group of controls just like group box BorderStyleget/set border style as

    BorderStyle.Fixed3D

    BorderStyle.FixedSingle

    BorderStyle.None

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    28/53

    28

    Radio Buttons

    Radio buttons are similar to checkboxes, but

    Appear slightly different

    Allow buttons to be grouped so that only one can be

    checked at a time

    A group is formed when the radio buttons are in the

    same containerusually a group box or panel

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    29/53

    29

    Radio Buttons

    Checkedget/set Boolean indicating if the button is

    checked

    CheckedChangeddelegate invoked when the

    button is checked or unchecked

    * see TextBoxDemo

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    30/53

    30

    TextBox

    This is a single line or multi-line text editor

    Multilineget/set Boolean to make multiline

    AcceptsReturnin a multiline box, if true thenpressing Return will create a new line. If false then the

    button referenced by the AcceptButtonproperty of the

    form, will be clicked.

    PasswordCharif this is set to a char, then the box

    becomes a password box

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    31/53

    31

    TextBox

    ReadOnlyif true, the control is grayed out and willnot accept user input

    ScrollBarsdetermines which scrollbars will be

    used: ScrollBars.None, Vertical, Horizontal, Both TextAlignget/set HorizontalAlignment.Left,

    Center, or Right

    TextChangedevent raised when the text is changed

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    32/53

    32

    File Dialog

    The file dialog allows you to navigate throughdirectories and load or save files

    This is an abstract class and you use OpenFileDialog

    SaveFileDialog

    You should create the dialog once and reuse it so

    that it will remember the last directory the user hadnavigated to

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    33/53

    33

    File Dialog

    InitialDirectorystring representing the

    directory to start in

    Filtera string indicating the different types offiles to be displayed

    A set of pairs of display name and pattern separated by

    vertical bars

    Windows Bitmap|*.bmp|JPEG|*.jpg|GIF|*.gif

    FilterIndexthe filter to use as an origin 1 index

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    34/53

    34

    File Dialog

    FileNamethe name of the file selected

    ShowDialoga method to show the dialog and block untilcancel or OK is clicked

    if (openDialog.ShowDialog() == DialogResult.OK) {

    Image img = Image.FromFile(openDialog.FileName);

    pictureBox1.Image = img;

    }

    * see ImageViewer

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    35/53

    35

    Image Class

    An abstract class that can store an image

    Several concrete classes are used for image typessuch as BMP, GIF, or JPG

    FromFile(string fname)loads any supportedimage format from a file

    FromStream(stream)loads an image from a stream

    Heightimage height

    Widthimage width *see ImageViewer

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    36/53

    36

    PictureBox Class

    This displays an image

    Imageassigned an Image object to display

    SizeModedetermines what to do if the image does not

    fit into the window Normal

    StretchImage

    AutoSize

    CenterImage Zoom

    * see ImageViewer

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    37/53

    37

    ToolTips

    These are the small pop-up boxes which explainthe purpose of a control

    To use Create a new tooltip in the designer

    Drop the tooltip onto the form

    The tooltip will appear on a tray below the form

    * see ImageViewer

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    38/53

    38

    ToolTips

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    39/53

    39

    ToolTips

    After the tooltip appears in the tray, a new tooltip

    property appears for every component

    This can be assigned different text for eachcomponent

    That text will be displayed when the mouse hovers

    over that component

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    40/53

    40

    NumericUpDown

    This allows the selection of an integer from a limited range

    Also called a spinner Minimumsmallest selectable value

    Maximumlargest selectable value

    Incrementsize of increment per click

    Valuethe selected value

    ValueChangedevent raised when the value changes

    * see DateSelector

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    41/53

    41

    MonthCalendar

    A control which displays a calendar for the selection

    of a range of dates MinDatethe first selectable date

    MaxDatethe last selectable date

    SelectionStartDateTime of start of selection

    SelectionEndDateTime of end of selection

    DateChangedevent raised when date is changed

    * see DateSelector

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    42/53

    42

    DateTimePicker

    Similar to a month calendar but Calendar pulls down and selection displayed

    More configurable

    Selects a single value, not a range Properties/methods

    FormatLong, Short, Time, Custom

    ValueDateTime value selected

    ValueChangedevent which fires when date or timechanges

    * see DateSelector

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    43/53

    43

    System.DateTime Structure

    A structure representing a date and time

    Constructors

    DateTime(int d, int m, int y) DateTime(int d, int m, int y, int h,int m, int s)

    Properties

    Nowreturns a DateTime object set to the current localtime

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    44/53

    44

    DateTime

    Dayday from 1-31

    Monthmonth from 1-12

    Yeartear from 1-9999 Hourfrom 0-23

    Minuteminute from 0 -59

    Secondsecond from 0 -59 Millisecondmillisecond from 0-999

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    45/53

    45

    DateTime

    DayOfWeekget enumeration of Sunday, Monday,

    DayOfYearday of year from 1366

    Methods DateTime AddYears(double value)

    DateTime AddMonths(double value)

    DateTime AddDays(double value)

    DateTime AddHours(double value)

    DateTime AddSeconds(double value)

    DateTime AddMilliseconds(double value)

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    46/53

    46

    DateTime

    TimeSpan Subtract(DateTime)

    int CompareTo(DateTime)

    static DateTime Parse(string) ToLongDateString()

    ToShortDateString()

    ToLongTimeString()

    ToShortTimeString()

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    47/53

    47

    ListBox

    The ListBox presents a list of items which can be

    selected A scrollbar is displayed if needed

    MultiColumndisplays list as multiple columns

    SelectedIndexindex of selected item

    SelectedIndicescollection of selected indices

    SelectedItemthe selected item

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    48/53

    48

    ListBox

    SelectedItemscollection of selected items

    SelectionModehow items can be selected

    Noneno selection

    Onesingle selection

    MultiSimpleeach click selects additional item

    MultiExtendeduses shift and control keys

    Sortedif true the items will be sorted alphabetically

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    49/53

    49

    ListBox

    Itemsa collection of items in the list box

    ClearSelectedmethod to clear selection

    GetSelectedreturns true if the parameter passedis selected

    SelectedIndexChangedevent when selection

    changes

    * see ListBoxDemo

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    50/53

    50

    Populating a ListBox

    Any object can be placed into a ListBox

    The display is generated by ToString()

    for(int i = 0; i < 50; i++) {

    listBox1.Items.Add(

    "Item " + i.ToString());

    }

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    51/53

    51

    ComboBox

    A combo box is like a list but lets you displays a

    selected value.

    The list pulls down when a selection is being made. Options allow the selected text to be editable or to

    require it to be selected from the drop-down list

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    52/53

    52

    ComboBox

    DropDownStyle

    Simpletext is editable & list always visible

    DropDowndefault indicating text is editable & usermust click to see list

    DropDownListvalue is not editable & user must

    click to see list

    Itemsthe collection of items in the list

  • 8/10/2019 Synapseindia Dot Net Development Windows Programming

    53/53

    53

    ComboBox

    MaxDropDownItemsmax number of items inpulldown before scrollbar used

    SelectedIndexindex of selection

    SelectedItemselected item

    Sortedwhether entries are sorted

    SelectedIndexChangedevent raised when

    selection changes