Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating and executing a program

Similar presentations


Presentation on theme: "Creating and executing a program"— Presentation transcript:

1 Creating and executing a program
Please use speaker notes for additional information! This presentation will create and execute a simple program.

2 Start Visual Basic and select Standard EXE.

3 The Visual Basic screen comes up
The Visual Basic screen comes up. The programmer can now layout the working environment in a workable way. I like to have project in the top right corner with properties underneath it and tools across the bottom. This is totally programmer choice. This leaves me room in the middle for the form.

4 I next go to name and change the name of the form
I next go to name and change the name of the form. The frm at the beginning is considered the naming convention for naming a form to distinguish it from other objects. Next, I will rename the project. The order does not matter, I could have renamed the project first.

5 To change the project name, I first click on it in the Project box and that brings up the properties for the Project. I can then go in and change the name. I changed it to PrDemo.

6 I am now going to save my form and my project
I am now going to save my form and my project. I save the components of the project first - in this case the form. Then I save the project. It is best to save each project in a separate folder or directory to keep all of the files that make up the project together. I am going to click on File/Save frmDemo As... Then I will create a directory or folder to save the frmDemo in.

7 I have created the directory or folder called Demo and now I am saving the form file in this directory. Note that Visual Basic adds the extension .frm so the form will be saved as frmDemo.frm. Note that I could have used a different name when I saved it, however using the same names increases clarity.

8 Note that the Project box now shows the saved form as frmDemo. frm
Note that the Project box now shows the saved form as frmDemo.frm. Now I am going to save PrDemo as a project file and Visual Basic will add the .vbp extension. Note that I used PrDemo as both the logical name in the project and the physical name. I could have used different names, but this helps me to keep track. Now that the form and the project have been saved, I can save them every few minutes using the Save as opposed to the Save As and I will be sure that I am saving my work as I go along. Again, I will save the form and then the project because the form is a component of the project.

9 First, notice that in the Project box, the Project has been saved as PrDemo.vbp. The first name is the name used in development. The second name is the physical name that the file is actually saved under. I also went in and changed one of the properties of the form. The property Caption has now been set to Demo Form. This Caption now appears at the top of the form.

10 I have now put a label on the form
I have now put a label on the form. To do this, I clicked on the A for label in the Toolbar and went to the form and sketched out the area for the label. After you click on the A in the toolbar and go to the form, you will see the crosshairs. You can use these to place the top left corner of the label. You then draw to where you want the bottom right corner to be - this creates a rectangle for the label. As you can see, this label is now named Label1 and has a caption of Label1. I will change both these things on the next screen.

11 The next step is to give this label a name
The next step is to give this label a name. Because I want the student to enter their name in the text box that I am going to put on the form, I named the label lblName. The lbl is a designation that this is a label. I then went to the caption and changed the information that I wanted to appear in the label on the form.

12 Now I am placing a textbox on the screen so the user can key in their name in the text box. Note that the original name is Text1. I will change that name on the next slide.

13 I have now changed the name of Text1 to txtName
I have now changed the name of Text1 to txtName. I chose this name because this is the textbox where I want the user to enter their name.The convention is to start textbox names with the letters txt. Notice that the name in the area showing which object is highlighted now reads txtName TextBox.

14 When you saw Text1 in the box it was because Text1 was listed as the Text Property. I went in and cleared it out and left blanks as the Text Property. To remove the word Text1 from the box, I need to go to the Text property and change it to blank. Note that with labels, I change the caption property and with textboxes I change the text property.

15 I have now put a second text box on the form
I have now put a second text box on the form. Note that it is called Text1 because I changed the name of the other text box. If the first one was still called Text1, then this would be Text2.

16 I named this TextBox txtHello because I am going to put a Hello message here. Note that I went down to Text and cleared the property so the TextBox appears with no entry on the screen.

17 In this slide, I am placing a CommandButton on the form
In this slide, I am placing a CommandButton on the form. I will use this button to accomplish my goal of putting out a message on the from in txtHello.

18 I have now named the CommandButton with the name cmdSayHello and put a caption on the button using the Caption Property. Note that CommandButton names start with cmd by convention.

