Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic Objects Controls

Similar presentations


Presentation on theme: "Visual Basic Objects Controls"— Presentation transcript:

1 Visual Basic Objects Controls
Chapter 3.1 & 3.2 12/2/2018 Prepared by: Deborah Becker

2 Prepared by: Deborah Becker
Managing Controls Common Control Properties The most common tools in the tool box Accelerator Keys and Focus Event procedures 12/2/2018 Prepared by: Deborah Becker

3 Prepared by: Deborah Becker
Studying Controls Types of controls Intrinsic Controls ActiveX Controls Insertable Controls Intrinsic controls are those controls that appear in the tool box when you first fire Visual Basic 6 from the program menu. ActiveX Controls contain a file extension of (.OCX) and are external controls that you can add to the tool box window. *Remember that there are no scroll bars on the tool box window. The tools available will extend the tool box beyond the open screen window, and only those tools that you can see are available. Hidden tools are not accessible. Insertable Controls are controls made from external applications like Microsoft Excel and Word. We access ActiveX controls and insertable controls by clicking ProjectsComponentsInsertable from the main menu During lectures and discussions I will from time to time give you the access thread to controls and VB features by indicating the Menu and submenu layers in the same manner, Menu optionsubmenu optionTab or component. These access threads make good test questions. 12/2/2018 Prepared by: Deborah Becker

4 Prepared by: Deborah Becker
Intrinsic Controls Intrinsic controls are the most common controls and the ones that we will use during this class. We will only discuss the first 10 controls during class lecture but you are welcome to experiment with the other controls during the coding of your program assignments. Shapes, Line and Image controls make the appearance of User interfaces more professional looking. 12/2/2018 Prepared by: Deborah Becker

5 Prepared by: Deborah Becker
Common Properties Caption & (name) Enable Left, Top, Height & Width Alignment BackColor & ForeColor BorderStyle Font ToolTipText & MousePointer Alignment determines position of text in labels and on control boxes options are Left and right justified, and centered. These effect the appearance of the caption and text content of labels. BackColor and ForeColor change the appears of controls. The Down-Arrow that appears when you click on this property tell you that there are several options. Clicking the arrow brings up two tab folders containing the palette of colors and the system defaults that are available. Both of these properties work primarily the same. 12/2/2018 Prepared by: Deborah Becker

6 Prepared by: Deborah Becker
Caption, Name, Enable Caption—is used to display text on the control Name—specifies the name of the control and is used to address the control when altering properties during run time. Enable—determines the action of the control Caption and name Caption list the text displayed on the control. Name specifies the name of the control and will be used when your write basic code procedures that affect the appearance of your forms. In the alphabetical listing name appear in parenthesis so that it will appear first in the property box. Enable can be set to either true of false using the drop down menu. Setting enable to false means that when the user click on the control it will not respond. True means that it will respond to user clicks. This feature may mean more to you when we actually begin generating user applications. 12/2/2018 Prepared by: Deborah Becker

7 Prepared by: Deborah Becker
Left, Top, Height & Width Measured in Twips (1440 per inch) Left—indicates the starting Twip from the left edge of the form or window. Top—indicates the starting twip from the top of the form or window. Left, Top, Height & Width A twip is the measurement used to indicate the position of a form on the screen or a control on a form. --Two measurement areas on the right side of the tool bar, the first is activated when you click on a control during design phase, and the second indicates the location of the form within the window. Left—for a control indicated the starting twip from the left edge of the form for the form it indicated the number of twips from the edge of the screen Top—for a control indicates the starting twip from the top of the form for the form itself it indicated its twip position from the top of the screen. --The left number indicated the position from the left edge and the right number tell you your position from the top edge. Both position indicators work the same for the control and the form. Height and Width are measurement for controls. The indicate the size of the control. 12/2/2018 Prepared by: Deborah Becker

