Lecture notes on visual basic.net

microsoft visual basic net programming fundamentals and mastering visual basic .net database programming pdf free download
LottieBarners Profile Pic
LottieBarners,Hawaii,Researcher
Published Date:12-07-2017
Your Website URL(Optional)
Comment
       Visual Basic.NET Programming Introduction to Visual Basic.NET VB.NET Programming Environment (Review) (Part I of IV) (Lecture Notes 1A) Prof. Abel Angel Rodriguez 1.2 Components of an Object-Oriented Program 1.2.1 Understanding Classes & Objects  Real world objects have attributes or properties that define the objects.  Also, real world objects are based on some mold or template.  In Object-Oriented programming, the objects are based on a class or template. In this section we take a look at the components that make up an Object-Oriented Program The Class  The mechanism VB.NET provides to implement Objects is the Class.  A Class is a template or blueprint that defines what Object of the class look like.  A Class is a plan or template that specifies what Data , Methods & Events will reside in objects  The objects of the class contain data and the Methods (member functions & procedures) that operate on such data  When creating a Class Module, the Data is made Private, & the interface or method to access the data (Procedures & Functions) are Public.  For example:  We can have a Class called Automobile, and from this class, we can define the Properties, Methods and Events of this class.  From this Automobile class we can create Objects of this class such as a Car Object, Truck Object, SUV Object etc.  The objects created have all the properties, methods and events dictated by the Class from which they were created from. Objects  Think of Objects as a thing or a noun. Objects are the items that represent real-world entities, such as a person, place or thing in a program.  In a program an Object is a software representation of a real-world entity. Objects - vs - Class  The concept of a Class an Object can be very confusing. A Class is NOT an Object. An Object is not a Class  DO NOT confuse a Class with the Objects, they are two different things.  Objects are the manifestation or instance of a Class specification.  A class IS NOT the object, but the template in which Objects will be created from  Think of the class as the architectural floor plan of a house, and the objects as the houses that are built from that plan. You create ONE floor plan or blue print, but you can create as many houses as you like from the blue print.  Objects behave exactly as they were specified in the Class. No more, no less Private Data  Data is the storage mechanism inside the object to store information.  Data is what we want to manipulate and protect.  For example, a person has a name, an ID, birth date etc. These entities are stored and preserved INSIDE THE OBJECT.  In a class Object, Data is Private and cannot bee seen by the outside world.. Public Properties (Attributes)  An Object has characteristics. Such characteristics or attributes are called properties of an Object. For example a Person Object has a name property, a social security property, a birth date property etc.  Properties represent the Data of the Object. DO NOT CONFUSE THE PROPERTY WITH THE DATA. They are two different things.  In reality, the Property is the way the outside world access the actual data directly.  This is confusing; the property is not the data, but a vehicle to access the data. The actual data is private and cannot be seen by the outside world, only the properties are seen by the outside world because they are PUBLIC.  For example from an Automobile Class, you may create an Object named objCar. The Automobile Class may have a color property, as well as a Make & Model property. But inside the Data is what stores this information. This is done using private variables inside the class. For example these variables can be named m_color, m_make & rm_Model etc. but the outside world cannot see these variables, when they want to use the data they see the property Color, Make & Model and through these properties the data are accessed.  Properties are the vehicle in which you can SET (write) or GET (access) the DATA 7  Syntax for using an object Properties is based on the DOT OPERATOR: Object.Property  Example, assuming you create an object named objCar from the Automobile class, writing and accessing data is done as follows: Purpose Syntax Example SET or write data Object.Property = value objCar.Make = “Acura” GET or access data value = Object.Property aStringVariable = objCar.Color Methods (Behavior)  Objects have behavior or take action.  Methods are actions that the Objects can take. Where Objects are the Nouns, Method are the verbs or actions of an Object.  For example a Car Class Object can take the following actions: Start, Stop, Speed Up, Slow Down, turn left, turn right etc.  Methods are implemented in a class by creating Functions and Sub Procedures that you write to make the object do things or take some kind of action  Syntax for using an object Methods uses the DOT OPERATOR as well: Object.Method()  Example, using the objCar from the Automobile class: Purpose Syntax Example Executing or telling the Car object to take an Object.Method() objCar.Stop() action such as stopping Executing or telling the Car object to take an Object.Method() objCar.Start() action such as starting the car Events  This is a tough one to explain and understand  Events are actions taken UPON the object by an outside force (User, Program code etc).  These actions or Events upon the object will automatically trigger specialized Methods automatically created outside of the object known as Event-Handlers. In other words, Objects respond to events by executing this special method or procedure know as an Event-Handler  Do not confuse Events and Event-Handlers with regular Methods. Events are the action taken by an outside source upon the object, while Methods are action taken by the Object itself when told.  Events & methods may work hand in hand, but they are two different things.  This can be confusing. For example an Object such as the objCar Object can have a method called Stop(). You can explicitly call the objCar.Stop() method to so the car will stop itself.  On the other hand, The Car Object can also have an Event programmed into it called OnCrash which will create outside the object and associated Event-handler named objCar_OnCrash().  In the event that the car is hit by another car or crashes, the OnCrash event will automatically trigger or EXECUTE the Event-handler objCar_OnCrash(). Inside the objCar_OnCrash()) Event-Handler you can code in what ever you like. For example you may want to put in a statement that calls or execute the objCar.Stop() method to stop the car, 8 1.2.2 Creating Object-Oriented Programs (IMPORTANT)  Object-Oriented Programs (OOP) are written based on the Class Objects and not on the functionality of the program  The following steps is what you need to do every time you create an Object-Oriented-Program  The three steps required to creating an Object-Oriented Programs are shown below: I. Create and Define the class specification or Class  Define Private Data, Properties, Methods & Events II. Create Object of the Class III. Use the Object of the Class  Write the program to manipulate, access or modify the objects as follows:  Get and Set Properties (Manipulate the data)  Call Methods  Trigger Events  Program Event-Handlers  Interact with other objects 10 1.2.4 Object-Oriented Programming and Graphical Elements (Forms & Controls)  At this point you have a basic understanding of a Class, Object, Properties, Methods and Events  Also, you have taken courses (CS101 & CS508) in which you crated VB.NET programs using Forms and graphical controls such as Text Boxes, Buttons, List Boxes, Labels, etc.  You wrote graphical programs using the following steps: 1. Create a Form 2. Dropped Graphical Controls onto the Form ( Labels, Text Boxes, Buttons, List Box etc.) to create your GUI 3. Added programming code to the Event-Handlers of the controls. For example if you have an OK button, you added code to do something on the Event-Handler: btnOK_Click() 4. You build & compiled the program 5. Executed the program  This process just described is known as Event-Driven Programming, as stated in section 1. Why? Because you are simply programming the Event-Handlers of the Graphical Controls, which execute as a reactions to Events on the Controls.  What is the point?  It turns out that Every Graphical Element in VB.NET, such as Forms, Controls etc. Are all OBJECTS  And if they are Objects, a Class exits for them.  The CLASS was CREATED by Microsoft. That’s right  And every time you drag and place a control to a Form object YOU are actually CREATING an OBJECT of that Control.  The Form is an Object and the Controls are now child Objects of the Form (Object Interaction).  Every Time you set or get a property using the Property Window, you are setting or getting a property of the Control Object, so you are USING the object.  Every time you placed code such as: txtBox.Clear() you were executing a Method.  And finally, when you are placing code inside the Event-Handler, you were telling the Event-Handler what to do when the Event takes place or Event-Driven Programming.  So as you can see, all along you have been applying the rules to create classes, create objects and use them as follows: I. Create and Define the class (Done by Microsoft) II. Create Object of the Class (Doe by YOU when you dropped a control on a Form) III. Use the Object of the Class (Done by YOU)  Get and Set Properties (Using Property Window)  Call Methods ( Inside the Event-handler code)  Trigger Events (Every time you clicked on an OK button, lost focus, etc)  Interact with other objects (Drag-Drop controls onto Forms etc)  So all along you were programming using OOP techniques. But Microsoft made it easier for you by creating all the classes and providing an interface or IDE to make it visual and simply the process.  In this course (CS608), you will perform all the steps. You will creating custom classes, creating the objects and using them. 11 1.3 Object-Oriented Analysis, Design & Programming 1.3.1 Analysis and Design  If you recall in our introduction to Object-Oriented Programming, I used the Video Management Program as an example to demonstrate the difference between Procedural, Event-driven and Object-Oriented Programming.  Note that in the beginning of each of the example, the first step was as follows: 1. Analyze the problem required to be solved: Design flow chart, algorithm etc.  In OOP, there are three phases or steps required for developing an Object-Oriented System: Phase 3 Phase 2 Phase 1 Programming Analysis Design (Write Code) (What is the Problem) (How can it be solved?)  Analysis and Design are very important steps. This is where all the “brain work” is done to developing the system and implementing the algorithm. 1.3.2 Program Development Cycle  The program development cycle refers to the steps or process required to create an application from start to finish.  The process involves first understanding what the problem is that you are required to solve, come up with a design or idea on how to solve the problem and finally choose a programming language to implement the project.  The programming language is simply the tool to create the actual program code. You can choose any language you wish to achieve the final results. The difficult part is in the thought process or design to solve the problem.  Remember the following: IS NOT THE LANGUAGE THAT MATTERS BUT THE SKILL OF PROGRAMMING  Knowing how to program is what’s important, not what language you know. Programming languages evolve and change, and new and more powerful languages are being developed. Once you have the SKILL of programming, you can tackle any language, simply learn the new syntax or rules to a new languages and you should be able complete your project. Visual Basic Solution & Project  When you write a program is basically done to solve a problem.  Solving the problem results in a solution to the problem. A VB application is called a Solutions & a solution is composed of one or more Projects.  In this CS608 and CS708, you will be creating Solutions that can contain one or multiple projects. 12 Creating an Applications to solve a problem  Creating an Application involves a Two Part process, I) Planning II) Design III) Programming.  Each Part is broken into three phases or steps: I. PHASE 1 – System Analysis (Understanding the Problem): 1. Analysis means to study, understand and define the requirements of the system. 2. Read and understand the problem description. 3. Write down the requirements and fully understand what is being asked for you to do. 4. Identify what are the INPUT or data to be entered or manipulated by the program 5. Identify what are the OUTPUT or results that are required. II. PHASE 2 – Design (Solving the Problem): 1. Design means developing or creating the solution to the problem. This is a thinking process and the solution is derived from the analysis of the requirements. 2. From the analysis of the INPUT & OUPUT we can derive the required processing. 3. You can use tools such as: o Design the solution – Use diagram, flow charts etc., to analyze and design the solution. 4. Plan the code (Problem Solving) – This step you will write down the action required to solve the problem, this is known as the Algorithm. The Algorithm is obtained using the following tools:  Flow Charts – Graphical representation of the program logic  Pseudo-code – Abbreviated short-hand English-like statements representation of the flow chart Wow this is Too Much THINKING 5. In this phase we design on paper (Not in the computer) the Forms and User Interface required. o Design the User-Interface (GUI) – You draw a sketch of what the Front-End or GUI will look like and the Control Objects required. o Plan the Properties or attributes to the objects in the GUI – In a table write down the properties to the Control Objects in the GUI. III. PHASE 3 – Programming (Writing the Code): 1. Here is where we use VB.NET to create our application. 2. Create the User-Interface (GUI) 3. Set the Properties or attributes to the objects in the GUI 4. Write the code – Use the syntax or programming code of the programming language to create the program. 131.3.3 Designing the Code – Creating an Algorithm Problem Solving via an Algorithm  We now focus on PHASE 2 of our program development cycle, which is planning the code or how to solve the problem. This is the most difficult part of creating an application since it requires thinking logically.  We will start with the definition of an Algorithm: Algorithm: a logical sequence of steps or actions executed in a particular order  The Algorithm is the collection of logical steps require for the solution of the program. The algorithm is what we will derive during the planning phase.  We will use the development tools describe in previous lectures to derive the algorithm:  UML– A graphical diagrams of the CLASSES which will be used to create the OBJECTS of the program  Flow Charts – A graphical representation of the algorithm  Pseudo-code – Short-hand English-like statements. Pseudo code programs are not executable code, but they help programmers “THINK OUT” before attempting to write it in the actual programming language  Plan the code – In this step, you plan (THINK) the code or steps required for the program to run. You will write down the action required to solve the problem. You will use programming tools like UML to design your classes and work flow, pseudo-code and flow charts to plan the necessary logic to solve the problem. This is really the tough part of programming since it requires thinking logically  How the algorithm is written can affect the results or solution to a problem.  For example, supposed you were asked to develop an algorithm name “rise and shine”, which lists the steps for a manager to get out of bed and get to work. The pseudo-code for the algorithm may be as follows: 1. Get out of bed 2. Take off pajamas 3. Take a shower 4. Get dressed 5. Eat breakfast 6. Drive to work  This algorithm gets the manager to work and prepared to make critical decisions.  Now lets change the program control or order in which the pseudo codes are written: 1. Get out of bed 2. Take off pajamas 3. Get dressed 4. Take a shower 5. Eat breakfast 6. Drive to work  This Algorithm gets the manager to work wet and probably not in a condition to make critical decisions. 14 Recommendation on how to Create Algorithms  Thinking like a programmer is not easy, but with a problem solving strategy in place and practice you will get better at it.  One method I can recommend to get you started when writing programs is as follows:  Read the requirements several times until you understand what is required of the program.  Now put yourself in the role of the computer itself and what is require of you to do, and then switch to the role of the user is standing in front of the computer to use it and what is required of the user to do.  As yourself questions as to what is required for each of these characters during their interaction.  Now you write down the steps of the interaction which takes place between the program and the user. Now extract the performed by the computer program, put these steps in their proper order and you have an algorithm and an idea of what needs to be done  For example, supposed you were asked to create the following program: Problem Statement: o Create a login program to authenticate users (similar as to when you log in to your computer). The program should have a Login Form with controls to allow the user to enter the username and password. In addition should have a button to execute the request or cancel. o The program should search a database of username and passwords to verify if this user/password combination is allowed access to the system if found in the database. o From result of the database search allow access to the system or display a message stating that access is denied.  The interactions or exchange between the computer and the user is as follows: What is the First thing the User does? a) Sits in front of the computer What should the Computer do? a) Display the Login Screen What should the User do? a) Enter username b) Enter password c) Click Ok button or Cancel button What should the Computer do? If you user clicks the OK button: a) Extract the username b) Extract the password c) Search the database for the username and password d) If the username and password is found in the database: 1. Allow access to the system 2. Display a welcome screen e) If the username and password is NOT found in the database 1. Display a message stating that access is denied 2. Go back and display the login screen again. If you user clicks the Cancel button: a) Display a message stating that the user cancelled the operation b) Clear the text boxes 15  The algorithm is derived by extracting ONLY the steps performed by the Computer Program: 1. Display the Login Screen 2. If you user clicks the OK button: a) Extract the username b) Extract the password c) Search the database for the username and password d) If the username and password is found in the database: 1. Allow access to the system 2. Display a welcome screen e) If the username and password is NOT found in the database 1. Display a message stating that access is denied 2. Go back and display the login screen again. 3. If you user clicks the Cancel button: a) Display a message stating that the user cancelled the operation b) Clear the text boxes 16 1.3.4 Summary of Strategy for Developing the Algorithm  Ok, lets come up with a strategy to help us learn to think and develop the algorithm.  The strategy to solve the problem will require seven steps as follows: Phase 3 Phase 2 Phase 1 Programming Analysis Design (Write Code) (What is the Problem) (How can it be solved?) I. PHASE 1 – System Analysis (Understanding the Problem): 1. Problem: Write down the problem statement 2. Understand the problem. Make sure you understand what are the inputs and outputs expected. II. PHASE 2 – Design (Solve the Problem): 1. Discussion: Think If it helps, write down your thoughts on what the problem is and what it takes to solve it How am I going to do this? LET’S THINK 2. Ask yourself, what is the input? 3. What is the output desired? 4. Try the technique I recommended of listing the expected interactions between the user and the computer 5. List what processing is required to obtain this output, such as classes or objects, user-interface etc. 6. Create Algorithm using, UML for illustrating the object model , Flow Charts or/and write down the pseudo-code III. PHASE 3 – Program (Write the Code): 1. Create the Classes 2. Create the User-Interface (GUI) using a Graphical programming language such as Visual Basic.NET 3. Write the code – Use the syntax or programming code of the programming language to create the program based on the UML diagram, Algorithm’s pseudo-code. In other words begin to create the OBJECTS AND USE THEM 17         """" %%%%&&&& &&&&   2.1 Microsoft .NET Framework and Visual Studio.NET 2.1.1 Microsoft .NET Framework  The Microsoft.NET Framework is the new computing platform from Microsoft designed to simplify the development for distributed environment such as the Internet.  The framework is designed from the grounds up with the Internet in mind.  It is not that this Framework was designed for Internet programming only, but simply that if an application you create needs Internet capabilities, access to those capabilities are available and almost transparent.  The .NET Framework is composed of two main components shown in the figured below: C++.NET VB.NET C.NET .NET Class Library .NET Common Language Runtime (CLR) Common Machine Language Executable 1. .NET Common Language Runtime or CLR  The CLR is the key component of the .NET Framework. It is a common compiler for all Microsoft programming language. It compiles all Microsoft languages to one Machine Language.  The CLR Allows programmers to write code in different Microsoft languages of their choice, and ensure that the  parts can work together  In addition it provides programmers with services for memory management, data types, security etc. 2. .NET Framework Class Library.  A collection of ready-made reusable classes that work with the CLR.   Programmers using any of the .NET programming languages can use these classes to include in their applications.  The CLR Allows programmers to write code in different Microsoft languages of their choice, and ensure that the parts can work together  Examples of the classes provided by the library are:  o Database access and manipulation o User Interface – Windows Forms, Web Forms, Web Services o Security o Encryption and decryption 18 2.2 The Visual Studio.NET Environment & Visual Basics.NET 2.2.1 Introduction  We will use Visual Basic.NET Integrated Development Environment or IDE to program our applications.  We could choose any of the languages to create our application such as C, VB.NET etc., but this course requires that we use Visual Basic.NET.  Before we begin using the IDE, lets point out some of the types of applications we will be creating in CS608 & CS708:  Console Application – Usually Text only application. Runs from a DOS or COMMAND PROMPT screen.  Windows Application – Graphical Interface user application. Typical Windows application that you normally use, such as MS WORD etc.  Web Based Windows Application – Web application that uses a Browser such as Internet Explorer.  Let’s look at these type of applications in more details 1. Console Application  A Console Application is a program whose output is usually text based (No Graphics).  Console Applications usually do not contain Forms or any graphics, but they can. You can if you like from a console application call Windows Forms etc.  Console Applications are created when an application performs processing that requires very or no user interaction.   They are lighter and have less overhead than standard windows applications since they contain no graphical libraries etc.  Console applications are a good choice when creating programs such as login scripts, device drivers, backend processes, test programs, programs that control hardware devices etc.  The results of a Console Application is placed or controlled from a Command Prompt Window:  2. Windows Application  Windows Applications are your standard graphical applications we are used to using.  Windows Apps use graphical entities such as Forms, Web Forms etc. 19 3. Web-Based Application  These are applications created for the World-Wide-Web or Internet.   These applications run from a Browser, but the actual program code for these applications (HTML) reside on a Web Server and are distributed to any client or browser which makes the request. Browser Web Page or Web Application 20 2.2.2 Creating Project Using the Integrated Development Environment (IDE)  The steps to using the IDE to program our applications as follows: Step 1: Open the Visual Studio IDE and invoke the Start Page: Step 2a: In the Start Page, select New Project Step 2b: In the New Project Dialog select Application Type a). In the Project Types box select: “Visual Basic Projects” b). In the Template box select: “Windows Applications” or “Console Application” c). Enter the project name into the “Name:” text box d). Set the project path or location in the “Location:” text box. You can also browse for the path using the “Browse” button. e). Click OK Step b – Windows Application Step a – Visual Basics Project type Step c – Name the Project Step d – Project Path or Location 21 Example of selecting Console Application: Console Application Step 3: The IDE Main Screen.  This screen is where you will create all your programs.  This window will vary depending on which type of application you have selected, Console Application, Window Application or other. Part I - Main Screen for a Console Application:  This screen is composed some basic Window items such as Title bar, Menu bar, Menu Toolbars and a Status Bar.  In addition some new components that will be important for creating applications, such as Document Window, Solution Explorer Window, and Properties Window.  A Console Application will automatically create a Module document in the Document Window. Mod ule Solution Explorer Property Window Document Window 22 Part I - Main Screen for a Windows Application:  This screen is composed some basic Window items such as Title bar, Menu bar, Menu Toolbars and a Status Bar, Document Window, Solution Explorer Window, and Properties Window  In addition some new components that will be important for creating Windows Applications, such as a Form Designer to create the Forms and User Interface. Also the Toolbox which contains the controls to create the UI. Form Designer Solution Explorer Toolbox Property Window Document Window Form Designer  This is the where you will design you Forms as a basis to your User Interface (UI) or Graphical User Interface (GUI).  When you begin a new Visual Basic project, a new form is automatically added to the project with the default name Form1 23Toolbox  The Toolbox is a palette that holds the control objects you will place on the Forms. Control Objects Solution Explorer  Displays the Files, Forms, Modules and Class Objects included in the project.  The Solution Explorer has two views: 1) Solution Explorer View - Files are displayed 2) Class View – Class Objects and their methods & events are shown Class View Solution Explorer View Property Window  This window is used to SET the Properties of the objects in your project. 24 You can view the Name of the property in the Name column, and set the property value in the Value Column.  Note that these objects include the controls, Forms, Class Objects and other objects used throughout your project. Property Value Property Name Document Window  Largest window in the center of the screen. Allows you to switch between the open documents.  For Console Applications, this window contains a Module & Code Editor.  For Windows Applications, this window contains the Form Designer & Code Editor. Document Window 25 Step 4: Set the Project Properties (StartUp Object)  So far we have talked about the Properties of Forms and Control Object. Now we focus on the properties of the Project itself.  In Visual Basic you can set properties for the entire project. Some of these properties you have already populated in the New Project Window in Step 2.  The Project Properties Window is invoked as follows:  In the Menu Bar select ProjectNameOfProject Properties to invoke the Project Property Page  Another Method uses the Solution Explorer. Simply Right-Click on the project name and select Properties from the context menu to invoke the Project Property Page Startup Form/Startup Object  There is one very important Project Property that must understand prior to writing code. That is the Startup Form or Startup Object.  The StartUp Form or Startup Object is the EXECUTION starting or entry point of a program.  A Visual Basic Application can be started using the following two options: I. FORM – (Default) this is the default method you are accustomed to, program starts via a FORM:  What this means is that when the project executes the Startup Form will display and control the flow of the program  Control or flow of program is done via a Form. In a standard Windows Application, this is by default automatically assigned to FORM1  I call this method of starting a Windows Application as a Form-Driven Application  For this method of executing the project, the property is named Startup Form  NOTE There is a checkbox for a property named Enable application framework in the property page. This property when checked, displays the startup option as Startup Form. This property is checked by default, so you don’t need to do anything, but if you uncheck this box, it will show as Startup Object (More on this below).  The figure below shows the default startup option as Startup Form: 26

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.