Kingdom of Saudi Arabia - mis101.files.wordpress.com file · Web viewDepartment of MIS Yanbu...

18
2 nd Semester 2008-09 MIS-101 Handout / Labsheet 1 INTRODUCTION TO PROGRAMMING USING VISUAL BASIC.NET OBJECTIVE The objective of this lab exercise is to explain the basics of the visual studio IDE, demonstrate to the students how to use Visual Studio’s help features and to how create, compile and execute a simple visual Basic program. LEARNING OUTCOMES Upon completion of this laboratory exercise, the student will learn how to: Start Visual Studio .NET Set up a projects folder for all his Visual Basic work Create a short Windows program Use the Visual Studio’s help features MATERIAL REQUIRED a. PC With MS Visual Studio.NET Installed b. Lab Sheet 1 _____________________________________________________________________________________________ Prepared By: Abubakar Adam Page 1 of 18 March 11, 2022 Kingdom of Saudi Arabia ة ودي سع ل ا ة ي ب ر لع ا كة ل م م ل اRoyal Commission at Yanbu ع ب ن ي ة ي لك م ل ا ة" ي هن ل اUniversity College – Yanbu ع ب ن ي- ة ي مع ا ج ل ا ة يل ك ل اDepartment of MIS Yanbu Al-Sinaiyah ة ي ع ا ن ص ل ع ا ب ن ي

Transcript of Kingdom of Saudi Arabia - mis101.files.wordpress.com file · Web viewDepartment of MIS Yanbu...

2nd Semester 2008-09MIS-101 Handout / Labsheet 1

INTRODUCTION TO PROGRAMMING USING VISUAL BASIC.NET

OBJECTIVE

The objective of this lab exercise is to explain the basics of the visual studio IDE, demonstrate to the students how to use Visual Studio’s help features and to how create, compile and execute a simple visual Basic program.

LEARNING OUTCOMES

Upon completion of this laboratory exercise, the student will learn how to: Start Visual Studio .NET Set up a projects folder for all his Visual Basic work Create a short Windows program Use the Visual Studio’s help features

MATERIAL REQUIRED

a. PC With MS Visual Studio.NET Installedb. Lab Sheet 1

_____________________________________________________________________________________________Prepared By: Abubakar Adam Page 1 of 15 May 14, 2023

Kingdom of Saudi Arabia السعودية العربية المملكةRoyal Commission at Yanbu ينبع الملكية الهيئةUniversity College – Yanbu - ينبع الجامعية الكليةDepartment of MISYanbu Al-Sinaiyah الصناعية ينبع

1 INTRODUCTION TO COMPUTER PROGRAMMING

1.1 Computer Program vs. Computer Programming

A computer, by itself, knows absolutely nothing. It doesn’t even know how to display information on a screen or access a hard drive for data. It doesn’t know how to access the internet nor does it know how to play a sound.

But computers are very good at following and repeating instructions flawlessly and very quickly. This is what makes these machines so powerful. Someone had to write the instructions, called code or program, to make the computer do what we want it to do.

The objective of this handout is to help the students be familiar with how to write their own programs in order to make the computer perform the tasks they want.

1.2 BugsComputers always do exactly what they are told to do. However, programmers don’t always instruct the computer correctly. When this occurs, it is referred to as a bug. A bug can have varying degrees of severity. Some bugs can cause the computer to become unresponsive and others are mere annoyances.

There are many reasons why bugs are introduced into software. They include the following:1. Laziness on the part of the programmer2. A misunderstanding of a particular feature of a programming language3. Not enough time to test every feature thoroughly.

2 A BRIEF HISTORY OF COMPUTER PROGRAMMING

2.1 Evolution of Programming LanguagesTo write a program for a computer, you must use a computer language. A computer language is a set of predefined words that are combined into a program according to predefined rules (syntax).

Over the years, computer languages have evolved from machine language to natural languages. A time line for computer languages is presented in Figure 1.

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig2

Program: is a set of instructions that tells the computer what to do.Programming: is the act of writing code or program that is interpreted by the computer to carry out some repeatable task.

A computer is an electronic, automatic and programmable device that accepts input, processes the input and gives output, which can be stored for future retrieval.

A bug is a mistake in a program introduced by a programmer, which may lead to undesired results.

Figure 1: Evolution of computer languages

2.2 Categories of Programming LanguagesToday, computer languages are categorized according to the approach they use in solving a problem and the category of problems they solve. As shown in Figure 2, computer languages can be categorized into five categories:

1. Procedural (Imperative) Languages2. Object-Oriented Languages3. Functional Languages 4. Declarative Languages5. Special Languages

Figure 2: Categories of computer languages

2.2.1. Procedural LanguageProcedural Language is a set of instructions that are executed one by one from beginning to end unless an instruction forces the control elsewhere. Even in this case, the program is still a series of instructions that are executed one after another, although some may be executed more than once or some may be skipped.

Examples of procedural programming languages include: FORTRAN (FORmula TRANslation), COBOL (COmmon Business-Oriented Language), Pascal, C etc.

2.2.2. Object-Oriented Language (OO)OO programming is a mindset or a philosophy that attempts to:

