neal stublen [email protected]. tonight’s agenda more form controls multi-form projects ...

38
C#: INTRODUCTION FOR DEVELOPERS Neal Stublen [email protected]

Upload: amberly-jenkins

Post on 26-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

C#: INTRODUCTION

FOR DEVELOPERS

Neal Stublen

[email protected]

Tonight’s Agenda

More form controls Multi-form projects Application debugging Object-oriented programming and

classes Q&A

BUT FIRST...SOME REVIEW

Practice Exercise

Write a program that can record ten student gradesWhat will you use to store the grades?

Find the average of those grades, but don’t include the lowest grade in the averageHow will you determine the

lowest grade?How will you remove it from

the average?

CHAPTER 10

MORE FORM CONTROLS

Radio Button

Grouped with other RadioButtons for mutually exclusive selectionProperty: CheckedEvent: CheckedChanged

Use a GroupBox to indicate which buttons are related

Checkbox

Turns a selection on or offProperty: CheckedEvent: CheckedChanged

Can be indeterminateProperty: CheckedStateEvent: CheckedStateChanged

ComboBox Select one item from a group of items

Property: SelectedIndexEvent: SelectedIndexChanged

DropDownStyleSimpleDropDownDropDownList

ListBox

Select one or more items from a list of itemsProperty: SelectedIndex, SelectedIndices,

SelectionModeEvent: SelectedIndexChanged

Shift and Ctrl keys allow

multiple selection

Group Box

Radio buttons can be grouped together Controls in a group box move together Grouping controls impacts tab order

View Help

Select any control on a form, press F1 Overview of controls Key properties and methods

Which control to use?

Your application prints reports. Which control would you use to:Select one of many report templateShow or hide page headersSelect from no headers, brief headers, or

verbose headersSelect a printer when printing the

report

CHAPTER 10, PART 2

MULTI-FORM PROJECTS

Working with Forms

Additional forms can be added to a project Load event typically initializes a form Typically the form’s name matches the file

where it is definedRenaming the form’s file will rename the form

An application’s initial form is

determined in the Main function

of the Program classAlso set in project properties

Modal vs. Modeless

“Modeless” windows do not restrict access to other windows/formsAn application’s main window/form is

typically modeless “Modal” windows restrict access to other

windows/formsCommonly referred to as “dialog boxes”Access to a “parent” form is blockedFile | Open...

Modal Forms Modal forms typically have a certain visual

appearanceFormBorderStyleControlBox, MaximizeBox, MinimizeBox

Modal forms are displayed with a different methodShow() vs. ShowDialog()

Modal forms communicate information to a parent formForm.Tag and Form.DialogResultButton.DialogResult

Simple Messages

MessageBox is a special modal formMessageBoxButtonsMessageBoxIconMessageBoxDefaultButtonDialogResult

Used for alerting the user or requesting simple feedbackMessageBox.Show(...);

Closing a Form

What happens when we close a form?FormClosing event

What can we do when a form closes?Prompt before closing a formPrevent the form from closing

○ FormClosingEventArgs.Cancel

A Simple Example First form contains a Label for a name and an

“Edit...” Button Second form contains a TextBox with a name

and OK and Cancel Buttons The name should be passed into and out of the

second form The name should update on the first form when

OK is clicked on the second form The second form should confirm

before exiting, but only if the

name was changed

CHAPTER 11

DEBUGGING AN APPLICATION

Debugging Windows

Debug > Windows > LocalsVariables scoped to the current method

Debug > Windows > AutosVariables from current and previous

statements Debug > Windows > Watch View and edit any value in the debugger Debug > Windows > Immediate

Evaluate expressions, call methods

Call Heirarchy

View > Call HeirarchyView all calls to and from any method

Output Window

Debug > Windows > OutputConsole.WriteLine();

Edit and Continue

Some changes can be made while debugging

Visual Studio will incorporate the changes as you debug the application

Debugging Options

Tools > OptionsDebugging

Debug > ExceptionsWhen thrownWhen unhandled

Debug and Release Builds Debug versions of an application contain

additional information for tracking down problems

An assembly is built as either a debug or release version

Debug and Trace Info

System.Diagnostics.TraceRelease and debug builds

System.Diagnostics.DebugOnly debug builds

Logging execution informationWriteLineAutoFlush, IndentSize, etc.

Confirming assumptionsAssert

Additional Help

MSDN Tracing and Instrumenting Applicationshttp://msdn.microsoft.com/en-us/library/zs6s

4h68(v=vs.110).aspx

CHAPTER 12

MORE OBJECT-ORIENTED PROGRAMMING

Classes Fields (data members) Constructors (initialization) Methods Properties

Practice the principle of encapsulationImplementation of an object is encapsulated within

the classHow does a Gradebook object calculate averages?

○ It shouldn’t matter

Tiered Architecture

Presentation layerForms

Middle layer (business logic)Business rules, calculations, special logic

○ Should a customer receive special rewards?○ What is the local sales tax in Overland Park?

Database layerAccess to physical storage

○ MySQL, Oracle?

Namespaces

Partition classes to group functionality Specify classes using fully qualified

name, or use the namespace within a .cs file

StatsCalculator Example Create a StatsCalculator class Create a method for adding numeric values

to the StatsCalculator Create properties that will return:

Total number of valuesSum of valuesAverage of valuesMinimum and maximum values

Create a method that will clear

the calculator

Class Summary

Fields can be readonly Control access with public/private Initialize through constructor or object

initializers Properties have get and set blocks

Read/write, read-only, write-onlyAuto-implemented

Methods can be overloaded Members can be static

Member Variable Defaults Numeric types = 0 Boolean = false Char = 0 Object = null Date = 01/01/0001 @ 12:00am

Class vs. Struct

Difference between reference and value types

Class requires “new”, struct does not

Suggested Practice

Exercise 12-1, p. 391

Start thinking about what classes might be used to model objects in the type of applications you will be writing.

Are there any questions about objects you might be trying to model?