530 likes | 644 Views
Microsoft VB 2005: Reloaded, Advanced. Chapter 6 More about Windows Forms. Objectives. Understand program code generated by Visual Basic for user interfaces Create a user interface with a flow layout and a table layout Create a user interface with tabbed pages and dialog forms
E N D
Microsoft VB 2005: Reloaded, Advanced Chapter 6 More about Windows Forms
Objectives • Understand program code generated by Visual Basic for user interfaces • Create a user interface with a flow layout and a table layout • Create a user interface with tabbed pages and dialog forms • Create a user interface with a menu and shortcut keys • Create an application with a multiple document interface (MDI) Microsoft VB 2005: Reloaded, Advanced
A Closer Look at GUIs • Objective • Understand the code generated by Visual Basic as you build a GUI Microsoft VB 2005: Reloaded, Advanced
Forms and Components • Form • Window or dialog box that makes up an application’s graphical user interface • Instance of the class System.Windows.Forms.Form • Component • Object that is added to a form to provide functionality • Control • Visible component derived from the Control class Microsoft VB 2005: Reloaded, Advanced
The Event-Handling Model • Event • A message indicating that a user action has occurred • Event-driven applications • GUIs generate events (via user actions) that drive the application’s functionality • Event-handlers • Methods written by the programmer to provide the application with the desired responses to events • Visual Basic uses a MulticastDelegate object to implement event handling Microsoft VB 2005: Reloaded, Advanced
The Event-Handling Model (continued) • Delegate • An instance of the Delegate class • Delegate class • Data structure that refers to either: • A static method • A class instance and an instance method of that class • An event generated by a GUI calls a delegate that is registered for that event • Delegate in turn calls all methods (static methods or instance methods) that it references Microsoft VB 2005: Reloaded, Advanced
Visual Basic Generated Code for GUIs • When you create a GUI using the Visual Basic Form Designer • Visual Basic generates the required program code in the Code window • View code by right-clicking the open space in the Windows Form Designer, outside the form • And then clicking View Code • Details of the code for the GUI are hidden from view • Figure 6.3 shows the code generated by Windows Form Designer Microsoft VB 2005: Reloaded, Advanced
Visual Basic Generated Code for GUIs (continued) Microsoft VB 2005: Reloaded, Advanced
Visual Basic Generated Code for GUIs (continued) Microsoft VB 2005: Reloaded, Advanced
Enhancing a GUI’s Appearance and Functionality • Objective • Learn tips for enhancing Windows application forms Microsoft VB 2005: Reloaded, Advanced
Form Startup Location • By default, a form will appear at the Windows default location • Near the upper-left corner of the screen Microsoft VB 2005: Reloaded, Advanced
Form Startup Location (continued) Microsoft VB 2005: Reloaded, Advanced
Making a Form Scrollable • A form may contain more controls than will fit within the boundaries of the form • You can make your form scrollable with scroll bars that appear only when necessary • Select True for the Autoscroll property value Microsoft VB 2005: Reloaded, Advanced
Making a Form Scrollable (continued) Microsoft VB 2005: Reloaded, Advanced
Making a Form Scrollable (continued) Microsoft VB 2005: Reloaded, Advanced
Making a Form Scrollable (continued) Microsoft VB 2005: Reloaded, Advanced
The FlowLayoutPanel Control Microsoft VB 2005: Reloaded, Advanced
The FlowLayoutPanel Control (continued) Microsoft VB 2005: Reloaded, Advanced
The Margin and Padding Properties • Margin property • Determines the amount of space, in pixels, reserved around a control • When calculating its automatic position in a FlowLayoutPanel • Padding property • Determines the amount of space, in pixels, reserved along the inside of containers Microsoft VB 2005: Reloaded, Advanced
The Margin and Padding Properties (continued) Microsoft VB 2005: Reloaded, Advanced
The TableLayoutPanel Control • Allows you to create a rectangular grid of rows and columns in which other controls can be placed • Helps maintain an organized appearance for the form Microsoft VB 2005: Reloaded, Advanced
The TableLayoutPanel Control (continued) Microsoft VB 2005: Reloaded, Advanced
The TableLayoutPanel Control (continued) Microsoft VB 2005: Reloaded, Advanced
The TableLayoutPanel Control (continued) Microsoft VB 2005: Reloaded, Advanced
The TableLayoutPanel Control (continued) Microsoft VB 2005: Reloaded, Advanced
The TabControl Control • Allows you to create a form with several related tabbed pages • Can consist of two or more TabPage controls, each of which can contain various other controls Microsoft VB 2005: Reloaded, Advanced
The TabControl Control (continued) Microsoft VB 2005: Reloaded, Advanced
The TabControl Control (continued) Microsoft VB 2005: Reloaded, Advanced
Dialog Forms • Dialog form • Modal form that can return standard results using the DialogResult property • Convenient way of getting quick standard responses • Displayed using the ShowDialog method • Modal form • Requires the user to close the form before activating another window in the application • Modeless form • Does not have this requirement Microsoft VB 2005: Reloaded, Advanced
Dialog Forms (continued) Microsoft VB 2005: Reloaded, Advanced
Menus • One of the best ways of organizing a larger number of commands is with menus Microsoft VB 2005: Reloaded, Advanced
About Menus • Menu • List of items that specify options or groups of options for an application • Menu items • Represent the individual menu commands in a menu • A menu item can be a parent menu for other submenu items • Shortcut key • Single key or a combination of keys that the user can press to execute a menu item Microsoft VB 2005: Reloaded, Advanced
Implementing Menus • Adding a menu • With an empty form displayed in the Designer • Drag a MenuStrip control from the Toolbox to any part in the form • Adding a menu item • See Figure 6.24 Microsoft VB 2005: Reloaded, Advanced
Implementing Menus (continued) Microsoft VB 2005: Reloaded, Advanced
Implementing Menus (continued) Microsoft VB 2005: Reloaded, Advanced
Implementing Menus (continued) • Adding shortcut keys • See Figure 6.26 and Figure 6.27 • Adding functionality to menu items • See Figure 6.29 Microsoft VB 2005: Reloaded, Advanced
Implementing Menus (continued) Microsoft VB 2005: Reloaded, Advanced
Implementing Menus (continued) Microsoft VB 2005: Reloaded, Advanced
Multiple-Document-Interface Windows • Single-document-interface (SDI) applications • Applications that support only one open window or document at a time • Multiple-document-interface (MDI) applications • Allow many documents to be open at once • Allow the user to easily move from document to document • The application window of an MDI program is called the parent window • Each window in application is called a child window Microsoft VB 2005: Reloaded, Advanced
Multiple-Document-Interface Windows (continued) Microsoft VB 2005: Reloaded, Advanced
Multiple-Document-Interface Windows (continued) Microsoft VB 2005: Reloaded, Advanced
Multiple-Document-Interface Windows (continued) • Example • Birthday application Microsoft VB 2005: Reloaded, Advanced
Multiple-Document-Interface Windows (continued) Microsoft VB 2005: Reloaded, Advanced
Multiple-Document-Interface Windows (continued) Microsoft VB 2005: Reloaded, Advanced
Multiple-Document-Interface Windows (continued) Microsoft VB 2005: Reloaded, Advanced
Multiple-Document-Interface Windows (continued) • Properties, methods, and events associated with MDI forms • See Table 6.1 Microsoft VB 2005: Reloaded, Advanced
Multiple-Document-Interface Windows (continued) Microsoft VB 2005: Reloaded, Advanced
Multiple-Document-Interface Windows (continued) Microsoft VB 2005: Reloaded, Advanced
Multiple-Document-Interface Windows (continued) Microsoft VB 2005: Reloaded, Advanced
Summary • A form is a graphical element that appears on the screen • A component is an object that ultimately derives much of its functionality from the Component class • When a user interacts with a control by using the mouse or the keyboard • The control raises an event within the application • When you create a GUI using the Visual Basic Form Designer • Visual Basic generates the required program code in the Code window Microsoft VB 2005: Reloaded, Advanced