Microsoft C# .NET Crash Course - ucy.ac.cy C# .NET Crash Course 2 ... design properties Maintaining...

download Microsoft C# .NET Crash Course - ucy.ac.cy  C# .NET Crash Course 2 ... design properties Maintaining two separate ... Practice: Build a simple calculator Open Form1

of 120

  • date post

    23-Feb-2018
  • Category

    Documents

  • view

    213
  • download

    1

Embed Size (px)

Transcript of Microsoft C# .NET Crash Course - ucy.ac.cy C# .NET Crash Course 2 ... design properties Maintaining...

  • Microsoft C# .NET Crash Course

    .NET Club University Of Cyprus presents:

    Creating Windows Applications

    Introduction to C# .NET

    (Copyright 2005-2011 Panayiotis Andreou)

  • 06/10/2011 2C# .NET Crash Course

    Course contents

    Overview of the Microsoft .NET Platform

    Lesson 1: Creating a new Windows Application

    Lesson 2: Introduction to Windows Forms

    Lesson 3: Adding Controls to a Form

    Lesson 4: Working with Controls

    Lesson 5: Creating MDI Applications

    Lesson 6: Introduction to Visual Basic

    Lesson 7: Building Mobile Applications

    Lesson 8: Deploying Applications

  • 06/10/2011 3C# .NET Crash Course

    Course goals

    Create a new Windows application

    Create Windows Forms and add controls to them

    Learn about different types of controls

    Organize controls on a form

    Create MDI (Multiple Document Interface) applications

  • Overview of the Microsoft .NET Platform

  • 06/10/2011 5C# .NET Crash Course

    What is the Microsoft .NET Platform

    Developer

    ToolsClients

    User

    Experiences

    ASP.NET Web

    Applications

    XML Web

    Services

    Databases

    .NET

    Framework

  • 06/10/2011 6C# .NET Crash Course

    Core Technologies in the .NET Platform

    .NET Framework

    .NET Building Block Services

    Visual Studio .NET

    .NET Enterprise Servers

  • 06/10/2011 7C# .NET Crash Course

    Components .NET Framework

    Base Class Library

    Common Language Specification

    Common Language Runtime

    ADO.NET: Data and XML

    Visual Basic C++ C#

    ASP.NET: Web Servicesand Web Forms

    JScript

    WindowsForms

  • Overview of C# .NET

    Syntax

  • 06/10/2011 9C# .NET Crash Course

    Comments

    Single-line comments are marked with (//) at the start

    //These are single-line comments

    Multiple-line comments are marked with

    /*

    * These are

    * multi-line comments

    */

  • 06/10/2011 10C# .NET Crash Course

    Variables

    Access Modifiers

    public The type or member can be accessed by any other code in the same assembly or another assembly that references it.

    private The type or member can only be accessed by code in the same class or struct.

    protected The type or member can only be accessed by code in the same class or struct, or in a derived class.

    internal The type or member can be accessed by any code in the same assembly, but not from another assembly.

    protected internal The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

  • 06/10/2011 11C# .NET Crash Course

    Variables

    Variable Declaration

    Class Variables

    private string name;

    Method Variables

    Value Types

    string name;

    int x=5;

    Reference Types

    object obj;

    object obj=new object();

    Access Modifier Type Variable name

  • 06/10/2011 12C# .NET Crash Course

    Variables - Arrays

    Variable Declaration

    Class Variables

    private string[] names;

    Method Variables

    Value Types

    string[] names = new string[4];

    int[] x={1,2,3,4};

    Reference Types

    object[] obj;

    object[] obj=new object[4];

    Access Modifier Type Variable name

  • 06/10/2011 13C# .NET Crash Course

    If statement

    if () {

    }

    else if {

    }

    else {

    }

    Single line if statement

    if ()

  • 06/10/2011 14C# .NET Crash Course

    For statement

    FOR statementfor (int I = 1; I

  • 06/10/2011 15C# .NET Crash Course

    While and Do Loop statements

    While statement

    int Counter = 0;

    while (Counter < 20) { // Test value of Counter.

    //Your code

    Counter++; // Increment Counter.

    // break; // Exit loop.

    } // End While loop when Counter > 19.

    DO LOOP statementDo [ statements ][ break ][ statements ]

    While condition

  • 06/10/2011 16C# .NET Crash Course

    Select Case (switch)

    Using using System.Diagnostics;

    int Number = 4;

    // ...

    switch(Number) { // Evaluate Number.

    case 1: // Number between 1 and 3, inclusive.

    case 2:

    case 3:

    Debug.WriteLine("Between 1 and 3");

    break;

    // The following is the only Case clause that evaluates to True.

    case 4:

    Debug.WriteLine("Number 4");

    break;

    default: // Other values.

    Debug.WriteLine("Not between 1 and 4");

    }

  • 06/10/2011 17C# .NET Crash Course

    Void Methods

    Void Methods do not return a value

    void ComputeArea(double Length, double Width) {

    double Area; // Declare local variable.

    if (Length == 0 || Width == 0) { }

    else {

    Area = Length * Width; // Calculate area of rectangle.

    Debug.WriteLine(Area); // Print Area to Immediate window.

    }

    }

    Function Name Parameters

  • 06/10/2011 18C# .NET Crash Course

    Non-Void Methods

    Non-Void Methods return a value

    double CalcSum (double[] Args)

    {

    int I;

    double sum = 0;

    if (Args.Length > 0) { // Check if arguments passed.

    for (I = 0; I < Args.Length; I++)

    {

    sum += Args[I];

    }

    }

    return sum; // Returns latest value of sum.

    }

    Function Name ParametersReturn type

  • Mechanisms for Passing Parameters

    Three ways to pass parameters

    in Pass by value

    in out

    Pass by reference

    out Output parameters

  • Pass by Value

    Default mechanism for passing parameters:

    Parameter value is copied

    Variable can be changed inside the method

    Has no effect on value outside the method

    Parameter must be of the same type or compatible type

    static void AddOne(int x)

    {

    x++; // Increment x

    }

    static void Main( )

    {

    int k = 6;

    AddOne(k);

    Console.WriteLine(k); // Display the value 6, not 7

    }

  • Pass by Reference

    Reference parameters are references to memory locations

    Using reference parameters

    Use the ref keyword in method declaration and call

    Match types and variable values

    Changes made in the method affect the caller

    Assign parameter value before calling the method

    static void AddOne(ref int x)

    {

    x++; // Increment x

    }

    static void Main( )

    {

    int k = 6;

    AddOne(ref k);

    Console.WriteLine(k); // Display the value 7

    }

  • Output Parameters

    Output parameters: Values are passed out but not in

    Using output parameters

    Like ref, but values are not passed into the method

    Use out keyword in method declaration and call

    static void AddOne(out int x)

    {

    x++; // This produces an errorx=5;

    }

    static void Main( )

    {

    int k = 6;

    AddOne(out k);

    Console.WriteLine(k); // Display the value 5

    }

  • Lesson 1

    Creating a new C# Windows Application

  • 06/10/2011 24C# .NET Crash Course

    Creating a new C# Project

  • 06/10/2011 25C# .NET Crash Course

    Visual Studio .NET Windows

    View the solution/project files

    Manage Database/Server connections

    View controls that can be added in forms and project

  • Lesson 2

    Introduction to Windows Forms

  • 06/10/2011 27C# .NET Crash Course

    Forms in Solution Explorer

    Form/Class name

    Partial Class with all design properties

    Maintaining two separate files for the design and code simplifies form development.

  • 06/10/2011 28C# .NET Crash Course

    Form Life Cycle

    1.Load

    2.Activated

    3.FormClosing

    4.FormClosed

    5.Deactivate

  • 06/10/2011 29C# .NET Crash Course

    How to handle Form Events

    Double click on a form to view the Load and other events

  • 06/10/2011 30C# .NET Crash Course

    How to set a Forms Properties

    Categorized View

    Alphabetical View

  • 06/10/2011 31C# .NET Crash Course

    TODO: Set the following properties on Form1

    To build and start your project:

    Press F5 or

    Press the Play ( ) button

    Set the following properties and build your project each time:

    Set MaximizeBox to False

    Set FormBorderStyle to Fixed3D

    Set Size Width to 150

    Set Size Height to 150

    Set WindowState to Maximized

    Set Text to MyFirstForm

  • Lesson 3

    Adding Controls to a Form

  • 06/10/2011 33C# .NET Crash Course

    How to add Controls to a Form

    Open the Toolbox

    With a Form open:

    Double click on a control

    Click on the control and

    Move the mouse over the form

    Click and drag to create the

    desired size of the control

  • 06/10/2011 34C# .NET Crash Course

    Practice (Adding Controls)

    Open Form1s design and

    Add 1 TextBox

    Set the (Name) Property to txtFirstNumber

    Set the Text property to Add first number

    Add 1 ComboBox

    Set the (Name) Property to cboOperation

    Set the Text property to

  • 06/10/2011 35C# .NET Crash Course

    Practice (continued)

    Add 1 TextBox

    Set the (Name) Property to txtSecondNumber

    Set the Text property to Add second number

    Add 1 Label

    Set the (Name) Property to lblEquals

    Set the Text property to =

  • 06/10/2011 36C# .NET Crash Course

    Practice (continued)

    Add 1 TextBox

    Set the