Your SlideShare is downloading. ×
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply



Published on

Published in: Technology

1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide
  • Complete objectives from the text: at the completion of this chapter, you will be able to ... 1. Describe the process of visual program design and development. 2. Explain the term object-oriented programming. 3. Explain the concepts of classes, objects, properties, methods, and events. 4. List and describe the three steps for writing a C# program. 5. Describe the various files that make up a C# project. 6. Identify the elements in the Visual Studio environment. 7. Define design time , run time , and debug time. 8. Write, run, save, print, and modify your first C# program. 9. Identify syntax errors, run-time errors, and logic errors. 10. Look up C# topics in Help.
  • Click on each element type to see an arrow to the examples in the figures.
  • GUI is pronounced "gooey".
  • OOP terminology is used throughout the text. The theory is presented in Chapter 12.
  • Express Editions are available for (currently) free download from Microsoft. The version that is bundled with this text is an evaluation version of the Professional Edition. The evaluation version can be used for 180 days. The version supplied to schools in the Academic Alliance is the Professional Edition. All programs in this text can be done with the Express Edition and Visual Web Developer (also a free download from Microsoft).
  • Planning: Design the user interface—draw a sketch of the screens the user will see when running the project; include forms and all controls that are to be used including the names to be assigned Plan the properties—for each object write down the properties that you plan to set or change during the design of the form Plan the C# code—plan the methods that will execute when the project runs including determining which events require action to be taken and then make a step-by-step plan for the actions. Use english-like pseudocode for the actions, rather than follow C# syntax. Programming: Define the user interface--create forms and controls Set the properties--give each object a name and define attributes such as the contents of a label. Write the C# code—use C# programming statements to carry out the actions needed by the program
  • The next slide shows the files in a project.
  • HelloForm.cs – Code written by the programmer HelloForm.Designer.cs – Code automatically generated by the Form Designer. A beginning programmer should ignore this file. HelloForm.resx – Resource file for the form. Can contain graphics and text strings for display on the form. HelloWorld.csproj – Project file.A text file that lists the files included in the project. HelloWorld.sln – Solution file. A text file that lists the projects included in the solution. HelloWorld.suo – Solution User Options file. Keeps the state of the environment from one run to the next. If you delete this file, the IDE reverts to the defaults. Program.cs – An automatically-generated file that is used to begin execution of a C# application. In Chapter 6 we edit this file if we want to change the startup form. HelloWorld.csproj.user – Project User Option file. Holds settings for the state of the environment for the project.
  • Form Designer – Lay out the form (the user interface), adding the controls, such as buttons and boxes. Editor – Write and edit the C# code. Compiler – Convert the C# code into runnable code (MSIL – Microsoft Intermediate Language). Debugger – Includes tools to single-step through program statements and display intermediate results. Covered in Chapter 4. Object Browser – Shows classes, objects, properties, methods, and events of all system elements, as well as classes you write. Covered in Chapter 12. Help –Includes extensive documentation, both local and on the Web. Referred to as MSDN – Microsoft Developers’ Network.
  • The title bar indicates the current mode. For Design Time, only the solution name displays. For Run Time, the solution name and (Running) display. For Debug Time, the solution name and (Debugging) display.
  • Note: You also can begin a new project from the Start Page, if it is displaying. This text assumes it to be closed.
  • In the Options dialog box, select Show all settings . Expand the Environment node and click on Startup . Drop down the list for At startup and select Show empty environment . By default, Show Start Page is selected.
  • Draw the sketch by hand and label the form and controls with the names you plan to give them.
  • Snap lines appear as you draw or move controls on the form. They can help align lefts, rights, or the text base line of controls.
  • Select a control on the form and the control’s properties appear in the Properties window. The default name of the first label is “label1” and the Text property is also “label1”. The namespace is the library location that holds the definition of the Label class.
  • Set the AutoSize property to False so that it doesn’t disappear when deselected.
  • Naming conventions are covered later. This style is called camel casing, and is the preferred convention for naming objects.
  • The next slide describes changing the form’s name and file name.
  • If you change the name of the form’s class in the properties window, you also must manually change the name of the file. It’s much better to change the name of the file and allow the IDE to rename the form’s class to match.
  • Details for comments, statement terminators, assignment statements, and executing a method are covered in the following slides.
  • Changing the name or deleting an event-handling method causes an error unless the change is also made in the Properties window. C# creates a program statement that is hidden in the FormName.Designer.cs file that assigns the method to the event. Although you can change the line of code using the Editor, the better solution is to make the change in the Properties window.
  • The next two slides detail the rules and conventions
  • MSDN – Microsoft Developer Network library. A good way to begin using Help is to view the topics that demonstrate how to look up topics in Help.
  • Transcript

    • 1. Programming in Visual C# 2005 Chapter 1 Introduction to Programming and C#
    • 2. Chapter Objectives (1 of 2)
      • Describe the process of visual program design and development
      • Explain the term object-oriented programming
      • Explain the concepts of classes, objects, properties, methods, and events
      • List and describe the three steps for writing a C# program
      • Describe the various files that make up a C# project
    • 3. Chapter Objectives (2 of 2)
      • Identify the elements in the Visual Studio environment
      • Define design time , run time , and debug time
      • Write, run, save, print, and modify your first C# program
      • Identify syntax errors, run-time errors, and logic errors
      • Look up C# topics in Help
    • 4. Creating Windows Applications
      • Your C# programs will look and behave like standard Windows applications
      • Use standard elements
       Labels  Text Boxes  Buttons  Radio Buttons  Picture Boxes  Check Boxes  Menus  Drop-Down Lists  List Boxes  Group Boxes Click each element type to see example
    • 5. Windows Graphical User Interface (GUI)
      • Windows are called forms
      • Elements are called controls
      • Add controls to your forms using a toolbox
      • The controls that you add look like and behave like standard Windows controls
    • 6. Programming Languages—Procedural, Event Driven, and Object Oriented
      • Procedural—Cobol, Fortran, Basic
        • Program specifies exact sequence of operations
      • Event Driven—Early versions of Visual Basic
        • Contains some elements of object orientation but not all
      • Object Oriented Programming (OOP)
        • User controls the sequence
        • Currently the most popular style of programming
    • 7. Object-Oriented Programming (OOP)
      • Each object is based on a class (blueprint or template)
        • Objects (noun, or thing)
        • Properties (attributes)
          • Name, Color, Size, Location
        • Methods (verb, or action)
          • Close, Show, Clear
        • Events (occurrence; user takes an action)
          • Click, KeyPress
    • 8. OOP Terminology
      • Object.Property (Object dot Property)
        • MainForm.BackColor
        • SalesForm.Text
      • Object.Method (Object dot Method)
        • BillingForm.Show
        • NameTextBox.Clear
    • 9. Object Model Analogy
      • Class = automobile
      • Object = one specific automobile
        • Object is an instance of the automobile class
      • Properties = make, model, color, year
      • Methods = start, stop, speedup, slowdown
      • Events = car arrives, car crashes
    • 10. Microsoft’s Visual Studio 2005
      • Includes:
      • .NET Framework
        • Environment That Allows Objects from Many Languages to Interoperate
        • Each language is Converted to Microsoft Intermediate Language (MSIL)
        • MSIL runs in the Common Language Runtime (CLR)
      • The Development Environment
        • Tools for Writing and Testing Programs
      • Programming Languages
        • Visual C#, Visual Basic, Visual J#, C++
      • C# Versions
        • Express Edition, Standard Edition, Professional Edition, Team System
    • 11. Steps for Planning & Writing C# Programs
      • Planning the program
        • Sketch the user interface
        • List the objects and properties needed
        • Plan the event-handling methods (the actions)
      • Creating the program
        • Define the user interface
        • Set the properties
        • Write the C# code
    • 12. C# Application Files
      • Each application is called a solution
        • One solution can contain multiple projects
        • Solution file — .sln extension
      • Projects
        • Can contain multiple forms
        • Project file — .csproj extension
      • Form files
        • .cs extension
      • The Visual Studio environment creates several more files
    • 13. Files for a C# Project Form files Project file Solution file Solution user options file C# File that runs the program Project user option file Resource File for the form
    • 14. The Visual Studio Environment
      • Integrated development environment (IDE)
      • Includes various tools including:
        • Form Designer
        • Editor for entering C# code
        • Compiler
        • Debugger
        • Object Browser
        • Help
    • 15. Default Environment Settings
      • Set to Visual C# Development Settings
        • Can Do when installing Visual Studio
        • Also Can Do with
        • Tools/Import and Export Settings
        • and Reset all settings
      • Turn Off Display of Start Page, if desired
        • Can Turn it On or Off, as desired
        • Textbook Exercises & Figures Assume that the Start Page is not used
    • 16. Begin a C# Project
      • Select File/New Project
      • New Project Dialog Box
      Select Visual C# Windows Select the Windows Application template Enter the Project Name
    • 17. The Visual Studio IDE Toolbox Main Document window Solution Explorer Properties window  Each window can be moved, resized, closed, or customized
    • 18. IDE Toolbars
      • Toolbar buttons are shortcuts for menu items
      • Display or hide toolbars
        • Select View/Toolbars to toggle on/off
        • Right-click in menu or toolbar area to display list of available toolbars and select to toggle on/off
    • 19. Toolbox & Document Window Pin Toolbox open with pushpin icon Use tab to change to Editor to edit code Form Designer tab – Use to create form Main Document window
    • 20. Help Filte r Help with specific tasks Selected topic Help Search Index Results – Select topic to display Help Favorites tab Help Contents tab Index tab
    • 21. IDE — 3 Modes of Operation
      • Design Time
        • Design the form
        • Write the code
      • Run Time
        • Run and test the program
        • Title bar of IDE indicates (Running)
      • Debug Time
        • Find and fix errors
        • Title bar of IDE indicates (Debugging)
      Look at the IDE’s title bar to determine the current mode
    • 22. Writing Your First C# Program – 1
      • Run Visual Studio 2005
      • Start a new project
        • File/New Project
        • Name the project “HelloWorld”
    • 23. Writing Your First C# Program – 2
      • Customize the environment
        • Reset to the default settings
          • Window/Reset Window ; respond Yes
        • Pin open the Toolbox
      Click the pushpin icon to pin the Toolbox open Point to the Toolbox icon to make the Toolbox appear
    • 24. Writing Your First C# Program – 3
        • Set the option to turn off the Start Page
            • (Optional)
          • Select Tools/Options and Show all settings
    • 25. Writing Your First C# Program – 4
      • Plan the project
        • Sketch the user interface
        • Steps to plan the properties and code are already done for this first sample program
    • 26. Writing Your First C# Program – 5
      • Use the tools in the Toolbox to add controls to the form
      Double-click the Label tool to add a Label control to the form
    • 27. Writing Your First C# Program – 6
      • Draw a button on the form
      • Click the tool and draw with the crosshair pointer
      • Use snap lines to help align the controls
      Draw the button control using the crosshair pointer Snap line
    • 28. Writing Your First C# Program – 7
      • Select a control by clicking on it
      • A selected control has resizing handles
      Point to a resizing handle and drag to resize Resize a selected control Point inside control and drag to a new location Move a selected control Press the Delete key Delete a selected control Click on it Select a control
    • 29. Writing Your First C# Program – 8
      • Add a second button and align the controls
      • Set the properties for the form and controls
      • Name property is used to refer to the object in code
      • Text property is the text that displays on the form for the user to see
    • 30. Set Properties in the Properties Window Name of selected object Properties window Object box Settings box Namespace and class of selected object Selected property
    • 31. Using the Properties Window
      • The list of properties can be sorted alphabetically or grouped by function
      • The Name property sorts to the top of the list
      • For most controls you will change the Name property and the Text property
      Sort the Properties list alphabetically Type the new name in the Settings box The new name appears in the Object box
    • 32. Writing Your First C# Program – 9
      • Set properties of the label
        • Name property
          • messageLabel
        • AutoSize property
          • False
        • Text property
          • (Blank)
          • Select it and press Delete
      Label is empty and selected
    • 33. Lock the Controls
      • After controls are where you want them, lock them in place
        • Can unlock later if needed
      Right-click on the form and select Lock Controls A button that is selected and locked
    • 34. Writing Your First C# Program – 10
      • Change properties of the two buttons
        • button1
          • Name – displayButton
          • Text – Display
        • button2
          • Name – exitButton
          • Text – Exit
    • 35. Writing Your First C# Program – 11
      • Change properties of the form
        • Text property appears in the form’s title bar
        • Set StartPostion property to CenterScreen
      Form’s Text property appears in the title bar Icon indicates that the form’s controls are locked
    • 36. Changing the Name of a Form
      • You must change the name of the form’s file and the name of the class
      • If you change the filename first, the IDE automatically changes the name of the class
        • The reverse is not true
      Change the name of the form’s file, keeping the .cs extension
    • 37. Events
      • While your program is running the user can perform actions, each of which causes an event to occur
      • You write event-handling methods for the events you care about; the events you want to respond to with code
      • C# will ignore any events for which you do not write code
    • 38. C# Event Handlers
      • Write C# code in methods
        • Each begins with the words private void
        • Code in method is enclosed in braces { }
      • C# names the event-handling method as the object name, and underscore (_) and the name of the event
        • displayButton_Click
    • 39. Writing Code
      • When writing code for the first project you will use
        • Comment statements
        • Code statements terminated by a semicolon (;)
        • Assignment statements
        • Executing a method of an object
    • 40. Comment Statements
      • Every method should begin with comments that describe the purpose and actions of the method
      • Non-executable statements within the code – have no effect on program execution
      • Automatically colored green by the editor
      • Single-line comments begin with two slashes
        • // Display a message to the user.
      • Multiline comments begin with /* and end with */
      • /* First line
      • ...
      • Last line */
    • 41. C# Statements
      • Most C# statements end with a semicolon (;)
      • A statement can be broken and appear on multiple lines; the semicolon determines the end of the statement
      • Comments and a few other C# statements do not end with a semicolon
        • You will learn about some others later in the course
    • 42. Assignment Statements
      • Assigns a value to a property or variable
      • Operates from right to left – the value that appears on the right side of the equal sign is assigned to the property named left of the equal sign
      • Enclose text strings in quotation marks ( " " )
      messageLabel.Text = "Hello World";
    • 43. Execute a Method to End a Program
      • Methods are always followed by parentheses
        • Can help distinguish methods from properties, which never have parentheses
      • To execute a method of an object, write:
      • Object.Method();
      • Current form may be referenced as this
      • this.Close();
      • Usually you will use this.Close() to end a program in an event-handling method for a Close button
    • 44. Write Code for the First Program
      • Double-click the Display button on the form
      • The Editor window opens
      • The header line for the event-handler is automatically created
      • The insertion point appears between the opening and closing braces
      • private void displayButton_Click(object sender, EventArgs e)
      • {
      • |
      • }
    • 45. Write the Event Handlers Editor tab Form Designer tab Comment statement Assignment statement Execute the Close method to end the program run
    • 46. Run the Program (1 of 2)
      • Use one of three techniques to run
        • Use the Debug menu / Start Debugging
        • Press the Start Debugging toolbar button
        • Press F5, the keyboard shortcut
      Form for the running application IDE title bar shows (Running)
    • 47. Run the Program (2 of 2) Click the Display button The message appears in the label Click the Exit button to end the program and return to Design time
    • 48. Saving a Project (1 of 2)
      • By default, Visual Studio saves to a temporary folder.
      • When you close the project, you can choose to save or discard a temporary project
      • You can choose to save a project – use either File/Save All or the Save All toolbar button
    • 49. Saving a Project (2 of 2)
      • Enter a name and location for the project
      • A new folder is automatically created for the project
      • Do not select the option to Create a directory for the solution, which creates a second folder inside the first
      Do not select this option
    • 50. Modify a Project
      • Can reopen a project and make changes to the design and/or the code
      • Can get to event handlers using the Properties window
      Select the Events button to display the events for the selected control Selected event Properties button Event handler for the Click event – Double click to go quickly to the method in the Editor
    • 51. Print the Code
      • Select File/Print to print the contents of the Editor window
      • The Editor must be open for the menu option to appear
    • 52. Finding & Fixing Errors
      • Syntax errors
        • Breaking the rules of the language
        • Generally found and reported by the Editor, the compiler finds the rest
      • Run-Time errors or exceptions
        • Program halts during execution due to such as an impossible arithmetic operation
      • Logic errors
        • Program runs but produces incorrect results
        • You must carefully proof your output to make sure that it is correct
    • 53. Compiling a Project
      • When you begin execution, the first step is compiling
        • C# statements are converted to Microsoft Intermediate Language (MSIL)
        • Any errors are reported
        • Zero errors is called a clean compile, which is necessary for the program to run
    • 54. Modifying an Event Handler
      • Event handlers are named and connected to controls automatically by C#
      • To change the name of the method or delete a method
        • Use the Events list in the Properties window to select the corrected method name or delete the method name
      Select the event Delete the method name or drop down the list to select a new method name
    • 55. Naming Rules and Conventions
      • Good programmers follow naming rules and conventions
      • Most programming shops have standards that must be followed
      • When programmers consistently follow standards, programs are easier to read and to maintain, by both the original programmer and by others
    • 56. Naming Rules
      • Follow the requirements of the language
        • Name must begin with a letter or an underscore
        • Can contain letters, digits, and underscores
        • Cannot contain a space or punctuation mark
        • Cannot be a reserved word, such as button or Close
        • Names are case-sensitive
          • exitButton and ExitButton refer to 2 different objects
    • 57. Naming Conventions (1 of 2)
      • A set of guidelines for creating "good" names
        • Create a meaningful name
        • Do not use abbreviations unless it is a very commonly used term
        • Append the full class name to the end of the name
        • For objects, use camel casing , which means to begin with a lowercase letter and begin each additional word with an uppercase letter
          • messageLabel, exitButton, discountRateLabel
    • 58. Naming Conventions (2 of 2)
      • Rename all controls on a form, with the exception of labels used for identification on the form that are never referred to in code
      • To name forms and all other classes
        • Use pascal casing , which is to begin with an uppercase letter and capitalize all following words
          • HelloForm, MainForm, AboutForm
    • 59. Visual Studio Help Additional Info
      • MSDN Help is greatly changed and improved for Visual Studio 2005
      • Filter Help to display only C# topics
      • Use context-sensitive help
        • Point to object or click in a word in the Editor
        • Press F1 – Help will open with the selected topic displaying, if possible
        • For help in using the IDE, click on an area of the screen and press Shift + F1