8 Alignment, BackColor, ForeColor, BorderStyle
Alignment– determines the orientation of the text displayed in textboxes, and captions BackColor—sets the color behind the text on the control ForeColor—sets the color of the text on the control (Caption) BorderStyle—can be set to make labels and command buttons appear 3D. To set the color on a command button you must also set the Style Property to Graphical. 12/2/2018 Prepared by: Deborah Becker

9 Font, TooltipText & MousePointer
Font—is much like other window products; it sets the font style, size and other properties. ToolTipText—is used for additional instructions MousePointer—change the appearance of the mouse as it passes over the control Font is the same as other Microsoft products. It allows you to set the size and appearance of text either on the control or in text boxes on the form. Tool Tip Text and Mouse pointer both activate when the mouse passes over the control. By typing in instruction in the tool tip box you are able to give users help and instructions with out cluttering up the form or control with text. 12/2/2018 Prepared by: Deborah Becker

10 Prepared by: Deborah Becker
Default Properties Default values are assigned, as controls are inserted into the form Most objects have key properties Some properties can only be set with Basic code Mouse pointer determines the shape of the mouse pointer when it passes over the control at run time Remember that properties have defaults values assigned when you place a control on your form. You will not want to change all properties and may never used some of the properties, but some Properties will be used extensively when coding your applications. Properties can also be modified with visual basic commands during coding. We use let statement or assign statements to change the properties of control during run time. We saw some of these during our example form in our lecture from Chapter 1. We can set the size and placement of the form in the coding window by placing the following statement in the Form_Load event in the coding window. frmMyfrist.Left = (Screen.Width - frmMyfrist.Width) / 2 frmMyfrist.Top = (Screen.Width - frmMyfrist.Height) / 2 Some properties must be set with visual basic code The name property may not be set with a let statement and must be set during design time through the property window. 12/2/2018 Prepared by: Deborah Becker

11 Prepared by: Deborah Becker
Form Key Properties BorderStyle decides whether you will allow the user to resize the form ContolBox Can have a value of true or False and controls whether you Will allow the user to have Access to the Control Menu. BorderStyle—determines whether you will let the user resize your form at run time. Setting this setting to fixed means that the window can not be made bigger by pressing the maximize button on the control box. ControlBox– is the menu that appears when you click a window’s icon in the upper-left corner of the window. If you set this property to false then you the programmer control the entering and exit of the program. Other property features that might be useful are MaxButton and MinButton – determines whether the form will appear with the _ and Maximum buttons in the righthand upper corner of the form. Moveable – determines whether the user can move the form around on the window or if the form is stationary ShowInTaskbar – determines if the program will appear in the taskbar StartUpPosition – determines where the form appears on the screen when the application is fired. The most common use of this is 2-CetersScreen which places the form in the center of the screen. WindowState – indicated whether the form is maximized or minimized when the program is launched. 12/2/2018 Prepared by: Deborah Becker

12 Setting the Forms Location
Drag the image of the form in the Form Layout Window Change the Form’s StartupPosition property Or we can use the example on the board to change the forms startupPosition Another way to change the location of the from is to change the StartupPosition property in the property window. There are always at least three ways to accomplish every task. During lectures I will try to point out alternate ways of accomplishing tasks during lecture. 12/2/2018 Prepared by: Deborah Becker

13 Prepared by: Deborah Becker
Labels Mrs. Becker Used to Display text on the screen User Prompts Key Properties AutoSize – if set to true, the label expands across the form to display all the word typed into the caption property WordWrap – If set to true, VB keeps the width the same but expands the label vertically to accommodate all the text typed in the caption. For wordwrap to work properly you must also set AutoSize to true. Focus is indicated in several ways and shows the user where the mouse cursor is active in the screen at any given time. Labels do not normally have the cursor focus. They may be used as input objects and then they will be able to have the focus. The cursor appears as a Line in the Textbox object. The cursor appears as an outline on a command button. Other object may appear darker or sunken when they have the cursor focus. 12/2/2018 Prepared by: Deborah Becker

