C Introduction ISYS 350 Visual Studio 2017 Demo

  • Slides: 28
Download presentation
C# Introduction ISYS 350

C# Introduction ISYS 350

Visual Studio 2017 Demo • Start page: New project/ Open project/Recent projects • Starting

Visual Studio 2017 Demo • Start page: New project/ Open project/Recent projects • Starting project: – File/New Project/ • Visual C# – Windows Classic Desktop » Windows Form App • Project name/Project folder

Project windows: • Form design view/Form code view • Solution Explorer – View/Solution Explorer

Project windows: • Form design view/Form code view • Solution Explorer – View/Solution Explorer • Tool. Box (visible while in form design view) – View/Tool. Box • • Property Window Properties and Events Project/Add New Item Property window example

Introduction to C# • Event-driven programming – The interface for a C# program consists

Introduction to C# • Event-driven programming – The interface for a C# program consists of one or more forms, containing one or more controls (screen objects). – Form and controls have events that can respond to. Typical events include clicking a mouse button, type a character on the keyboard, changing a value, etc. – Event procedure

Form • Properties: – Name, Form. Border. Style, Text, Back. Color, Back. Image, Opacity

Form • Properties: – Name, Form. Border. Style, Text, Back. Color, Back. Image, Opacity • Events: – Load, Form. Closing, Form. Closed – Got. Focus, Lost. Focus – Mouse. Hover, Click, Double. CLick

Common Controls • • Text. Box Label Button Check. Box Radio. Button List. Box

Common Controls • • Text. Box Label Button Check. Box Radio. Button List. Box Combo. Box Picture. Box

Text Box • Properties: – Border. Style, Cause. Validation, Enabled, Locked, Multiline, Password. Char,

Text Box • Properties: – Border. Style, Cause. Validation, Enabled, Locked, Multiline, Password. Char, Read. Only, Scroll. Bar, Tab. Index, Text, Visible, Word. Wrap, etc. • Properties can be set at the design time or at the run time using code. • To refer to a property: – Control. Name. Property. Name – Ex. Text. Box 1. Text – Note: The Text property is a string data type and automatically inherits the properties and methods of the string data type.

Typical C# Programming Tasks • Creating the GUI elements that make up the application’s

Typical C# Programming Tasks • Creating the GUI elements that make up the application’s user interface. – Visualize the application. – Make a list of the controls needed. • Setting the properties of the GUI elements • Writing procedures that respond to events and perform other operations.

To Add an Event-Procedure • • 1. Select the Properties window 2. Click Events

To Add an Event-Procedure • • 1. Select the Properties window 2. Click Events button 3. Select the event and double-click it. Note: Every control has a default event. • Form: Load event • Button control: Click event • Textbox: Text Changed event – To add the default event procedure, simply double -click the control.

Demo First. Name Last. Name Show Full Name . Control properties. Event: Click, Mouse.

Demo First. Name Last. Name Show Full Name . Control properties. Event: Click, Mouse. Move, Form Load, etc. . Event procedures Full. Name: text. Box 3. Text = text. Box 1. Text + " " + text. Box 2. Text; Demo: Text alignment (Text. Box 3. Text. Align=Horizontal. Align. Right) Text. Box 3. Back. Color=Color. Aqua;

Demo Num 1 Num 2 Compute Sum. Control properties. Event: Click, Mouse. Move, Form

Demo Num 1 Num 2 Compute Sum. Control properties. Event: Click, Mouse. Move, Form Load, etc. . Event procedures Sum: text. Box 3. Text = (double. Parse(text. Box 1. Text) + double. Parse(text. Box 2. Text)). To. String(); In-Class lab: Show the product of Num 1 and Num 2.

C# Project’s Main Method • The execution starts from the Main method which is

C# Project’s Main Method • The execution starts from the Main method which is found in the Program. cs file. – Solution/Program. cs – Contain the startup code • Example: Application. Run(new Form 1());

What does variable mean? • A variable, in the context of programming, is a

What does variable mean? • A variable, in the context of programming, is a symbolic name given to an unknown quantity that permits the name to be used independent of the information it represents. • Variables are associated with data storage locations, and values of a variable are normally changed during the course of program execution.

Variable Names • A variable name identifies a variable • Always choose a meaningful

Variable Names • A variable name identifies a variable • Always choose a meaningful name for variables • Basic naming conventions are: – the first character must be a letter (upper or lowercase) or an underscore (_) – the name cannot contain spaces – do not use C# keywords or reserved words • Variable name is case sensitive

Declare a Variable • C# is a strongly typed language. This means that when

Declare a Variable • C# is a strongly typed language. This means that when a variable is defined we have to specify what type of data the variable will hold. • Data. Type Varaible. Name; • A C# statement ends with “; ”

string Data. Type • string Variables: • Examples: string emp. Name; string first. Name,

string Data. Type • string Variables: • Examples: string emp. Name; string first. Name, last. Address, full. Name; • String concatenation: + • Examples: full. Name = first. Name + last. Name; Message. Box. Show(“Total is “ + 25. 75);

