Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming

Similar presentations


Presentation on theme: "Introduction to Programming"— Presentation transcript:

1 Introduction to Programming
Fundamentals of Programming in Visual Basic

2 Outline and Objective Visual Basic Objects Visual Basic Events Numbers
Strings Input/Output Built-In Functions hhhhhhh

3 The Initial Visual Basic screen
Toolbar Menu bar Project Explorer window Toolbox Properties window Form

4 Steps to Create a Visual Basic Program
1. Create the Objects 2. Set Properties 3. Write the Code for each Event hhhhhhh

5 Four most useful Visual Basic Controls
Text Boxes Labels Command Buttons Picture Boxes The icons in the Tool Box represent objects that can be placed on the form. The four objects discussed today are: text boxes….. Text Box: you use a text box primarily to get information, referred to as input , from the user Label is placed next to text box to tell the user what type of information to enter into the to he text box Command button : The user clicks a command button to initiate an action Picture Boxes: You use a picture box to display text or graphics hhhhhhh

6 A Text Box Walkthrough:
Double-click on Text Box to add a Text Box to your form Activate the Properties window (Press F4) Set values of Properties for Text Box Go over resizing and dragging the text box The first line of the properties window (called the object box) reads “Text1 TextBox” . Text1 is the current name of the textbox. The two Tab permit you to view the list of properties either alphabetically or grouped by categories.. We discuss four properties : The Text property determines the words in the text box. Press Shift+Ctrl+F to move to the first property beginning with F . Move to the property ForeColor. The foregroung color is the color of the text.. Click on Palette tab to display a selection of colors. Highlight the Font property and change it to Italic hhhhhhh

7 A Text Box Walkthrough Text box

8 Some Useful Properties:
Name Caption Border style Visible Back Color Alignment Font Border Style: Setting the BorderStyle to 0-None removes the border from an object Visible: Setting the property to false hides an object when the program run. The object can be set to reappear with code BackColor: Specifies the background color for text box, label, picture box or form.. Also specific background color for a command button having Style set to “1-Graphical” BackStyle: The background of a label is opaque by default. Setting the background style of a label to transparent causes whatever is behind the label remain visible.; the background color of the label essentially becomes “see through” Font: Two unusual fonts are Symbols and Wingdings> For instance with the windingsfonts , changing the text to % & ‘ and J yields a bell, a book, a candle and a smiling face hhhhhhh

9 Naming Objects: Use the Property window to change the Name property of an object Good Programming habit is that each name begins with three letter prefix that identifies the type of control. hhhhhhh

10 Naming Objects: hhhhhhh

11 Visual Basic Events Code is a set of statements that will be executed when you run a program. Write Code for each Event. Most Events are associated with Objects. The code for each event is called an “Event Procedure”. When a VB program is run, a form and its controls appear on the screen. Normally, nothing happens until the user takes an action, such as clicking a control or pressing the Tab key. Such an action is called event. hhhhhhh

12 The steps for creating a VB program:
Create the Interface. Set Properties for the objects. Write the code that executes when event occur.

13 An Event Procedure Walkthrough
Create the interface. Set Properties. Double click on the object to open the Code window. Click on the Procedure box to find the event Write the code for that event. Object Property Setting frmWalkthrough Caption Demonstration txtPhrase Text (blank) cmdBold Caption Make Phrase Bold Object Box Procedure Box: Contains a list of all possible event procedures associated with the text box Choose txtPhrase_LostFocus() txtPhrase.Font.Size = 12 End Sub Choose GotFocus and type: txtPhrase.Font.Size = 8 txtPhrase.Font.Bold = False Go to command Button and choose cmdBold_Click and type txtPhrase.Font.Bold = True hhhhhhh

14 Arithmetic Operations & Hierarchy of Operations
Operator Operation Basic expression ^ Exponentiation A ^ B * Multiplication A * B / Division A / B Addition A + B Subtraction A - B hhhhhhh

15 Keywords Words that have predefined meaning to Visual Basic .
Can Not be used as variable names. Example: Print Cls If While The VB editor automatically capitalizes the first letter of reserved word hhhhhhh

16 Internal Documentation
An apostrophe (‘) can be used to indicate comments; comments are ignored by Visual Basic. The keyword Rem can also be used instead of an apostrophe for comments. Remarks can also be placed after program statement too. hhhhhhh

17 Data Types Each variable in the program is assigned to a data type.
hhhhhhh

18 Declaring Variable Types
Use the Dim statement to Declare the type of a variable. Example: Dim number As Integer Dim flower As String Dim interestRate As Single From now on we will declare all variables. Declaring variables is regarded as good programming practice. hhhhhhh

19 Data Types : Single-precision numeric variable: Stores real numbers
Double-precision numeric variable: Stores real numbers with many digits Integer: Stores integers Long integer: Stores integers with many digits The default data type is a single precision numeric variable Therefore Number and Number! Are the same Double precision variable is used to store number with many digits A single precision variable is accurate to about seven decimal points where double precision is accurate to about 15 decimal points Is used when a high degree of accuracy is needed. If you try to assign a real number , the number would be cut off and an integer would be assigned to it. Declaring variables is regarded as good programming practice Example: Dim variableName As String Dim variableName As Single Type Declaration Tags hhhhhhh

20 Using Text Boxes for Input/Output
The contents of a text box are always a string. Numbers are also stored in text boxes as strings. hhhhhhh

21 Using Text Boxes for Input/Output
Therefore, the contents of a text box should be changed to a number before being assigned to a numeric variable. Val (txtBox.Text) changes the input string into a number. Example: numVar = Val (txtBox.Text) Function Input Output Str number string Val string number If str is a string representation of a number, then Val(str) is that number. Conversely, if num is a number, then Str(num) is a string representation of the number. Therefore statements such as numVar = Val(txtBox.Text) and txtBox.Text = Str(numVar) hhhhhhh

22 Example (convert miles to furlong and vice versa)
Private Sub txtFurlong_LostFocus() txtMile.Text = Str(Val(txtFurlong.Text / 8)) End Sub Private Sub txtMile_LostFocus() txtFurlong.Text = Str(8 * Val(txtMile.Text)) hhhhhhh

23 Using Message Box for Output:
Use message box to get the user’s attention. Message box is a predefined dialog box too.

24 Syntax for Message Box MsgBox prompt, , title
When a statement of the form above is executed , where prompt and title are strings, message box with prompt displayed and the title bar caption title appears., and stays on the screen until the user presses Enter or clicks K. hhhhhhh

25 Example of Message Box MsgBox “Nice try, but no cigar”, , “Consolation” Stays on the screen until the user presses OK


Download ppt "Introduction to Programming"

Similar presentations


Ads by Google