14 Prepared by: Deborah Becker
Text Box Used when you want users to type information into your form. Programmers often set a default value Properties most often used Alignment, Locked, MaxLength, Multiline, PasswordChar, and ScrollBars Alignment – used to control how data is display in the text box. Alphabetic data is usually left justified, while numeric data is normally right justified. Locked – determines whether the control can be edited during runtime. MaxLength – returns/set the maximum number of characters the the user can enter into the control. returns means that I can use an assign statement to display the value of this property at run time. Multiline – returns/set the value that determines if the control can accept multiple lines of text from the user. PasswordChar – will cause the program to display the password character as the user enters text instead of the text entered. This is most commonly an asterisk. Scrollbars – usually set to true when multiline us set to true to allow the user to scroll up or down, or from side to side in the text entered into the textbox control You can always find out what effect a control property will have by placing the mouse at that property in the property window. An explanation of the property will appear below the property box window. 12/2/2018 Prepared by: Deborah Becker

15 Command Buttons Key Properties
Cancel—can be set to enable the KEY to activate the control Caption—used to type words on the control The underlined X is entered by placing a & before the X in the caption It means that the control can be activate by pressing “Alt-X”. We call this an accelerator key ESC Cancel can be set to True for only one control per form Acceleration keys can be set by placing an ampersand character before the letter you wish to use as an acceleration key. These allow the user quicker access to the control without using the mouse. Default – if set to true the user can trigger the command button by pressing ENTER 1—ONLY ONE COMMAND BUTTON DEFAULT CAN BE SET TO TRUE. IF YOU SET MORE THEN ONE COMMAND BUTTON’S DEFAULT PROPERTY TO TRUE, vb RESETS ALL BUT THE LAST ONE TO FALSE. Picture—you can place a picture on the command button using this property. Style—determines if the command button display the text caption or the picture If set to Zero it displays text If its set to 1 it displays the picture. Remember to set both properties if you intend to use a picture on a command button. Command buttons are used to call other forms, and execute specific procedures within the VB program. Exit Program 12/2/2018 Prepared by: Deborah Becker

16 Prepared by: Deborah Becker
Default, Picture, Style Default can be set to True for only one control per form Picture– you can place an icon on the command button through the picture property. Style You must also set the style property to Graphical in order to use a picture or change the BackColor property Cancel—allows one command button to set to true so that the user can press the cancel button the the keyboard and exit the program. Default– determines which command button has the cursor when the program first runs. Accelerator keys make the program more user friendly 12/2/2018 Prepared by: Deborah Becker

17 Prepared by: Deborah Becker
Frames  A container for other controls Used for option buttons or check boxes Default name: Frame1, Frame2 Naming Convention Prefix: fra, e.g.,  fraMeaningfulName Examples: fraColor, fraStyle Key Property: Caption property—default is default name (appears on top left border of frame) Frames appear as a box on a form. They divide the plane (surface area) of the form into separate areas. You place other objects into a frame you may not drag objects already placed on a form into a frame you must create the object inside the frame. Setting the caption property for a frame allows you to display a meaningful purpose for the frame to the user. 12/2/2018 Prepared by: Deborah Becker

18 Prepared by: Deborah Becker
Image Control Holds a picture (bmp, wmf, ico, gif, jpg, etc.) Default name: Image1, Image2 Naming Convention: imgMeaningfulName Examples: imgBigIcon, imgSmallIcon Key Properties for Image control Picture property – use Load Pictures into the control through the dialog box. Stretch property – default: False - Change to True to make the picture enlarge to fill the control Visible property default: True - False hides the image at run time A Picture control is often used to display output to the screen. We will use picture boxes to display the results of our programs to the screen. This save the forest for future generations and allows for more efficient debugging sessions. The image object is used most often to display an Icon, Logo or picture to the screen It contains a stretch property that allows the image to fill the entire control. The picture control does not have a stretch property, you must determine the size of the picture box control on the screen by setting the Height and Width properties or manually stretching the control to the desired size during design time. 12/2/2018 Prepared by: Deborah Becker