1. simplify application development2. relates code objects to real world objects3. creates more flexible applications

Examples of OO programming languages include: C++, Java, VB.NET

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig

Computer Languages

Procedural Object-Oriented Functional Declarative Special

3

2.2.2.1. Classes vs. ObjectsThe basic building block of Object-Oriented programming is the class; class is closely related to object.

Informally, an object represents an entity, either physical, conceptual, or software. An object is something that has:

1. State2. Behavior3. Identity

The state of an object is one of the possible conditions in which an object may exist. The state of an object normally changes over time.

Behavior determines how an object acts and reacts to requests from other objects. Behavior is represented by the set of messages it can respond to (the operations the object can perform). Assigns, teaches, takes etc.

Each object has a unique identity, even if its state is identical to that of another object

2.2.3. Functional LanguageIn functional programming, a program is considered a mathematical function. In this context, a function is a black box that maps a list of inputs to a list of outputs. See the following Figure 3.

Figure 3: Function in a functional language

For example, summation can be considered a function with n inputs and only one output. The function takes the n inputs, adds them, and creates the sum.

A functional language does the following:1. It predefines a set of primitive (atomic) functions that can be used by any programmer.2. It allows the programmer to combined primitive functions to create new functions

LISP (LISt Programming) is an example of functional programming languages.

2.2.4. Declarative LanguageDeclarative Language uses the principle of logical reasoning to answer queries. It is based on formal logic defined by Greek mathematicians and later developed into what is called first-order predicate calculus.

Logical reasoning is based on deduction. Some statements (facts) are given that are assumed to be true; the logician uses solid rules of logical reasoning to deduce new statements (facts).

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig

Function(Black box)

n inputs m inputs

4

A class is a description of a group of objects with common properties (attributes), behavior (operations), relationships, and semantics. An object, on the other hand, is an instance of a class.

For example, the famous rule of deduction in logic is

Prolog is an example of declarative language.

2.2.5. Special LanguageDuring the last decade, some new languages have emerged that cannot be placed in the four categories just discussed. Some are a mixture of two or more models and others belong to a specific task. Examples include HTML, PERL, and SQL.

3 PROGRAM DEVELOPMENT CYCLESince Software refers to a collection of instructions for the computer, the computer only knows how to do what the programmer tells it to do. Therefore, the programmer has to know how to solve problems.

The following are steps that will help the programmer plan his program development.

i) Analyze: Define the problem.ii) Design: Plan the solution to the problem.iii) Choose the interface: Select the objects (text boxes, buttons, etc.).iv) Code: Translate the algorithm into a programming language.v) Test and debug: Locate and remove any errors in the program.vi) Complete the documentation: Organize all the material that describes the program.

4 PROGRAMMING TOOLSThree tools can be used to convert algorithms into computer programs:

• Flowcharts - Graphically depict the logical steps to carry out a task and show how the steps relate to each other.

• Pseudo-code - Uses English-like phrases with some VB.NET terms to outline the program.• Hierarchy Charts – It shows the overall program structure. They are also called structure

charts, HIPO (Hierarchy plus Input-Process-Output) charts, top-down charts, or VTOC (Visual Table of Contents) charts.

Hierarchy charts depict the organization of a program but omit the specific processing logic. They describe what each part, or module, of the program does and they show how these modules are related to each other. The details of the functionality of modules are shown. The chart reads from top to bottom and from left to right. Each module maybe subdivided into a succession of sub modules that branch out under it. A quick glance at the hierarchy chart reveals each task performed in the program and where it is performed.

The main benefit of hierarchy charts is in the initial planning of a program. We break down the major parts of a program so we can see what must be done in general.

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig5

If (A is B) and (B is C), then (A is C)Using this rule, we haveIf Ehab is a human and a human is mortalThen Ehab is mortal.

Figure 1.1 Hierarchy chart for the postage stamp problem

ExamplesExample1: The following example shows a decision flowchart.

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig

Postage Stamp program

Read Sheets Calculate Stamps Display Stamps

Set Stamps = sheets / 5 Round stamps up to next whole number

6

Example 2: The following example shows a looping flowchart.

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig7

Labsheet 1

INTRODUCTION TO VISUAL STUDIO.NETVisual Studio .NET is the tool for rapidly building enterprise-scale ASP Web applications and high performance desktop applications. Visual Studio includes component-based development tools, such as Visual C#, Visual Basic, and Visual C++, as well as a number of additional technologies to simplify team-based design, development, and deployment of your solutions.

5 STARTING UP VISUAL STUDIO .NETTo proceed, you will need to have Microsoft Visual Studio.NET installed on your computer. To start Visual Studio.NET, following the following instructions:

StartProgramsVisual Studio .NETVisual Studio .NET

5.1 The Start Page

Figure 4: The start page

The Start Page:– Opens when Visual Studio .NET is opened– Contains helpful links– Allows creation or opening of a project– Displays recent projects

5.2 Type of ProjectsClick on the New Project button in the Projects page. This will open the New Project dialog box (see fig 1.2), from which you can select a type of project to create.

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig

Buttons

Start Page

Empty project list

