430 likes | 445 Views
Microsoft Visual Basic: Reloaded. Chapter Two Creating a User Interface. Classes and Objects Properties, Methods and Events Namespaces Encapsulation Planning an Application - TOE Chart Descriptive and Output Labels Suggested Naming Conventions GUI Design Tips and Other Tips
E N D
Microsoft Visual Basic: Reloaded Chapter Two Creating a User Interface
Classes and Objects Properties, Methods and Events Namespaces Encapsulation Planning an Application - TOE Chart Descriptive and Output Labels Suggested Naming Conventions GUI Design Tips and Other Tips Access Keys, Tab Order, Splash Screens and Dialog Boxes Overview
In Object Oriented Programming, a class is analogous to a blueprint for an object. It can be used to create many objects again and again. Each time you add a control to a form (like a button or text box), you create instances of the class represented by those controls. Those controls are considered objects. Classes and Objects
Each object has specific Properties, Methods and Events Properties: Attributes that describe the object Events: Characterizes how an object can respond Methods: Defines actions an object can take Function – returns a result when called Sub – does not return a result when called Properties, Methods and Events
Properties Balloon.Color = Red Balloon.Diameter = 10 Balloon.Inflated = True Methods Balloon.Inflate Balloon.Deflate Balloon.Rise(5) Object Example
Event Sub Balloon_Puncture() Balloon.MakeNoise("Bang") Balloon.Deflate Balloon.Inflated = False End Sub Object Example
Buttons Text Box Labels Radio Buttons Check Box Combo Box List Box Tool Tip Numeric Up Down Common Form Controls (Objects)
BackColor Enabled ForeColor Location Name Size (Height, Width) Text Visible Can “manually” set these for now, but we’ll see how to manipulate the properties with code soon. Common Control Properties
Public Function Add( ByVal var1 as Integer, ByVal var2 as Integer) As Integer Dim result as Integer result=var1 + var2 Return result End Function Methods – Example of a Function
Public Sub MySub() MessageBox.Show(“ this is a non value returning method ”) End Sub Methods – Example of a Sub
Button: Click Text Box: TextChanged Radio Button: CheckedChanged Check Box: CheckedChanged Combo Box: SelectedIndexChanged Double click on the control in design view to automatically open the code window with the default event for that control. Default Control Events
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click End Sub Default Event for Button
Enter TextChanged KeyDown MouseEnter MouseLeave MouseHover And many more (68 in all) which can be explored in the Object Browser, Properties Window or Code Window Other Command Button Events
The .NET Framework Class library stores pre-developed classes to facilitate program development. The classes are organized into directory-like structures called Namespaces. Namespaces 15 Name of object(Welcome) Namespace (System.Windows.Forms) Class type of object (Form) Welcome application GUI objects Figure 3.43| Component object box expanded to showthe Welcome application’s objects. Figure 3.44|The name and class of an object are displayed in the component object box.
System: the root for primitive data types and other namespaces in the .NET base class library System.Data.SQLClient: this namespace contains classes that are optimized for interacting with MS SQL Server System.Windows.Forms: this namespace contains types involved in creating standard Windows applications. Classes that represent forms and controls reside here as well. Namespace Examples 16
The “My” namespace stores the most commonclasses from the .NET framework in one collection. This namespace allows a programmer to access the .NET framework classes and parts of a project. Example using the My namespace reference: My.Computer.Audio.Play("c:\windows\media\Windows XP Startup.wav") My Namespace 17
Displaying a splash screen Getting the computer name Getting network settings Verifying a web site is up and running Reading a text file into a string Sending something to print on the default printer Getting application settings Common Tasks using My Namespace 18
You don’t need to know HOW something works in detail, all you need to know is how to call it in code. The 1 line of code used to create the basic web browser is a great example of Encapsulation. myBrowser.Navigate(txtURL.Text) Every control added to a form is automatically created by Visual Studio without having to instantiate the classes in code. Easy! Encapsulation
Planning an Application • Plan the application before creating the user interface • Work jointly with the user to ensure the success of the application • TOE (Task, Object, Event) chart: • Shows application’s tasks, objects, and events • Tasks, objects, and events should be identified in the first three steps of planning • Sunshine Cellular Company: • Takes orders by phone for cell phones priced at $100 each • Two colors: blue and silver • Currently the salespeople calculate the order total
Planning an Application Figure 2-2: Current order form used by Sunshine Cellular
Planning an Application Figure 2-6: Completed TOE chart ordered by object
Labels • As the control’s name indicates, Labels are often used to identify other controls on the Form. • Descriptive Labels help the user understand each control’s purpose. • Output Labels display program output.
Descriptive and Output Labels Output Label (recessed appearance) DescriptiveLabel
Suggested Naming Conventions • Change the Form name to a unique and meaningful name for easy identification. • Capitalize the first letter of the Form name because Form is a class. • Objects (such as controls) should begin with lowercase letters. • Change the Form’s title to help users identify the Form’s purpose. • Form titles should use book-title capitalization.
Suggested Naming Conventions • Use standard suffixes for names of objects (controls and Forms) so that you can easily tell them apart. • Append the suffix Form to Form names. • Append the Label suffix to all Label control names. • Append the Button suffix to Button control names. • When entering values for aLabel’s Text property, you should use sentence-style capitalization.
Choose short, descriptive Form titles. Capitalize words that are not articles, prepositions or conjunctions. Do not use punctuation. Use 9pt Segoe UI font to improve readability for controls that display text. Use colors in your applications, but not to the point of distracting the user. Use Labels to display text that users cannot change Ensure that all Label controls are large enough to display their text. GUI Design Tips
GUI Design Tips • Make TextBoxes wide enough for their expected inputs. • Each TextBox should have a descriptive Label indicating the input expected from the user. • Place each descriptive Label either above or to the left of the control that it identifies. • A descriptive Label and control it identifies should be aligned on the left if they vertically arranged. • The TextAlign property of a descriptive Label should be set to MiddleLeft. This ensures that text within groups of Labels align.
GUI Design Tips • Align the left or right sides of a group of descriptive Labels if the Labels are arranged vertically. • If several output Labels are arranged vertically to display numbers used in a mathematical calculation (such as in an invoice), set the TextAlign property to MiddleRight. • Output Labels should be distinguishable from descriptive Labels. This can be done by setting the BorderStyle property of an output Label to Fixed3D.
Snap Lines Format Menu Options Lock Controls Align Sizing Spacing Centering Manually Resize Labels Change the Form file name (Form1.vb) to a name that describes the application’s purpose. Other Helpful Features and Tips
Clear the an output Label’s value initially or provide a default value. When the application performs the calculation for that value, the Label’s Text property should be updated to the new value. You’ll learn how to do this in the next chapter. To save time, Copy and Paste new controls that are similar to existing controls. They will automatically contain the same properties as the existing controls. Other Helpful Tips
Assigning Access Keys • Access key allows user to select an object using Alt + access key. • Assign access keys to each control that can accept user input (exceptions: OK and Cancel buttons) • Each access key should be unique • Include & in front of the character to be used as the access key: • &Calculate Order Calculate Order
Controlling the Tab Order • TabIndex property determines the order in which a control receives the focus when the Tab key is pressed • Starts at 0 • Assigned by default as the order in which controls are added to the form at design time • Should be set to the order in which the user will want to access the controls • Focus: the state of being able to accept user input • Set TabIndex using the Properties window or the Tab Order option on the View menu
Splash Screens • Splash screen: appears when an application is started • Used to introduce the application • Used to hold the user’s attention while the program is being loaded into memory
Splash Screens (cont’d.) Figure 2-13: How to add a new splash screen to a project
Splash Screens (cont'd.) Figure 2-16: How to specify the splash screen
Splash Screens (cont'd.) Figure 2-17: Project Designer window
Dialog Boxes • Use tools in the Dialogs section of the toolbox for commonly used dialog boxes: • Color, Font, Save As • These controls do not appear on the form • Component tray stores controls that do not appear in the user interface during run time • Dialog boxes are modal • They remain on the screen until the user closes the dialog box • No input from keyboard or mouse can occur in the primary window until the dialog boxes is closed • Write code to display the dialog box and to use values selected in the dialog box by the user
Dialog Boxes (cont'd.) Figure 2-21: Primary window and Font dialog box in Notepad
Dialog Boxes (cont’d.) • Default button: activated when user presses Enter key when the button does not have the focus • Cancelbutton: activated when user presses Esc key when the button does not have the focus • AcceptButton property • A form property that designates the name of the default button • Only one per form • Should be the button that is most often selected by the user (unless the tasks performed by this button are both destructive and irreversible)