intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Chapter 3: Programming with Windows Forms

Chia sẻ: Nguyenhien Hien | Ngày: | Loại File: PPT | Số trang:18

201
lượt xem
52
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Agenda: Introduction Windows Forms, How to handle events in Windows Forms, Adding controls to forms (design-time), Dynamically adding controls to Forms (runtime), Using Complex Controls, Creating GUI Components, Working with Menu, Creating MDI applications with Windows Forms, Deploying Windows Forms Applications.

Chủ đề:
Lưu

Nội dung Text: Chapter 3: Programming with Windows Forms

  1. Chapter 3 Programming with Windows Forms Department of Software Engineering Faculty of Information Technology Natural Sciences University
  2. Agenda Introduction Windows Forms How to handle events in Windows Forms Adding controls to forms (design-time) Dynamically adding controls to Forms (runtime) Using Complex Controls Creating GUI Components Working with Menu Creating MDI applications with Windows Forms Deploying Windows Forms Applications
  3. What is Windows Forms (a.k.a. WinForms)? Windows Forms is part of the .NET framework core classes in System.Windows.Forms namespace design-time support in various namespaces Windows Forms provides classes for building UIs e.g. custom forms, common controls, standard dialogs Visual Studio .NET provides tools for using Windows Forms templates for common starting places, and a visual designer
  4. Windows Forms Application Structure A Windows Forms application has three pieces the application itself forms in the application controls on the form Application MyForm Label label1 “Hell…” mainForm Button button1 “OK”
  5. System.Windows.Forms.Application The Application class represents the application itself no instances (all properties and methods are static) processes UI events delivered by Windows Run, DoEvents provides access to application environment ExecutablePath, StartupPath CommonAppDataPath, UserAppDataPath CommonAppDataRegistry, UserAppDataRegistry class MyApp { public static void Main() { MyForm form = new MyForm(); System.Windows.Forms.Application.Run(form); } }
  6. System.Windows.Forms.Form Instances of the Form class represent windows provide window-style services, e.g. properties: Text, Size, Location, Controls methods: Show, ShowDialog, Close events: Load, Click, Closing custom forms typically derive from base Form class class MyForm : Form { public MyForm() { this.Text = "This is my form!"; this.Location = new Point(10, 10); this.Size = new Size(100, 100); } }
  7. Form appearance Various properties influence a form’s appearance Property Type Purpose Text string Text to display (if applicable) Location Point Upper/left coordinate of form Size Point Width/height of form Font Font Get/set displayed text font ForeColor Color Get/set foreground color Opacity double Get/set degree of opacity (as a %) (many more…) … … Often, changing a property results in event notification Move (Location), Resize (Size), FontChanged (Font)
  8. Controls Controls are visual components System.Windows.Forms.Control is base class for UI elements e.g. Form, Button, Label, TextBox, ListBox, etc. contained and arranged by parent (usually a Form) held in parent’s Controls collection public class MyForm : Form { private Button button1; public MyForm() { button1 = new Button(); button1.Text = "Click Me!"; button1.Location = new Point(10, 10); this.Controls.Add(button1); } }
  9. Control interaction (part 1) Public events are used by control “consumers” containing form/control registers for events of interest public class MyForm : Form { private Button button1; public MyForm() { button1 = new Button(); ... button1.Click += new EventHandler(button1_Click); } void button1_Click( object sender, EventArgs e ) { // Handle event as needed... } }
  10. Control interaction (part 2) Derived controls/forms have two approaches may register for base class events may override corresponding virtual functions (if provided) Decision driven by functionality desired, not performance event versus override approach equivalent in many cases overriding provides control over when/if events are fired public EveryOtherClickButton : Button { private int clickNum = 0; protected override void OnClick( EventArgs e ) { clickNum++; if( (clickNum % 2) == 0 ) base.OnClick(e); // Button.OnClick fires Click event } }
  11. Complex Controls Docking Controls Anchor a control to one edge of its container Make a control fill the available space in its container Splitter Windows Allow docked controls to be resized at run time by the user Panels Control Group controls together or subdivide a form into functional areas TreeView and ListView controls Controls for Navigating Data
  12. The Explorer Interface
  13. User Controls A User Control has all the basic functionality for a graphical control that will be used on a Windows Form. Inherits from System.Windows.Forms.UserControl Inherited Properties can override these properties. Inherited Events can override these inherited events if you need to, but it is best if you override only the inherited On method instead of directly overriding the base event, so that future controls may benefit from the standards.
  14. Menu MainMenu component displays a menu at run time. All submenus of the main menu and individual items are MenuItem objects. ContextMenu component is used to provide users with an easily accessible Pop-up menus that appear when you right-click a form or control.
  15. MDI applications MDI – Multiple Document Interface Form Properties IsMDIContainer MDIParent ActiveMDIChild MenuItem Properties MergeType & MergeOrder MDIList Arrange MDIChilds MdiLayout.Cascade, MdiLayout.TileHorizontal, or MdiLayout.TileVertical
  16. Deploying Windows Forms Applications Microsoft Windows Installer Service keeps track of every application that's installed on a computer allows to uninstall, repair, or reinstall a package based on the state of the machine allows to roll back installations. Deployment Projects Templates
  17. Deploying Windows Forms Applications (cont) Creating a Windows Installer Package Setup project properties File Installation Management Registry Settings Management File Types Management User Interface Management Custom Actions Management Launch Condition Management
  18. References www.msdn.microsoft.com MS Press Microsoft Visual C Sharp Dot NET Step By Step Version.2003 - l-mcs301-2003- 11-25 Sams Teach Yourself Visual.Studio.Dot.Net.2003.In.21Days - l- stdn02-2003-7-11.rar FTP: 172.29.22.45 Username: sv Password: sv Directory: dotNET
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2