1 / 28

C# Introduction

C# Introduction. ISYS 350. 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

mcniel
Download Presentation

C# Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C# Introduction ISYS 350

  2. 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

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

  4. 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

  5. Form • Properties: • Name, FormBorderStyle, Text, BackColor, BackImage, Opacity • Events: • Load, FormClosing, FormClosed • GotFocus, LostFocus • MouseHover, Click, DoubleCLick

  6. Common Controls • TextBox • Label • Button • CheckBox • RadioButton • ListBox • ComboBox • PictureBox

  7. Text Box • Properties: • 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.

  8. 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.

  9. 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.

  10. Demo FirstName LastName Show Full Name .Control properties .Event: Click, MouseMove, FormLoad, etc. .Event procedures FullName: textBox3.Text textBox3.Text = textBox1.Text + " " + textBox2.Text; Demo: Text alignment (TextBox3.TextAlign=HorizontalAlign.Right) TextBox3.BackColor=Color.Aqua;

  11. Demo Num1 Num2 Compute Sum .Control properties .Event: Click, MouseMove, FormLoad, etc. .Event procedures Sum: textBox3.Text = (double.Parse(textBox1.Text) + double.Parse(textBox2.Text)).ToString(); In-Class lab: Show the product of Num1 and Num2.

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

  13. 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.

  14. 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

  15. 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 “;”

  16. string DataType • string Variables: • Examples: string empName; string firstName, lastAddress, fullName; • String concatenation: + • Examples: fullName = firstName + lastName; MessageBox.Show(“Total is “ + 25.75);

  17. Numeric Data Types • int, double • Examples: double mydouble=12.7, rate=0.07; int Counter = 0; Other: decimal: decimal myMoney = 300.5m; float: float y = 4.5f;

  18. 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 • 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);

  19. 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 iNum1; double dNum1 = 2.5; iNum1 = (int) dNum1; Note: We can also use the .Net’s Convert class methods

  20. Implicit conversion and explicit conversion int iNum1 = 5, iNum2 = 10; double dNum1 = 2.5, dNum2 = 7.0; dNum1 = iNum1 + iNum2; /*C# implicitly convert integer to double*/ iNum1 = (int) dNum1 * 2; /*from doulbe to integer requires cast operator*/

  21. Performing Calculations • Basic calculations such as arithmetic calculation can be performed by math operators Other calculations: Use Math class’s methods.

  22. Modulus Operator, % • Examples: • 7%2 • 25%4 • 7.5%2

  23. 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(); Note: The result of an integer divided by an integer is integer. For example, 7/2 is 3, not 3.5.

  24. 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.

  25. 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();

  26. 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 = pv*Math.Pow(1 + rate, years); textBox4.Text = fv.ToString();

  27. 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

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

More Related