1 / 23

Topic 8: Using Objects and Program Commands

Topic 8: Using Objects and Program Commands. Learning outcomes. By the end of this topic, you should be able to: 1. Apply three objects to one form; and 2. Use command objects by constructing simple programs based on text. Introduction.

deion
Download Presentation

Topic 8: Using Objects and Program Commands

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Topic 8: Using Objectsand ProgramCommands

  2. Learning outcomes By the end of this topic, you should be able to: • 1. Apply three objects to one form; and • 2. Use command objects by constructing simple programs based on text

  3. Introduction • This topic will concentrate on using several objects in one form. One object in VB6 programming is usually used to command other objects. Each object has its own properties. • This was explained in the previous topic. Now, you will be introduced to “Event”. • Make sure you concentrate fully on this topic, especially with regards to the relationship between properties and events.

  4. Using command button objects • Generally, an object without command will not be of any use to you. For example, when you place one command button object on a form, it is of no use if a program is not written on it. • As a programmer, you must know how to operate the object and also how to command the program effectively

  5. Consider the following problem. You are given the task of printing several words on a form that you have constructed by clicking on the print button. How would you tackle this problem? • Follow these steps: • (a) Click on the CommandButton on the ToolBox. • (b) Drag the mouse to the form. Drag the mouse to the button and release click. • (c) To shift position of button, click without releasing the button and drag it to a location on the form. • (d) Shift your attention to the properties windows and change the name "Command1" to "btnPrint". • (e) Change the display on the button from "Command1" to "Print". Still at the properties window, look at the Caption, delete the word Command1 and type Print. • (f) If you type in the symbol ‘&’ before the letter P, "Print" will be displayed.

  6. Once Print is displayed on the button, double click quickly and the program codes similar to the codes shown in the next Figure will be displayed.

  7. Private Sub btnPrint_Click( ) Print “Click command button” End Sub - The purpose of the print command is to allow the system to display the sentence ‘Click command button’, when the user clicks the btnPrint.

  8. Run the VB program

  9. The "pause" and "stop" buttons are not active because at this time, the program has not been run. When the "run" button is clicked, you will find that the "run" button is not active, but the "pause" and "stop" buttons have become active. • Run the program and you will find a form with a command button on it with the command "print". Click on the "Print" button several times. Notice that the words "Click command button" are displayed each time you click the command button. Look at the next Figure. To end the program, click on the "stop" button and the program will return to program mode.

  10. The following are the steps to add new command buttons: 1. Now, you will need to add a command button on the form so that you will be able to end the program. 2. Add command button, and name it "btnExit". 3. Display "Exit" on the button. Double click to return to program mode. 4. In function "btnExit_Click" type, End or Unload Me. ----------------------------------------------- Private Sub btnExit ( ) End End Sub -------------------- 5. Run the program

  11. Using label objects • Label objects are used to display text. It also has similar events as command buttons. Labels cannot modify text. Let us follow the steps below to create a label. • 1. Click on Label button in the Windows toolbox. • 2. Click and drag it onto form. • 3. A square box with ‘Label 1’ displayed will appear. The properties window will have information on Label1. • 4. Insert a name in the name space in properties. Use the name ‘lblTitle’. • 5. Type the words ‘Number Machine’ in the Caption space in the properties window. • 6. You will find that the words ‘Number Machine’ are displayed in the Label space. Modify the label size if the words cannot be seen properly. Use the mouse and click on one of the squares surrounding the label object. • 7. Run the program and you will find that the sentence is displayed on the form and when it is clicked on, no action is executed on it.

  12. Text Box Object Another important object that you need to learn about is the text box. Text boxes are usually used as a location to insert program inputs. It is also used to display text. Let us look at the steps needed to use text boxes. The following is an example of how it can be used: 1. Click on the TextBox button and drag it to the form. 2. Enter the text box name as "txtInput". Go to the space "text" in the properties window and delete the word "text1". 3. Recreate the command button "Print" and "Exit". 4. Double click quickly on the button "Print" and type "Text entered is“ &txtInput.text in "btnPrint_Click" function ------------------------------- Private Sub btnPrint_Click ( ) Print “Text entered is” & txtInput.Text End Sub

  13. 5. System will display the sentence "Text entered is" followed by text in the TextBox. • 6. The symbol ‘&’ is the connecting symbol for the two words. While "textinput.Text" refers to the text that is contained in the "txtInput" box. • 7. Click on the TextBox object to get to the properties window for the TextBox. • 8. Go to the space "TabIndex" at the properties window and make sure that the value for TabIndex for the "txtInput" TextBox properties is 0.

  14. 9. TabIndex value shows the focus position of the cursor when program is run for the first time. • 10. When program is run, the cursor will be positioned at the object that is at value 0 when the user holds down the tab key on the keyboard, the cursor will shift to the object placed at value 1. The movement of the cursor will be from 0, 1, 2 and the cycle will be repeated beginning at 0. • 11. Check that the value of the ‘Print’ and ‘Exit’ buttons. The ‘Print’ button is at TabIndex value 1 and ‘Exit’ button is at value 2. • 12. Run the program. • 13. Type in some words in the TextBox and click on the Print button

  15. Using command button, text box and labels

  16. The steps necessary to develop a program like the previous one include: 1. Create a new project with Standard EXE. Change the form name to "frmCalculator". 2. Display System Demo at the "frmCalculator" form head. Use the properties window and type it on Caption. 3. Place a label and display "Output Number" at the top of the "frmCalculator" from. Use Arial size 14 as the font. 4. Name the label as "lbloutput". 5. Place one TextBox at the bottom of the label display. Name it "txtoutput“ and leave the text value empty. 6. Place nine control buttons on the form. Name them "btnOne" until "btnNine". Display 1 on btnone, 2 on btntwo and so on. 7. Add one more control button and name it btnExit. Display the word "Exit“ on it. 8. Arrangement of the objects.

  17. Once you have completed work on the objects in the design mode, switch to the program mode. • 1. Double click on btnone and type textoutput. Text = 1 • 2. Perform the same activity on all the command buttons. • 3. Double click the btnout and in function type btnExit_Click ( ). • 4. Arrange the TabIndex as shown in next Figure

  18. 5. Run the program. • 6. Click on btnone to btnnine repeatedly. Notice that the text in the TextBox changes with each button you click.

  19. Important Tips • VB processes all lines of instruction that have been written. • VB displays error messages if an error occurs on any line of instruction. • Create a new folder to save your completed project

  20. Summary • This topic has introduced you to several important objects and examples of applying several objects on one form. • Also covered are several VB program code instructions. • It is important that you adapt yourself to the applications that you have learnt. • You should get additional text for better understanding on the concepts that are presented in this module

More Related