Microsoft® C#® .NET Crash Course
Total Page:16
File Type:pdf, Size:1020Kb
.NET Club University Of Cyprus presents: Microsoft ® C# ® .NET Crash Course Creating Windows Applications Introduction to C# .NET (Copyright © 2005-2011 Panayiotis Andreou) 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 C# .NET Crash Course 2 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 06/10/2011 C# .NET Crash Course 3 Overview of the Microsoft .NET Platform What is the Microsoft .NET Platform Developer Clients Tools ASP.NET Web Databases Applications XML Web User .NET Services Experiences Framework 06/10/2011 C# .NET Crash Course 5 Core Technologies in the .NET Platform • .NET Framework • .NET Building Block Services • Visual Studio .NET • .NET Enterprise Servers 06/10/2011 C# .NET Crash Course 6 Components του .NET Framework Visual Basic ® C++ C# JScript ® … Common Language Specification ASP.NET: Web Services Windows and Web Forms Forms ADO.NET: Data and XML Base Class Library Common Language Runtime 06/10/2011 C# .NET Crash Course 7 Overview of C# .NET Syntax 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 C# .NET Crash Course 9 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 C# .NET Crash Course 10 Variables Variable Declaration Class Variables private string name ; Access Modifier Type Variable name Method Variables • Value Types string name; int x=5; • Reference Types object obj; object obj= new object () ; 06/10/2011 C# .NET Crash Course 11 Variables - Arrays Variable Declaration Class Variables private string[] names ; Access Modifier Type Variable name Method Variables • Value Types string[] names = new string[4]; int[] x={1,2,3,4}; • Reference Types object[] obj; object[] obj= new object [4] ; 06/10/2011 C# .NET Crash Course 12 If statement if (<condition>) { … } else if { … } else { … } Single line if statement if (<condition>) 06/10/2011 C# .NET Crash Course 13 For statement FOR statement for (int I = 1; I<=10; I++) { for (int J = 1; I<=10; J=J+2) { for (int K = 10 K>=1; K--) { // Statements to operate with current values of I, J, and K. } } } FOR each statement bool Found = false; List<Object> objectList = new List<Object>(); foreach (Object obj in objectList) { // Iterate through elements. if (obj.Name == “Hello”) { // If Name equals "Hello" Found = true; // Set Found to True. break; // Exit loop. } else continue; } } 06/10/2011 C# .NET Crash Course 14 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 Do… LOOP statement [ statements ] [ break ] [ statements ] While condition 06/10/2011 C# .NET Crash Course 15 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 C# .NET Crash Course 16 Void Methods Void Methods do not return a value Function Name Parameters 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. } } 06/10/2011 C# .NET Crash Course 17 Non-Void Methods Non-Void Methods return a value Return type Function Name Parameters 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. } 06/10/2011 C# .NET Crash Course 18 Mechanisms for Passing Parameters Three ways to pass parameters in Pass by value in Pass by reference out 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 error x=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 Creating a new C# Project 06/10/2011 C# .NET Crash Course 24 Visual Studio .NET Windows View the solution/project files View controls that can be added in forms and project Manage Database/Server connections 06/10/2011 C# .NET Crash Course 25 Lesson 2 Introduction to Windows Forms Forms in Solution Explorer Maintaining two separate files for the design and code simplifies form development. Form/Class name Partial Class with all design properties 06/10/2011 C# .NET Crash Course 27 Form Life Cycle 1.Load 2.Activated 3.FormClosing 4.FormClosed 5.Deactivate 06/10/2011 C# .NET Crash Course 28 How to handle Form Events Double click on a form to view the Load and other events 06/10/2011 C# .NET Crash Course 29 How to set a Form’s Properties Categorized View Alphabetical View 06/10/2011 C# .NET Crash Course 30 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 06/10/2011 C# .NET Crash Course 31 Lesson 3 Adding Controls to a Form 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 C# .NET Crash Course 33 Practice (Adding Controls) Open Form1’s 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 C# .NET Crash Course 34 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 C# .NET Crash Course 35 Practice (continued) • Add 1 TextBox – Set the (Name) Property to txtResult – Set the Text property to “” • Add 1 Button – Set the (Name) Property to btnCalculate – Set the Text property to “Calculate” 06/10/2011 C# .NET Crash Course 36 Practice (continued) Next we need to resize each control (hint: resize one control and use the Format menu to make each other control follow the same properties) • Set the Form1’s Size Property to 160,192 • Set each control’s Size Property to 136,20 • To resize Labels you need to set the AutoSize property to False • Set each control’s Location X Property to 8 06/10/2011 C# .NET Crash Course 37 Practice (continued) • Set the Location Y Property to –txtFirstNumber 8 –cboOperation 31 –txtSecondNumber 55 – lblEquals 78 – txtResult 104 – btnCalculate 127 06/10/2011 C# .NET Crash Course 38 Practice (continued) When you have finished your Form1 should look like this 06/10/2011 C# .NET Crash Course 39 Let’s talk about controls • TextBox • Label • MainMenu • CheckBox • RadioButton • GroupBox • ListBox • CheckedListBox • ComboBox • TreeView • TabControl • …and so much more!!! 06/10/2011 C# .NET Crash Course 40 Using the Format Menu Use the format menu to automatically align/resize controls. After Selecting multiple controls single click on the leader control