19 A text button does nothing until I put in the code to do something.
First, to bring up the Code Window where I can write my code, I double-click on the CommandButton named SayHello. The Code Window shown appears with the code that you see already written. These are the first and last lines of the sub procedure that will be executed when the SayHello button is clicked. The first line is the name of the object being used and the name of the event associated with that object. What we have is the Click event on the Command Button named comdSayHello. The last line simply ends the sub procedure. Look at the boxes at the top. The left box says cmdSayHello this is showing the name of the object that the code applies to. The right box says Click. I left the mouse on this box to bring up the word Procedure which applies to this box. Essentially Click is an Event that will trigger the subprocedure that we are coding to be executed. The next few slides go into this in more detail.

20 This shows the choices available in the object box
This shows the choices available in the object box. They include the form itself, the objects that I have put on the form and (General). General can be used to declare variables, a concept that will be discussed in later examples.

21 This slide shows the events that are possible including theClick even, the KeyDown event, the MouseDown event and the LostFocus event. For our purposes we are using the click event, so it becomes part of the Visual Basic generated name for the subprocedure. Note that the event is written in the name as Click(). More about that later.

22 Now I am adding code to the click event
Now I am adding code to the click event. This code is an assignment statement which assigns the code on the right of the assignment sign (=) to the named object on the left of the assignment sign. Looking at the code on the right, we have the word Hello followed by a space inside quotes. This is a literal. We want this exact word. Then we have the & which stands for concatenation. Concatenating means that we are combining the thing that precedes the concatenation sign with the thing that follows it. In this case we have txtName.Text after the concatenation sign. This means we want to concatenate it with the Text that is in the txtName TextBox. Note that Text is the property of txtName. Since Text is the default property it does not have to be written, but it helps with clarity especially at the beginning of the course. Note that we have the name of the object (txtName) followed by the dot and then the property. This is the way we express the property of a particular object. Now that we have concatenated Hello with the contents of the txtName TextBox, we will assign this result to the TextBox called txtHello. Note that I could have coded this as: txtHello.Text = "Hello " & txtName.Text This would clarify that I want to make the information on the right the Text property of the txtHello TextBox.

23 Note that when I keyed in the command, when I came to enter the Property, a box appears that offers me the legal possibilities. Note also that in this version of the example, I assigned the results to txtHello.Text. Again, this assigns it to the Text Property of the TextBox named txtHello. At this point, I am going to save the form and the project as illustrated on previous slides.