Page loaded when Get Started is clicked (This

page also is displayed when the Start Page is loaded

Start Page Links

8

Figure 5: The New Project dialog box

6 CREATING A FOLDER FOR YOUR VB .NET PROJECTSAt this stage, you could simply enter the name of the new project in the Name box and press the OK button to begin. However, since this is your first project, a far more organized approach is to start by nominating the folder you want your projects to be stored in.

Figure 6: Project location

6.1 Starting a New ProjectTo start a new project in a folder of your choice:1. Select the Get Started menu in the Start page (shown in Figure 4) then

a. Click on the Projects tab and then

b. Click the New Project button

2. Click on the Browse button in the New Project dialog box and navigate to the drive and folder you intend to store your projects on (e.g. C:\MIS). Select the folder you want to use or click on the New Folder icon to create one, and then press the Open button.

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig

Title bar (displaying

New Project)

Projects Types: pane

Templates: pane

Visual Basic .NET Windows Application (selected)

Description ofproject selected in Templates: pane

Default project and solution name

Location of new project

Visual Basic Projects folder (selected)

9

3. You should now have returned to the New Project dialog box. Make sure the project type selected in the left hand pane is Visual Basic Projects as shown in Figure 5.

4. Enter the name WelcomeToVB in the Name box.

5. Click on OK to start up the new project in Visual Studio.

7 CREATING A SHORT WINDOWS PROGRAM

7.1 The VB.NET Environment

Figure 7: The VB.NET Environment

7.2 Solution Explorer, Server Explorer, Toolbox and Property WindowTable 1: The Solution Explorer

The Solution Explorer:1. Displays Project Hierarchy

a. Project referencesb. Forms, classes, modulesc. Folders with sub-items

2. “Show All Files” Mode3. Help the user to manipulate Projects

a. Drag-and-drop editingb. Context menus

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig

Tabs

Active tab

Form (Windows application) titled

Form1

Menu bar

Solution Explorer

Properties window

10

Table 2: The Server ExplorerThe server explorer can be used to:

1. Manage Data Connections2. View and Manage Servers3. Drag-n-Drop items

Table 3: The Toolbox and the Property WindowToolbox Property window

7.3 Designing the Welcome to VB.NET Windows Application

Example 1Using Labels and Picture Box on the toolbox, designed the Welcome to VB.NET Windows Application.

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig11

Do the following:1. In your folder, create the folder MIS2. Create LAB1 in the MIS folder3. Using Paint editor, create a simple logo for VB.NET and save it in the folder LAB1.4. Create a new VB.NET project in LAB1 using the name WelcomeToVB5. Using the Label, and Picture Box tools in the toolbox, design a form similar to the one shown

below.

Figure 8: The Welcome to VB Application

7.4 Adding a Button to the Windows ApplicationTo add a button to the form above

1. double click the button icon on the toolbox in order to add the a button to form2. rename the title of the button to “Designed By”3. Double click the “Designed By” button and add the following code

MessageBox.Show(“Designed by MIS 101 Students”)

7.5 BuildingThe Build menu, shown in Figure 7, contains commands that let you compile projects within a solution.

7.6 Running the programTo run the program you designed, simply press F5 and the output of the program will be displayed as shown below:

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig12

Figure 9 Running the Program

Example 2Using Labels and Picture Box on the toolbox designed another Windows Application. Your form should look similar to the one shown in Figure 10.

Figure 10: Example 2 ApplicationDo the following:

1. Create another VB.NET project in LAB1 using the name Example 2.2. Add the labels shown in example 2.3. Add the button “Designed By”.4. Using the Picture Box control, add the above picture by:

a. Clicking on the button besides “Image” in the Properties Windowb. Check the “Local resource” radio buttonc. Click the “import…” buttond. Select any picture in the following folder

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig13

C:\Program Files\Microsoft Office\CLIPART\PUB60COR

8 Use the Visual Studio’s help featuresVisual Studio provides extensive help features and as you begin to learn Visual Basic; you will probably become very familiar with the Help system. However, it is worthwhile to give you an overview, just to help speed your searches for information.

The Help menu contains the menu items shown in Figure 11.

Figure 11: The Help menu

As you can see, this menu contains a few more entries than the typical Windows application. The main reason for this is the vastness of the documentation.

One really fantastic feature is Dynamic Help. When you select the Dynamic Help menu item from the Help menu, the Dynamic Help window is displayed with a list of relevant topics for whatever you may be working on.

9 EXERCISES1. Create a new folder in LAB1 (which is the folder you created in MIS as described above) using

the name EXERCISE.

2. Using VB.NET, write a simple program that displays the names of your favorite Animal or your City.

3. Insert any picture on the form you have designed.

10 REFERENCE

[1] Behrouz A. Forouzan, “Foundation of Computer Science”, THOMSON, BROOKS/COLE, Pacific

Grove, CA 93950, USA, 2003.

[2] Evangelos Petroutsos, “Mastering Visual Basic .NET”, SYBEX Inc., USA, 2002.

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig14

[3] Internet

MIS 101 Handout/Labsheet 1 Prepared by: Mirza Abrar Baig15