19 Prepared by: Deborah Becker
Picture Control Holds a picture (bmp, wmf, ico, gif, jpg, etc.) Default name: Picture1, Picture2 Naming Convention: picMeaningfulName Examples: picResults, picDisplay Key Properties for Picture control Picture property – use Load Pictures into the control through the dialog box. Visible property default: True - False hides the image at run time Use in class to display output from our programs 12/2/2018 Prepared by: Deborah Becker

20 Prepared by: Deborah Becker
OptionButton We use OptionButtons to give users the opportunity to select an option from a bank of options. Users can choose only one optionbutton in the bank Usually placed in a Frame control Default properties Value—default is false, when true the box appears with a period mark inside at run time. Caption—appears to the right of the control, if not changed hold the name of the control Enable—default is true, if set to false the checkbox appears as grayed when the program runs. 12/2/2018 Prepared by: Deborah Becker

21 Prepared by: Deborah Becker
CheckBox We use CheckBoxes to give users the opportunity to select items from the screen. Users can choose one or all in a check box series Usually placed in a Frame control Default properties Value—default is false, when true the box appears with a check mark inside at run time. Caption—appears to the right of the control, if not changed hold the name of the control Enable—default is true, if set to false the checkbox appears as grayed when the program runs. 12/2/2018 Prepared by: Deborah Becker

22 Prepared by: Deborah Becker
Other Controls Shape—Circle, box, rectangle, triangle Places a shape on the form Line Allow lines to be added on the form 12/2/2018 Prepared by: Deborah Becker

23 Prepared by: Deborah Becker
Events Events are caused in several ways Clicking on the control to shift the focus Tab Order—Pressing the tab key to shift the focus to the next control Setting a Default Property to true for one control on the form Setting a control Cancel property to true Tab order is set by Setting the TabStop property to true and Assigning the TabIndex property as(0,1,2,) When you set the tab stop property to true the cursor will stop there when the form is running. Setting this property to false means that the cursor will skip the control when the user presses the tab key. The tabIndex determines the order of the cursor when the tab key is pressed at run time. 12/2/2018 Prepared by: Deborah Becker

24 Prepared by: Deborah Becker
Default Events Textbox – Change() Command Button – Click() Option Button – Click() CheckBox – Click() 12/2/2018 Prepared by: Deborah Becker

25 Prepared by: Deborah Becker
Labels & Frames Normally Labels do not have event procedures coded for them. We change the value of labels during coding and at run time in other object events. Frame are another object that usually does not have coded specific events. We expect the objects inside the frame to have the events procedures. 12/2/2018 Prepared by: Deborah Becker

26 Prepared by: Deborah Becker
Focus Object Focus - the placement of the cursor Only one control can have the focus at a time The focus can be identified in several ways Command buttons have a series of dashes around the perimeter Textbox will have a cursor line either in the left corner or right corner depending on the alignment property. 12/2/2018 Prepared by: Deborah Becker

27 Prepared by: Deborah Becker
Visual Basic Code Remember the VB Programming steps Create the Interface Set the Properties Write the code Code consists of the steps that carry out and complete our tasks. These can be assign statement, or other commands txtName = “David Copperfield” lblCompanyName = “Very Very Boards” End The code in the VB program is placed into sub procedures and functions. This allows is to structure our programs and make them more maintainable. Each sub procedure begins with a Private Sub statement which contains the Object name and Event name and ends the a set of parentheses. The ending statement to any Private sub is the End Sub. Every sub procedure must have and End Sub statement. Private Sub ObjectName_EventName() coding lines End Sub Events can be created in the coding window by double clicking the object on the form at design time, or opening the coding window and writing the code into the coding form. You may also create a Procedure by Clicking on Tools from the Main Menu and selecting Add Procedure ToolsAdd Procedure. Here you give the procedure a name and tell the editor whether it will be a sub procedure, function, event or property. These other options will be covered latter. In the coding window are two drop down list boxes. The one on the left contains the list of Objects the are on the Form, The one on the Right contains a list of the Procedure and events. They are called the Object Box and the Procedure Box. By click the Procedure box you can see a list of possible events for the object on the form. The lost focus event is an event we will use in this class. This event executes when the cursors attention leaves the objects. We can use this event to reset properties when the cursor goes to the next object on the form. Example Private Sub lblName_LostFocus() lblName.Forecolor = vbRed lblName.font.size = 16 12/2/2018 Prepared by: Deborah Becker

