neal stublen [email protected]. practice solution create a new solution add a winforms project ...

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

Upload: angela-obrien

Post on 11-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

C#: INTRODUCTION

FOR DEVELOPERS

Neal Stublen

[email protected]

Page 2: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

DEPLOYMENT

Page 3: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Practice Solution

Create a new solution Add a WinForms project Add a Class Library project Reference the library from the

WinForms project

Page 4: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

XCopy

Simple file copy Dependencies must already be installed

(e.g. .NET Framework) Won’t install Start menu icons, etc.

Page 5: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Using XCopy Deployment Defaults of bin\Debug, bin\Release within

project folder $(SolutionDir), $(ProjectDir), etc.

http://msdn.microsoft.com/en-us/library/c02as0cs.aspx

$(SolutionDir)_Output\Debug\$(TargetFileName) $(SolutionDir)_Output\Release\$(TargetFileName) C:\Stage\$(SolutionName)\Debug\$(TargetFileName) C:\Stage\$(SolutionName)\Release\$

(TargetFileName)

Page 6: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

ClickOnce

Install from a link on a web page Adds Start menu shortcuts, etc. Adds entry to Programs and Features

for changing and uninstalling the application

Automatic updates

Page 7: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Setup

Create a setup program to be run from the user’s computer

Install components to the GAC (global assembly cache)

Modify the registry Create a setup program when

ClickOnce is not adequate Discontinued after VS2010

Page 8: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

WiX Toolset

Windows Installer XML Toolset Alternative to discontinued Setup

projects http://wixtoolset.org

Page 9: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

USER CONTROLS

Page 10: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

When To Use Them?

Create compound controls Create specialized controls with

modified behavior

Page 11: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

User Control Properties Override UserControl properties [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] [DesignerSerializationVisibility

(DesignerSerializationVisibility.Content)] [Bindable(true)]

Page 12: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Code Practice Create a Label that turns into a TextBox

when clicked Create a User Control Library Add a Label to the form Add a TextBox to the form

Visible = false Override Text property of UserControl to

update Label Handle Click event to edit Label Text Handle Enter key to finish editing

Page 13: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Code Practice

Handle Escape key to cancel editing Handle loss of focus to cancel editing Create an event to signal a text change Ensure Text and TextChanged are

visible in the designer

Page 14: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

EXTENSION METHODS

Page 15: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Extend Existing Classespublic static class Extensions{ public string ToTitleCase(string inText) { }

public int WordCount(string inText) { }}

string title = Extensions.ToTitleCase(title);int count = Extensions.WordCount(title);

Page 16: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Extend Existing Classespublic static class Extensions{ public static string ToTitleCase(this string inText) { }

public static int WordCount(this string inText) { }}

string title = title.ToTitleCase();int count = title.WordCount();

Page 17: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

MULTIPLE THREADS

Page 18: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Thread Class Create a new thread to execute some background

task

Thread t =new Thread(new ThreadStart(ThreadProc_);

t.Start();// Do some other stuff...t.Join();

public static void ThreadProc(){}

Page 19: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Code Practice

Create a new project Create a method that will write the

numbers 1 to 10 to the console Create a secondary thread that will call

this method Start the thread Call the method on the primary thread Join the thread

Page 20: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Code Practice

Modify the method that writes to the console to update a shared integer value 100 times

Run the program and write the counter value to the console

Page 21: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

lock Synchronization

The lock statement allows us to guard access to an object

Page 22: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Code Practice

Update the previous example to prevent concurrent access to the counter

Page 23: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

BackgroundWorker The BackgroundWorker control provides a

way to add background functionality to a form

Handler to do work on a background thread Handlers to report progress and catch

completion IsBusy confirms background process is

running Call RunWorkerAsync() from primary thread

Page 24: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

BackgroundWorker Progress

Call ReportProgress within DoWork handler

Page 25: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Cancel BackgroundWorker Call CancelAsync () from primary thread Check CancellationPending property

within DoWork handler Set Cancel property to true in

DoWorkEventArgs Exit DoWork handler

Page 26: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Code Practice

Create a BackgroundWorker that just updates a progress bar

Page 27: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Async Patternclass Example{ // Synchronous method call public bool LongMethod(string param);

// Asynchronous method call public void LongMethodAsync(string param); public event LongMethodCompletedEventHandler

LongMethodCompleted;}

Page 28: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

MDI APPLICATIONS

Page 29: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Multiple Document Interface Word can be changed from SDI (all

documents appear on taskbar) to MDI (single taskbar icon)

Often replaced by separate top-level Windows or a tabbed interface (e.g. Visual Studio)

Page 30: Neal Stublen nstublen@jccc.edu. Practice Solution  Create a new solution  Add a WinForms project  Add a Class Library project  Reference the library

Using MDI

Set IsMdiContainer to true on parent form

Set MdiParent property on child forms at runtime

Parent’s ActiveMdi property indicates the currently active child form

Parent’s LayoutMdi property can arrange the child forms