Windows Forms

Chia sẻ: Lavie Lavie | Ngày: | Loại File: PDF | Số trang:0

lượt xem

Windows Forms

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Windows Forms Overview about describe the structure of a Windows Forms application (application entry point, forms, components and controls); Introduce deployment over networks. It provides about Visual Studio, Application Structure, Form border; Form properties.

Chủ đề:

Nội dung Text: Windows Forms

  1. Windows Forms
  2. Overview • Describe the structure of a Windows Forms application – application entry point – forms – components and controls • Introduce deployment over networks 2
  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” 3
  4. Visual Studio • Visual Studio .NET provides development environment – starter templates, drag-and-drop editing, property grid, etc. 4
  5. Application Structure • Windows Forms application has three main pieces – the application itself – forms in the application – controls and components on the forms Label Application myForm "Hello, World!" mainForm label1 button1 Button "OK" 5
  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(); start application Application.Run(form); } } 6
  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 define custom form class MyForm : Form { public MyForm() { this.ShowInTaskbar = false; set properties this.Location = new Point(10, 10); this.Size = new Size(100, 100); } } 7
  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 8
  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; } properties public Size MinimumSize { get; set; } public MainMenu Menu { get; set; } public bool ShowInTaskbar { get; set; } public FormBorderStyle FormBorderStyle { get; set; } ... } 9
  10. Form as container • Form manages a list of controls – stored in Controls collection property all forms inherit class MyForm : Form Controls collection { Button button1 = new Button(); Label label1 = new Label (); public MyForm() { ... this.Controls.Add(button1); add to collection this.Controls.Add(label1 ); } ... } myForm Controls button1 label1 10
  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 11
  12. Controls • Controls are visual components derived from Control class – common controls supplied Object Component Control ButtonBase DataGrid ScrollableControl Button DateTimePicker ContainerControl CheckBox GroupBox Form RadioButton Label PropertyGrid UserControl MonthCalendar ListControl ComboBox PictureBox Panel TabPage ListBox ProgressBar Splitter StatusBar 12
  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"; set properties button1.Text = "OK"; ... } } 13
  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; events public event KeyPressEventHandler KeyPress; public event MouseEventHandler MouseDown; public event PaintEventHandler Paint; ... } 14
  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 2 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 event details generated the event 15
  16. EventArgs • Event handlers pass event details to handler – passed in EventArgs or derived class object generic base class public class EventArgs so no event data { } info specific to class MouseEventArgs : EventArgs mouse event { public MouseButtons Button { get; } public int Clicks { get; } public int X { get; } public int Y { get; } ... } info specific to class KeyPressEventArgs : EventArgs keyboard event { public char KeyChar { get; } ... } 16
  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; define void button1_Click(object sender, EventArgs args) method { ... } public MyForm() { ... register button1.Click += new EventHandler(this.button1_Click); ... } } 17
  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 18
  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 window message queue dequeue owning other thread thread access Login access window Name: controls Password: OK Cancel 19
  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 GET /App.exe HTTP/1.1 Server cache App.exe App.exe 20



Đồng bộ tài khoản