28 Coding for the Controls
You can clear TextBoxes and Labels txtName.Text = “” lblName.Caption = “” You can Resetting the Focus to a specific object using a method txtName.SetFocus SetFocus is the method textName.setfocus is a VB statement the components of this statement are ObjectName separated by a Period and then the Method Name. Remember a method is like a verb it tell the object to perform some action. Here we are telling the Textbox to capture the cursor focus. 12/2/2018 Prepared by: Deborah Becker

29 Prepared by: Deborah Becker
Coding for Controls You can change the Font property with code txtName.Font.Bold = true lblMessage.Font.Size = 12 Sub Properties are separated by periods, may also appear without the period between the property and sub property. You can change the Color of Text txtName.ForeColor = vbRed cmdExit.BackColor = vbGreen Color Constants vbBlack, vbGreen, vbRed The font property has sub properties that can be set. This is indicated by the  to the left of the property. You may separate sub properties with a period or simply indicate the name of the sub property. Like textName.FontSize = 12 Good programming form is to separate the components of the property but you will see this both ways in industry code. 12/2/2018 Prepared by: Deborah Becker

30 Prepared by: Deborah Becker
Coding for Controls You can make objects Appear and disappear with code lblCompanyName.Visible = False lblCompanyName.Visible = optCompanyName.Value 12/2/2018 Prepared by: Deborah Becker

31 Changing Multiple Properties
With…End With statements With lblCompanyName .Caption = “” .ForeColor = vbBlack .Font.Bold = True .Setfocus End With Concatenating Text lblName.Caption = “Your Name: “&txtName The with block is a powerful VB multistatement block. It begins with the Width key word and is followed by the object name. Inside the block properties and methods are indicated by placing a period and then the property name and the assignment or the method. Methods do not have assignments they are verbs or commands. You may want to enter text and control names together in a label for the user this is done through concatenation the components with the &, The & has two function in VB, indication of accelerator keys and Concatenation 12/2/2018 Prepared by: Deborah Becker

32 Prepared by: Deborah Becker
The Property Assumed TextBox: Text Label: Caption Command button: Caption Frame: Caption The assumed property need not be stated Default properties are assumed during coding and may be left out of assign statements. Text1 = “Todays Date” Label1 = “My Name” 12/2/2018 Prepared by: Deborah Becker

33 Object Event Procedures
Every Object placed on the form has a default event. The Command button default event is the Click() event. Private Sub cmdExit_Click() ‘End is the VB Command End End Sub Every program that you write for this class will contain a exit routine. All user interfaces must contain and exit button and a cmdExit_Click() event procedure. The programmer should always the control the execution of his/her program. This includes the exit routine. The default event for the textbox control is the _Change() event Private Sub txtName_Change() txtName = “David CopperField” End Sub Inside the event procedures we place code to change the value of properties at run time. They may also contain Method statements, decision and blocking structure. These will be covered in later chapters. Events can either be Private or Public. Private events can only be executed in the form in which they reside. Public events may be executed by other forms in the Project by indicating the form name and the event Name. 12/2/2018 Prepared by: Deborah Becker

34 Prepared by: Deborah Becker
Homework Answer the Practice problems 1 thru 6 page Do Exercises 30 and 32 page 57, turn in the User interface, Form as text and the property sheet from the print option in the VB editor. Do exercises 1 thru 6 page 67 Do exercises 12, 14, 16 page 67 12/2/2018 Prepared by: Deborah Becker

35 Prepared by: Deborah Becker
Good Bye That’s all Folks!!!! 12/2/2018 Prepared by: Deborah Becker


Download ppt "Visual Basic Objects Controls"

Similar presentations


Ads by Google