Numeric Data Types • int, double • Examples: double mydouble=12. 7, rate=0. 07; int

Numeric Data Types • int, double • Examples: double mydouble=12. 7, rate=0. 07; int Counter = 0; Other: decimal my. Money = 300. 5 m; float: float y = 4. 5 f;

Inputting and Outputting Numeric Values • Input collected from the keyboard are considered combinations

Inputting and Outputting Numeric Values • Input collected from the keyboard are considered combinations of characters (or string literals) even if they look like a number to you • A Text. Box control reads keyboard input, such as 25. 65. However, the Text. Box treats it as a string, not a number. • In C#, use the following Parse methods to convert string to numeric data types – int. Parse – double. Parse – Examples: int hours. Worked = int. Parse(hours. Worked. Text. Box 1. Text); double temperature = double. Parse(temperature. Text. Box. Text); Note: We can also use the. Net’s Convert class methods: To. Double, To. Int, To. Decimal. Example: hours. Worked = Convert. To. Double(text. Box 1. Text);

Explicit Conversion between Numeric Data Types with Cast Operators • C# allows you to

Explicit Conversion between Numeric Data Types with Cast Operators • C# allows you to explicitly convert among types, which is known as type casting • You can use the cast operator which is simply a pair of parentheses with the type keyword in it int i. Num 1; double d. Num 1 = 2. 5; i. Num 1 = (int) d. Num 1; Note: We can also use the. Net’s Convert class methods

Implicit conversion and explicit conversion int i. Num 1 = 5, i. Num 2

Implicit conversion and explicit conversion int i. Num 1 = 5, i. Num 2 = 10; double d. Num 1 = 2. 5, d. Num 2 = 7. 0; d. Num 1 = i. Num 1 + i. Num 2; /*C# implicitly convert integer to double*/ i. Num 1 = (int) d. Num 1 * 2; /*from doulbe to integer requires cast operator*/

Performing Calculations • Basic calculations such as arithmetic calculation can be performed by math

Performing Calculations • Basic calculations such as arithmetic calculation can be performed by math operators Operator Name of the operator Description + Addition Adds two numbers - Subtraction Subtracts one number from another * Multiplication Multiplies one number by another / Division Divides one number by another and gives the quotient % Modulus Divides one number by another and gives the remainder Other calculations: Use Math class’s methods.

Modulus Operator, % • Examples: – 7%2 – 25%4 – 7. 5%2

Modulus Operator, % • Examples: – 7%2 – 25%4 – 7. 5%2

Example int dividend, divisor, quotient, remainder; dividend = int. Parse(text. Box 1. Text); divisor

Example int dividend, divisor, quotient, remainder; dividend = int. Parse(text. Box 1. Text); divisor = int. Parse(text. Box 2. Text); quotient = dividend / divisor; remainder = dividend % divisor; text. Box 3. Text = quotient. To. String(); text. Box 4. Text = remainder. To. String(); Note: The result of an integer divided by an integer is integer. For example, 7/2 is 3, not 3. 5.

Lab Exercise • Enter length measured in inches in a textbox; then show the

Lab Exercise • Enter length measured in inches in a textbox; then show the equivalent length measured in feet and inches. • For example, 27 inches is equivalent to 2 feet and 3 inches.

Change Machine to Return Smallest Number of Coins int changes, quarters, dimes, nickles, pennies;

Change Machine to Return Smallest Number of Coins int changes, quarters, dimes, nickles, pennies; changes = int. Parse(text. Box 1. Text); quarters = changes / 25; dimes = (changes % 25) / 10; nickles = (changes - quarters * 25 - dimes * 10) / 5; pennies = changes - quarters * 25 - dimes * 10 - nickles * 5; text. Box 2. Text = quarters. To. String(); text. Box 3. Text = dimes. To. String(); text. Box 4. Text = nickles. To. String(); text. Box 5. Text = pennies. To. String();

FV = PV * (1 +Rate) double pv, rate, years, fv; pv = double.

FV = PV * (1 +Rate) double pv, rate, years, fv; pv = double. Parse(text. Box 1. Text); rate = double. Parse(text. Box 2. Text); years = double. Parse(text. Box 3. Text); fv = pv*Math. Pow(1 + rate, years); text. Box 4. Text = fv. To. String(); Year

Formatting Numbers with the To. String Method • The To. String method can optionally

Formatting Numbers with the To. String Method • The To. String method can optionally format a number to appear in a specific way • The following table lists the “format strings” and how they work with sample outputs Format String Description Number To. String() Result “N” or “n” Number format 12. 3 To. String(“n 3”) 12. 300 “F” or “f” Fixed-point scientific format 123456. 0 To. String("f 2") 123456. 00 “E” or “e” Exponential scientific format 123456. 0 To. String("e 3") 1. 235 e+005 “C” or “c” Currency format -1234567. 8 To. String("C") ($1, 234, 567. 80) “P” or “p” Percentage format . 234 To. String("P") 23. 40%

Comments • Line comment: // // my comment • Block comment: /* …… */

Comments • Line comment: // // my comment • Block comment: /* …… */ /* comment 1 Comment 2 … Comment n */