Appendix A: Windows Forms - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Appendix A: Windows Forms

Description:

Common to derive from Form to create custom form. class MyForm : Form. public MyForm ... No-touch deployment. Windows forms applications can be deployed automatically ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 22
Provided by: markt161
Category:

less

Transcript and Presenter's Notes

Title: Appendix A: Windows Forms


1
Appendix A Windows Forms
2
Overview
  • Describe the structure of a Windows Forms
    application
  • application entry point
  • forms
  • components and controls
  • Introduce deployment over networks

3
Windows Forms
  • Windows Forms are classes for building UIs
  • forms, controls, dialogs, etc.
  • part of the .NET Framework Library
  • core classes in System.Windows.Forms namespace
  • Possible uses include
  • stand-alone Windows applications
  • web services front-ends
  • intranet smart clients

4
Visual Studio
  • Visual Studio .NET provides development
    environment
  • starter templates, drag-and-drop editing,
    property grid, etc.

5
Application Structure
  • Windows Forms application has three main pieces
  • the application itself
  • forms in the application
  • controls and components on the forms

Label
myForm
Application
"Hello, World!"
label1
mainForm
button1
Button
"OK"
6
Application
  • Application class represents the application
    itself
  • Run method processes UI events delivered by
    Windows
  • provides access to application environmentExecuta
    blePath, CommonAppDataPath, UserAppDataPath, etc.
  • no instances, all members are static

class MyApp public static void Main()
MyForm form new MyForm()
Application.Run(form)
start application
7
Form
  • Form class represents window, provides
    appropriate services
  • properties Size, Location, Controls,
    ShowInTaskbar
  • methods Show, Close, SetDesktopLocation
  • events Load, Click, Closing
  • Common to derive from Form to create custom form

class MyForm Form public MyForm()
this.ShowInTaskbar false this.Location
new Point(10, 10) this.Size
new Size(100, 100)
define custom form
set properties
8
Form border
  • Forms can look like windows, dialogs, or tools
  • controlled using FormBorderStyle property
  • set ShowInTaskbar property to false for tool
    windows
  • window border styles show the icon from the Icon
    property

9
Form properties
  • Form has many properties
  • used to customize appearance and behavior

public class Form ContainerControl public
IButtonControl AcceptButton get set
public IButtonControl CancelButton get set
public bool HelpButton get set
public Icon Icon get set public
String Text get set public Size
MaximumSize get set public Size
MinimumSize get set public MainMenu Menu
get set public bool ShowInTaskbar get
set public FormBorderStyle FormBorderStyle
get set ...
properties
10
Form as container
  • Form manages a list of controls
  • stored in Controls collection property

all forms inherit Controls collection
class MyForm Form Button button1 new
Button() Label label1 new Label ()
public MyForm() ...
this.Controls.Add(button1)
this.Controls.Add(label1 ) ...
add to collection
myForm
button1
label1
Controls
11
Form layout
  • Form determines layout for controls it manages
  • recalculates as needed, such as when user resizes
    form
  • tries to accommodate control preferences for size
    and location
  • uses Anchor and Dock property of each control
  • can override OnLayout method to do custom layout

resize
12
Controls
  • Controls are visual components derived from
    Control class
  • common controls supplied

13
Control properties
  • Controls supply many properties
  • used to customize appearance and behavior

public class MyForm Form private Button
button1 public MyForm() button1 new
Button() button1.Name "OK button"
button1.Text "OK" ...
set properties
14
Control events
  • Controls offer events
  • click
  • mouse or keyboard activity
  • property value change
  • paint
  • etc.

public class Control Component ... public
event EventHandler Click public event
EventHandler Enter public event
EventHandler TextChanged public event
KeyPressEventHandler KeyPress public event
MouseEventHandler MouseDown public event
PaintEventHandler Paint ...
events
15
Event handlers
  • Several types of event handler delegates
  • generic EventHandler used for many events
  • specific types for some events such as mouse and
    keyboard
  • Event handlers have two arguments
  • control that generated event
  • event details in EventArgs or derived object

delegate void EventHandler (object sender,
EventArgs e)
delegate void MouseEventHandler (object sender,
MouseEventArgs e)
delegate void KeyPressEventHandler(object sender,
KeyPressEventArgs e)
the control that generated the event
event details
16
EventArgs
  • Event handlers pass event details to handler
  • passed in EventArgs or derived class object

generic base class so no event data
public class EventArgs
info specific to mouse event
class MouseEventArgs EventArgs public
MouseButtons Button get public int
Clicks get public int X
get public int Y get
...
info specific to keyboard event
class KeyPressEventArgs EventArgs public
char KeyChar get ...
17
Event registration
  • Clients can register for events
  • define method with signature required by delegate
  • create delegate instance and add to event

public class MyForm Form private Button
button1 void button1_Click(object sender,
EventArgs args) ... public
MyForm() ... button1.Click new
EventHandler(this.button1_Click) ...
define method
register
18
Components
  • Components are non-visual elements useful in UI
  • e.g. Timer, FileSystemWatcher, EventLog, etc.
  • extend System.ComponentModel.Component

components in toolbox
shown in component tray when added to form
19
Threading
  • Control should only be accessed by the creating
    thread
  • messages from different threads subject to
    interleaving
  • resulting race conditions can lead to
    inconsistent behavior
  • Control class provides methods for
    thread-switching
  • see docs for InvokeRequired, Invoke, BeginInvoke

20
No-touch deployment
  • Windows forms applications can be deployed
    automatically
  • client uses browser to access executable
  • needed assemblies downloaded and stored in
    download cache
  • program runs locally
  • executable downloaded only if changed since last
    run

Client
Server
GET /App.exe HTTP/1.1
App.exe
cache
App.exe
21
Summary
  • Windows Forms supports UI development
  • applications, forms, controls
  • integration with designer
  • no-touch deployment over networks
Write a Comment
User Comments (0)
About PowerShow.com