24 Click on this button to stop running the project.
Click on this button to run your project. To run the project you can use the start button on the toolbar as shown here. The execution can be stopped by clicking the stop button. You could also run the project by using the menu and selecting Start/Run. Another option is to press F5 (if you look at the menu, you will see this is the shortcut key for Start/Run.

25 User enters the name in the TextBox.
User presses the Say Hello CommandButton. The results appear in the TextBox. This slide shows the execution of the project. The user keys in the name Mary Smith in the first TextBox. The user then Clicks the Say Hello CommandButton. The Click Event code is executed and the code is assigned to the second TextBox and is displayed on the screen.

26 Private Sub cmdSayHello_Click()
frmDemo - 1 Private Sub cmdSayHello_Click() txtHello.Text = "Hello " & txtName.Text End Sub If you want to print out your code, you can select File/Print and then pick the options. Current Module will get you the active module you are working on while Current Project will get you all of the code in the project. You can also print an image of the form, the code that you wrote and/or the Form as Text.

27 VERSION 5.00 Begin VB.Form frmDemo Caption = "Demo Form" ClientHeight = ClientLeft = 60 ClientTop = 345 ClientWidth = LinkTopic = "Form1" ScaleHeight = ScaleWidth = StartUpPosition = 3 'Windows Default Begin VB.CommandButton cmdSayHello Caption = "Say Hello" Height = 495 Left = 360 TabIndex = 3 Top = Width = End Begin VB.TextBox txtHello Height = 615 Left = 240 TabIndex = 2 Top = Width = Begin VB.TextBox txtName Left = TabIndex = 1 Top = 120 Width = Begin VB.Label lblName Caption = "Enter your name:" Height = 375 Left = 240 TabIndex = 0 Top = 240 Width = End Attribute VB_Name = "frmDemo" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Private Sub cmdSayHello_Click() txtHello.Text = "Hello " & txtName.Text End Sub This is the code you see if you go to Notepad and open the frmDemo.frm that you saved. The part up to Attribute is the part you would see if you do Print and select Form As Text. This code shows the properties that are associated with the form and the properties associated with each object that I put on the form It also shows the subroutine that I coded which is a Click event associated with the button. Notice the Height, Left, Top and Width that were created when I positioned the object on the form. Notice also the TabIndex. The first thing I put on the form has a TabIndex of 0, the next thing has a TabIndex of 1 and so forth. This represents the order on the form. The objects that allow user input will show focus in this order. When I executed this project the focus was on txtName because that is the first place where I could enter data. The TabIndex property can be changed in the properties of each object to make movement through the form conform with the developer's goals.

28 This shows how I can change the Font in a particular text box
This shows how I can change the Font in a particular text box. I am making the font of txtHello Script MT Bold and setting it to a size of 12. The result is shown on the next slide.

29 This shows the response after the Say Hello button has been clicked
This shows the response after the Say Hello button has been clicked. The result is shown in the TextBox with a script font and in a larger size. The properties after this change are also shown.

30 I have now use the Palette to change the ForeColor (which will be the color of the font) to a bright blue.

31 I have now changed BackColor using the palette
I have now changed BackColor using the palette. Again note that these changes are being made to the properties of txtHello.

32 In this slide I have set the background color (BackColor) for the form
In this slide I have set the background color (BackColor) for the form. Note that this says frmDemo. I have also set the background color of the label box called lblName.

33 I have now added a Clear button
I have now added a Clear button. It does nothing until I put in the code that I want to be executed when the button is clicked on.

34 I should have used txtName instead of txtNam.
I have made a mistake here - I am trying to set a TextBox named txtNam to null which is an empty string or a null string. This is done by putting two quotation marks together. It will clear the contents of a text box or a label.

35 When I execute this code by clicking on the Clear button, I get a Run-time error. When I click on Debug it shows me the code that has caused the problem. The message is Object required. This is because txtNam has not been defined as an object - I should have used txtName. When I click on Debug, I see the code where the problem occurred highlighted in yellow.

36 Note that when I position my cursor over the invalid name, I see nothing. When I position my cursor over the valid object, txtHello, I see the contents of txtHello. This happens in run time and is a helpful debugging tool.

37 Once the accurate object name (txtName) is used, the program works when the Clear button is clicked.

38 Now I am going to go to General and code Option Explicit
Now I am going to go to General and code Option Explicit. This forces you to explicitly declare all variables. This means everything has to be defined. In the previous, code without Option Explicit, VB assumed there was a field called txtNam and the you got a Run time error. Now you are going to get a Compile Error

39 The compile error tells you that the variable is not defined and highlight txtNam. It is referred to as a variable because the user can define a variable field to hold things in memory and VB does not know what you were doing here so it assumes it should be a variable. Note that a variable is simply a work area that the programmer can set up in memory.

40 Note that if under Tools/Options you had checked Require Variable Declaration, VB would put in the Option Explict for you.

41 Click on the End icon or the X to exit the program if the programmer/developer has not provide an alternative I am now adding a third button which I have named Exit. Up until now, if you wanted to end the program you could click the End icon or the X in the top right corner of the form. Now we are establishing a button that can be used to exit.

42 The End command can be used to terminate the program
The End command can be used to terminate the program. The End command stops running the program and returns to design time. This is the time when you can work on the design and development of your project. Run time is when you are executing your program.

43 Finally, I have added documentation to my project
Finally, I have added documentation to my project. The single quote marks a line as documentation which means that it will be ignored when the program is run. Note that the documentation lines are shown in green. Documentation is valuable for someone picking up your program and trying to understand what it does.

44 This is another example of a compile error
This is another example of a compile error. Here instead of using the word End, I used the word Exit. Exit has a different meaning to Visual Basic and so it immediately tells me that it expected something else to accompany Exit.

45 A logic error means that you did not get the output you wanted because you made a mistake when you did the code. This is not a misuse of the language - it is simply not doing what you planned. For example, this program says Hello followed by a space and the person’s name. If I had not included the space in the quotes around Hello, then the name would be pushed right against the word Hello. This is a logic error.


Download ppt "Creating and executing a program"

Similar presentations


Ads by Google