Download presentation
Presentation is loading. Please wait.
Published byNora Randall Modified over 9 years ago
1
Appendix A: Windows Forms
2
2 Overview Describe the structure of a Windows Forms application –application entry point –forms –components and controls Introduce deployment over networks
3
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
4 Visual Studio Visual Studio.NET provides development environment –starter templates, drag-and-drop editing, property grid, etc.
5
5 Application Structure Windows Forms application has three main pieces –the application itself –forms in the application –controls and components on the forms myForm label1 button1 Label "Hello, World!" Button "OK" Application mainForm
6
6 Application Application class represents the application itself –Run method processes UI events delivered by Windows –provides access to application environment ExecutablePath, 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
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
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
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
10 class MyForm : Form { Button button1 = new Button(); Label label1 = new Label (); public MyForm() {... this.Controls.Add(button1); this.Controls.Add(label1 ); }... } myForm Form as container Form manages a list of controls –stored in Controls collection property Controls button1label1 all forms inherit Controls collection add to collection
11
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
12 Controls Controls are visual components derived from Control class –common controls supplied ScrollableControl Object Component Control ButtonBase Button CheckBox RadioButton ContainerControl Form PropertyGrid UserControl Panel TabPage ListControl ComboBox ListBox MonthCalendar PictureBox ProgressBar Splitter StatusBar DataGrid DateTimePicker GroupBox Label
13
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
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
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 the control that generated the event event details delegate void MouseEventHandler (object sender, MouseEventArgs e); delegate void EventHandler (object sender, EventArgs e); delegate void KeyPressEventHandler(object sender, KeyPressEventArgs e);
16
16 EventArgs Event handlers pass event details to handler –passed in EventArgs or derived class object class MouseEventArgs : EventArgs { public MouseButtons Button { get; } public int Clicks { get; } public int X { get; } public int Y { get; }... } public class EventArgs { } generic base class so no event data class KeyPressEventArgs : EventArgs { public char KeyChar { get; }... } info specific to mouse event info specific to keyboard event
17
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);... } register define method
18
18 Components Components are non-visual elements useful in UI –e.g. Timer, FileSystemWatcher, EventLog, etc. –extend System.ComponentModel.Component shown in component tray when added to form components in toolbox
19
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
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 GET /App.exe HTTP/1.1 Client App.exe Server cache App.exe
21
21 Summary Windows Forms supports UI development –applications, forms, controls –integration with designer –no-touch deployment over networks
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.