Lecture Set 3 Introduction to Visual Basic Concepts
Lecture Set 3 Introduction to Visual Basic Concepts Part B – Simple Controls, Event Handlers and Windows Forms Applications 9/26/2020 7: 56 AM
Objectives n n n Slide 2 To gain an introduction to some simple controls To understand more about the properties, methods, and events associated with controls To gain an introduction to designing forms and writing the code behind to “make things happen” 9/26/2020 7: 56 AM
Understanding the Name Property n Every form and control instance has a name n n n Visual Studio assigns a default name to forms and control instances – assign your own names The value of the Name property is used to reference a form or control instance programmatically Assigning meaningful names creates more readable code n n Use consistent prefixes form and control instance names To get a list of controls on your forms – Click on n n Slide 3 View Other Windows Document Outline Better yet – draw your own form and name controls 9/26/2020 7: 56 AM
Requirements for the Name property n n n Slide 4 The first character must be a letter Subsequent characters can be letters, numbers, or the underscore character Name must be less than 255 characters in length Names cannot contain spaces Names cannot contain special characters Form names and control instance names must be unique 9/26/2020 7: 56 AM
Some Control Prefixes Control Prefix Button btn Label lbl Open. File. Dialog ofd Picture. Box pic Tool. Tip tip The text book has many more illustrations. Slide 5 9/26/2020 7: 56 AM
Properties of the Control Class n n Controls are instances of classes These are properties common to all controls and therefore inherited by all subclasses (individual control classes such as buttons, labels, text boxes, picture boxes etc) of the controls class n n n Slide 6 The text and background colors are specified by the Fore. Color and Back. Color properties The Enabled property defines whether the control instance responds to end user interaction Height and Width properties specify the size Location property defines the position Text property contains the text appearing in the control instance Visible property defines whether the control instance is visible or invisible 9/26/2020 7: 56 AM
Methods and Events of the Control Class n The Hide method makes a control instance invisible n n The Show method makes a control instance visible n n Slide 7 The Visible property is set to False The Visible property is set to True The Click event fires when the end user clicks the control instance 9/26/2020 7: 56 AM
The Label Control n The Label control class derives directly from the Control class n n n A label is used to display textual information This control inherits all the properties of the Control class, and it is a few unique properties of its own n Slide 8 Recall – all classes are descendants of the object class Some of these properties are properties of some other control subclasses as well 9/26/2020 7: 56 AM
Properties of the Label Control n n Slide 9 The Border. Style property defines the appearance of the border surrounding the label The Font property defines the font of the text appearing in the table The Alignment property defines the alignment of the text within the visible region of the control instance Lots of other properties – do you know what some of them are? 9/26/2020 7: 57 AM
Some Label Properties Slide 10 9/26/2020 7: 57 AM
The Button Control n n n The Button control is derived from the Button. Base class It supplies a clickable button The Check. Box and Radio. Button controls also supply clickable buttons n Slide 11 The Check. Box and Radio. Button controls are discussed in subsequent chapters 9/26/2020 7: 57 AM
Some Button Properties Slide 12 9/26/2020 7: 57 AM
The Button Control (Members) n n Slide 13 The Enabled property defines whether the control instance will respond to events The Flat. Style property defines the visual appearance of the button The Text property defines the text appearing in the visible region The Click event fires when the end user clicks the button 9/26/2020 7: 57 AM
The Picture. Box Control n n n Slide 14 The Picture. Box control derives directly from the Control class It displays images having formats of JPEG, bitmap, and others The image can be scaled inside the control instance 9/26/2020 7: 57 AM
The Picture. Box Control (Members) n n n Slide 15 The Border. Style property defines the appearance of the border surrounding the Picture. Box The Image property contains the image The Image. Location property contains the disk file name where the image is stored The Size. Mode property defines how the image appears (is scaled) inside of the Picture. Box The Click event fires when the end user clicks the Picture. Box 9/26/2020 7: 57 AM
Using the Size. Mode Property to Scale Images Slide 16 9/26/2020 7: 57 AM
The Open. File. Dialog Control n The Common. Dialog class is the base class for other classes n n n Slide 17 These classes are used to display standard dialog boxes The Open. File. Dialog control allows the end user to select a file to open n n (Optional) Not a database, but a file such as a linear or sequential file The control instance appears in a resizable tray below the Windows Forms Designer at design time Call the Show. Dialog method to display the control instance 9/26/2020 7: 57 AM
Open. Dialog Illustration (VB) Slide 18 (Optional) 9/26/2020 7: 57 AM
Using the Open. File. Dialog Control n n The Show. Dialog method displays the dialog box to the end user The File. Name property contains the file name selected by the end user n n The Open. File. Dialog control does not actually open a file You need to do this n n Slide 19 (Optional) You can find the Open. File. Dialog control in the dialogs section of your toolbox – just click twice on the Toolbox icon to insert the box into your form Click twice on the inserted box to get the control event handler 9/26/2020 7: 57 AM
The Open. File. Dialog Control n (Example-Optional) Display an instance of the Open. File. Dialog control named ofd. Images, and print the filename selected by the end user ofd. Images. Show. Dialog() System. Console. Write. Line(ofd. Images. File. Name) n Slide 20 Here is another example 9/26/2020 7: 57 AM
The Open. File. Dialog Control (VB) (Optional) (VB Example - See if you can convert this to C# -- if you need help, ask!!) 9/26/2020 7: 57 AM Slide 21
The Open. File. Dialog Control (VB Example) (Optional) 9/26/2020 7: 57 AM Slide 22
Invisible Control Instance Slide 23 (optional) 9/26/2020 7: 58 AM
Tool. Tips (Introduction) n Tool. Tips appear as pop-up windows when the mouse hovers over a control instance n n Create Tool. Tips with the Tool. Tip control n n Slide 24 Use to display informational messages to the end user The Tool. Tip control is a provider control, meaning that it works with other control instances Use one Tool. Tip control instance for all the other control instances on a form 9/26/2020 7: 58 AM
Tool. Tip Control (Members) n n n Slide 25 Initial. Delay property controls the amount of time the mouse must hover before displaying the Tool. Tip Automatic. Delay property controls the length of time the Tool. Tip appears Use to Tool. Tip. On. Control. Instance. Name to display the Tool. Tip 9/26/2020 7: 58 AM
Creating Code for a Windows Application n Code executes as the end user interacts with the form’s control instances n n The code behind the form is made up of two parts n n Slide 26 This code is called the code behind the form One part is automatically generated by the Windows Forms Designer The second part contains the code you write 9/26/2020 7: 58 AM
Introduction to Class Blocks n A form is made up of two files n The Windows Forms Designer generated code appears in the file named frm. Name. Designer. vb n n n It is good to be able to read some of this code, but we will focus only on some portions of this code and not others Developer created code appears in the file frm. Name. vb A form is a class having the following structure public partial class frm. Splash. Start : Form { // form block of statements } // end Class frm. Splash. Start Slide 27 9/26/2020 7: 58 AM
A Class n n // Current File Class // Data stores and method related to the Current File processed by the project n // Frank Friedman // CIS 3309 n // Adapted from VB Version 1 - Feb 6 2010 by FLF July 18, 2013 n using System; using System. Collections. Generic; . . . using System. Windows. Forms; n n n n n Slide 28 (Example) namespace ATM_Simulation_Project_CSharp { public class current. File. Class { private string current. File. Path; private System. IO. Stream. Reader current. File. SR; private int record. Read. Count; // Constructor with file path input // Create instance of Stream. Reader class (type) and store reference public current. File. Class (string file. Path). . . } // end current. File. Class } // end namespace 9/26/2020 7: 58 AM
The Syntax of a Class Block n All classes belong to a Namespace n n Classes have a header and are delimited with braces { } A C# Class block contains: n n n A class constructor is the entry point n Slide 29 Methods (written as functions) Attributes (class variables – declared outside any method) The constructor’s header contains only the words public and the name of the class itself 9/26/2020 7: 58 AM
A (Partial) Class Associated with a Form If a (partial) class is associated with a form your code behind goes in the form’s. cs file The associated (generated) partial class can be found in the forms …designer. cs file See next three slides Slide 30 9/26/2020 7: 58 AM
Your Partial Class for a Form Slide 31 9/26/2020 7: 58 AM
Windows Forms Designer Generated Code Slide 32 9/26/2020 7: 58 AM
More Generated Code n n n The next slide contains code that is generated for controls added to a Form Object instantiation code appears first Definitions of default attributes (properties) appear next – but only shown for n n Slide 33 lbl. Welcome lbl. To. The 9/26/2020 7: 58 AM
Forms Designer Generated Code - 2 Slide 34 9/26/2020 7: 58 AM
Introduction to Event Handlers - 1 n n n We are now operating in an event driven programming environment. Compare to a standard C (and Java in 1068 and 2168) An event hander is a procedure containing statements that execute when the end user performs an action. Activity happens when an event is fired. n n n Slide 35 Windows executes different event handlers as different events fire Windows fires a Click event when the end user clicks a button Different types of controls support different events 9/26/2020 7: 58 AM
Executing an Event Handler Slide 36 9/26/2020 7: 58 AM
Characteristics of an Event Handler n An event handler is a void function n All event functions have two arguments n n n Arguments send information to a procedure More on these in a later slide set The Handles clause (found in the Designer. cs code) wires the function to the event this. btn. Find. Me. Click += new System. Event. Handler(this. btn. Find. Me_Click); n Slide 37 Above wiring code generated automatically in the Windows Forms Designer 9/26/2020 7: 59 AM
Event Handlers - 2 n The handler itself can be generated by doubleclicking the control instance in the Windows Forms Designer. n n n Slide 38 You get the default event wired to the control To see a list of additional handlers that can be wired to an event click on the lightening bolt icon in the Properties window for the control at hand You can create custom handlers for events (See textbook, pp. 174 -175) 9/26/2020 7: 59 AM
Event Handlers - 3 Slide 39 9/26/2020 7: 59 AM
List of Events for a Button Control Slide 40 9/26/2020 7: 59 AM
- Slides: 40