C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent...

26
C# Introduction ISYS 350

Transcript of C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent...

Page 1: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

C# Introduction

ISYS 350

Page 2: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

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

• File/New Project/– C# – Windows

» Windows form application– Project name

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

• View/Solution Explorer– Server Explorer– Property Window

• Properties and Events– ToolBox– Project/Add New Item– Property window example

Page 3: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

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

Page 4: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

Form

• Properties:– Name, FormBorderStyle, Text, BackColor,

BackImage, Opacity

• Events:– Load, FormClosing, FormClosed– GotFocus, LostFocus– MouseHover, Click, DoubleCLick

Page 5: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

Typical Controls

• TextBox• Label• Button• CheckBox• RadioButton• ListBox• ComboBox• PictureBox

Page 6: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

Text Box• Properties:

– AutoSize, BorderStyle, CauseValidation, Enabled, Locked, Multiline, PasswordChar, ReadOnly, ScrollBar, TabIndex, Text, Visible, WordWrap, etc.

• Properties can be set at the design time or at the run time using code.

• To refer to a property: – ControlName.PropertyName– Ex. TextBox1.Text– Note: The Text property is a string data type and

automatically inherits the properties and methods of the string data type.

Page 7: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

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.

Page 8: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

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.

Page 9: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

Demo

FirstName

LastName

FullName

.Control properties

.Event: Click, MouseMove, Form Load, etc.

.Event proceduresFullName: textBox3.Text textBox3.Text = textBox1.Text + " " + textBox2.Text;

Demo: Text alignment (TextAlign property)

Page 10: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

Demo

Num1

Num2

Sum =

.Control properties

.Event: Click, MouseMove, Form Load, etc.

.Event proceduresSum: textBox3.Text = (double.Parse(textBox1.Text) + double.Parse(textBox2.Text)).ToString();

Page 11: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

C# Project

• 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 Form1());

Page 12: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

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

Page 13: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

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.

• DataType VaraibleName;• A C# statement ends with “;”

Page 14: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

string DataType

• string Variables: • Examples:

string empName;string firstName, lastAddress, fullName;

• String concatenation: + • Examples:

fullName = firstName + lastName;MessageBox.Show(“Total is “ + 25.75);

Page 15: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

Numeric Data Types

• int, double, decimal• Examples:

double mydouble=12.7, rate=0.07;int Counter = 0;

Page 16: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

The decimal Data Type

• In C#, the decimal keyword indicates a 128-bit data type (16 bytes).

• Compared to double types, it has more precision and a smaller range, which makes it appropriate for financial and monetary calculations.

• Be sure to add the letter M (or m) to a decimal value:

decimal payRate = 28.75m;decimal price = 8.95M;

Page 17: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

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 TextBox control reads keyboard input, such as 25.65. However, the TextBox 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– decimal.Parse

• Examples:int hoursWorked = int.Parse(hoursWorkedTextBox1.Text); double temperature = double.Parse(temperatureTextBox.Text);

Note: We can also use the .Net’s Convert class methods: ToDouble, ToInt, ToDecimal:Example: hoursWorked = Convert.ToDouble(textBox1.Text);

Page 18: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

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 wholeNumber;decimal moneyNumber = 4500m;wholeNumber = (int) moneynumber;

double realNumber;decimal moneyNUmber = 625.70m;realNumber = (double) moneyNumber;moneyNumber=(decimal) realNumber;

Note: All variables come with a ToString() method.Note: We can also use the .Net’s Convert class methods

Page 19: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

Example

double num1;int num2;decimal num3;num1 = Convert.ToDouble(textBox1.Text);num2 = Convert.ToInt16(textBox2.Text);num3 = Convert.ToDecimal(num1) + Convert.ToDecimal(num2);num3 = (decimal)num1 + (decimal)num2;textBox3.Text = num3.ToString();

Page 20: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

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.

Page 21: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

Example

int dividend, divisor, quotient, remainder; dividend = int.Parse(textBox1.Text); divisor = int.Parse(textBox2.Text); quotient = dividend / divisor; remainder = dividend % divisor; textBox3.Text = quotient.ToString(); textBox4.Text = remainder.ToString();

Page 22: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

Change Machine to Return Smallest Number of Coins

int changes, quarters, dimes, nickles, pennies; changes = int.Parse(textBox1.Text); quarters = changes / 25; dimes = (changes % 25) / 10; nickles = (changes - quarters * 25 - dimes * 10) / 5; pennies = changes - quarters * 25 - dimes * 10 - nickles * 5; textBox2.Text = quarters.ToString(); textBox3.Text = dimes.ToString(); textBox4.Text = nickles.ToString(); textBox5.Text = pennies.ToString();

Page 23: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

FV = PV * (1 +Rate) Year

double pv, rate, years, fv; pv = double.Parse(textBox1.Text); rate = double.Parse(textBox2.Text); years = double.Parse(textBox3.Text); fv = Math.Pow(pv * (1 + rate), years); textBox4.Text = fv.ToString();

Page 24: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

Formatting Numbers with the ToString Method

• The ToString 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 ToString() Result

“N” or “n” Number format 12.3 ToString(“n3”) 12.300

“F” or “f” Fixed-point scientific format 123456.0 ToString("f2") 123456.00

“E” or “e” Exponential scientific format 123456.0 ToString("e3") 1.235e+005

“C” or “c” Currency format -1234567.8 ToString("C") ($1,234,567.80)

“P” or “p” Percentage format .234 ToString("P") 23.40%

Page 25: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

Working with DateTime Data

• Declare DateTime variable:– Example: DateTime mydate;

• Convert date entered in a textbox to DateTime data:– Use Convert:

• mydate = Convert.ToDateTime(textBox1.Text);– Use DateTime class Parse method:

• mydate = DateTime.Parse(textBox1.Text);

Page 26: C# Introduction ISYS 350. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.

Comments

• Line comment: //// my comment

• Block comment: /* …… *//* comment 1

Comment 2…Comment n */