HCS 207 ADVANCED PROGRAMMING (VB.NET) LECTURER: C RUVINGA (0775 936 673)

COURSE WORK 30% (2 ASSIGNMENTS (PRACTICAL) AND 2 TESTS) FINAL EXAM 70%

Compulsory to attend lecturers a register will be marked in every lecture.

Familiarize with e-learning portal (Notes, Assignments, Course Work marks, Any questions)

Course Outline

  1. Introduction to VB.NET
  2. Working with Windows Forms
  3. Creating windows controls
  4. Declaring variables, Expressions and Statements
  5. Creating Conditional Statements
  6. Creating Loops
  7. Declaring arrays
  8. Defining Sub routines and Functions
  9. Understanding Databases
  10. Debugging your application
  11. Using classes
  12. Creating components
  13. Creating Graphics
  14. Providing Input and Output
  15. Packaging your application

REFERENCES: E-BOOKS, VB TEXT BOOKS, INTERNETModule is Practical therefore there is need to practice and research more information on your own.

1.  INTRODUCTION TO VB.NET

INTRODUCTION

Several Versions are available 2010, 2008,2005

·  Installation of Visual Studio 2008 (Practical session)

Visual Studio 2008 is a suite of products that includes the .NET Framework and the Integrated Development Environment (IDE)

The .NET Framework supports four programming languages : Visual Basic, Visual C#, Visual C++, Visual J#

The Visual Studio IDE is your interface to .NET -- it is used to develop applications in any of the supported programming languages

Provides a common set of services that can be used when programming in any supported language

Enables you to write programs that run on any operating system on any hardware platform

Main components …

·  .NET Framework Class Library (FCL)

o  Contains files of pre-written code organized as classes

o  The classes themselves are organized (grouped) into Java-like packages called namespaces

o  The groupings are done according to the functionality of the classes

o  There are many classes and many namespaces

·  Common Language Runtime (CLR)

The CLR manages the execution of .NET programs (called managed code)

Coordinates essential functions …

·  Memory management

·  Code execution

·  Security

·  Other services

Common Type System (CTS) is a component of the CLR that ensures that all .NET applications use the same basic data types regardless of the language they are coded in

The IDE is the interface between the programmer and the .NET tools he or she uses

n  Includes design components for Console, Web, and Windows development (to name a few)

n  Includes an editor for all .NET languages as well as XML and HTML

n  Includes a comprehensive set of tools for forms design and code organization

·  Methods

·  Attributes (aka properties or data stores or …)

*******************************VIEW VB.NET IDE***********************************

Compiling & Running a VB Application

n  You will create a project made up of many files

n  The project will be contained within a solution

n  The compiler builds (translates) your VB source code into an intermediate language (MSIL) à

n  The result of this translation is packaged into a special container called an Assembly.

n  An Assembly is an (almost) ready to execute file with either a .dll or .exe extension

n  The Assembly is run by the CLR

Visual Studio supports the creation of a number of different types of Applications à

n  Each different type of Application has a different structure and automatically provides support for different applications code

n  Types of Applications we will work with in this course

n  Console Applications

n  Windows Applications

n  Web-based, Client-Server Applications

****************************View types of applications ***************************

Visual Studio Solutions and Projects

n  A solution is a container for all your work on an application

n  A solution contains several folders that define an application’s structure

n  Solution files have a file suffix of .sln

n  A solution contains one or more projects

n  The project file is used to create an executable application

n  A project file has a suffix of .vbproj

Project properties are set using the project's property pages

Dot Net Architecture with multiple platform use optional

Applications

Several kinds of applications: desktop and server-side

·  Desktop (CIS 3309)

o  Console (desktop) – batch oriented tasks; display messages through console window

§  Can run thru Windows Explorer or DOS commands

§  Uses system class named Console (in mscorlib)

o  Windows Forms

§  Rich user interface – loads of controls

§  Looks like an e-commerce page

§  Games?

·  Server-side (CIS 4309, 4344)

o  ASP.NET – for creation of server-side apps to communicate with client apps across network using HTTP (Hypertext Transfer Protocol)

Why use .net

·  Move away from Windows 32 bit API and COM-based APIs – a Microsoft decision

·  Integrated Development Environment (IDE) –

o  Much improved for designing, writing and debugging software

o  Platform neutrality / language neutrality

o  Managed language support

·  Framework Class Library

·  Mechanisms for code distribution and deployment

·  Improved security

·  Better execution performance

·  Far better software engineering capabilities across all aspects of the development process

·  Easier to create distributed applications across vendor and platform boundaries

·  Easier to build distributed applications in larger networked environments such as the Internet

·  Take advantage of various class libraries facilitating targeting to lightweight and special devices such as pocket PCs, cell phones, tablet PCs, etc

·  Interoperability with older code – staged conversion

Creating a new project

Click File - Select New Project (Ctrl N)

New Project window appears (Select template Windows Application)

Save Project

Parts of a project

Toolbox – contains controls necessary to transform a form into an interactive screen

Properties Window – contains a set of properties that define characteristics or behavior of a control

Solution explorer – contains all elements of a project

Code window – actual writing of code

Hello world application

Open new project

Draw label on form

Change the text property of label to Hello VB.Net

Change font, font color

Press F5 to run the application

2  WORKING WITH WINDOWS FORMS

2.1  Create a new form

When you open project

Click Project – Add windows form

2.2  Add control to form

Drag and drop onto form

2.3  Set control properties

Highlight form/ control

Use the properties window

Name- unique identifier (should reflect nature of name e.g. FName)

Text – text that appears on control

Tab Index – sets order in which controls receives focus when tab key is pressed

Location upper left corner coordinates of control position

2.4  Resize Control

Adjust size handles to corresponding size

2.5  Create a menu

Drag and Drop MenuStrip control – creates menus and sub menus these menus can be linked to forms

menuItem – adds menu item, double clicking menu item displays subroutine that is called when the menu item is selected

Combo box – inserts combo box contol

Separator – draws lines between previous and next menu

Textbox – inserts text box control

2.6  Create a dialog box

A dialog box is a pop up box used to display and receive information from the application user, has command buttons

Click project, add new item , select dialog, it appears on another tab, you can add controls to that dialog box e.g. label Hello

Opened by calling ShowDialog() function

Run F5

2.7  Set the tab key order

Tab is Alternative to using the mouse, it moves from one input control to another

Focus – were the cursor appears or highlights

Tab index determines reception order of inputs by setting tab index value beginning from 0, in order to skip tab set the Tab stop property to false

Example add 3 textboxes and set tab index values to 0,1,2 respectively and also set the t tab stop property of the middle text box to false

2.8  Create a message box

Dialog box used to inform application user

VB.net creates the msg box but you must type message in parenthesis

e.g. MessageBox.Show(“Invalid Password”)

If (Messagebox.Show(“Do you really want to exit”, “Confirm exit”,MessageBoxButtons.YesNo)= Dialogresult.Yes) Then Application.Exit()

End If

2.9  Write code for an event

VB.Net is an event driven application

Event is a user action

Left window – consists of list of objects on form Right Window – events


CREATING WINDOWS CONTROLS: LABELS,TEXTBOXES AND LISTS

Add a text box control to a form

Textbox is used to accept one line of input in the form of letters, numbers, etc

Drag and drop textbox onto form

Multiple line input- select arrow in the upper right corner of text box, pop up menu appears select multiline text box

e.g. Draw interface with one Button and one text box write code highlighted

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MessageBox.Show(TextBox1.Text)

End Sub

End Class

Access a text box control

Refers to entering input into textbox or assigning input into text box

e.g. textbox1.text = “myself ” - placing text into textbox

Read only property – prevents editing of text when set to True

Set Defaults of a textbox control

Properties commonly changed

MaxLength – sets max number of characters that can be entered in a text field

Multiline Textalign – False for single line and True for multiple lines

Cursor – I beam is default

Tab Index

Tab Stop

Add a label control to a form

Displays text on a form

Can be set during design and at run time e.g. Label1.Width() = 7, Label1.text = “me”

Add a list box Control

Displays a list of items that can be selected by the user

Selection Mode – allows user to select one or multiple items from list

Horizontal scroll bar –

Items –

Set defaults for a list box Control

Multi Simple – enables user to select one or more Items

Selection mode – how many items the user can select from list None –idsplays list but does NOT allow user to select, One – enables selection of one

Multi extended – enables user to use the Shift, Ctrl and arrow key to select items from the list

Multi column – divides control into 2 or more columns each column displays separate but related info, the complete row is selected when user selects Item

Sorted – order True to alphabetic order false data entry order

Tab stop – if false then set tab index property and if false

Tab Index-

Format string - Numeric currency date, time

Write items to a List box

Items Property

Code ListBox1.items.Add(“myself”)

Code to remove item from list ListBox1.Items.RemoveAt(X(number of item it starts counting from 0,1,…))

Code remove All ListBox1.items.Cear()

Access an Item selected from the List box

1. Reference selected index property of list box control (0,1,2)

-1 if no item was selected from Listbox c0ntrol ListBox1.Selected index = 1

2. reference the selected text property ListBox1.text = “me”

If Listbox1.Text = “me” then MessageBox.Show(“You selected me”)

SelectedIndexchanged () Subroutine

Add a combo box control

Combination of textbox and listbox

Drag and Drop

Combo1.Items.Add(“”) – control.property.method

DropdownStyle - ???

Access an Item selected from a combo box Control

1.  reference the combo box text, combobox1.text

convert String to date CDate(ComboBox1.Text).

CDate() is a function that converts string to date data type

4.CREATING WINDOWS CONTROLS :BUTTONS, RADIO BUTTONS, AND CHECK BOXES

Add a Button Control

Drag and drop it onto form,

Avoid having too many buttons on form

Enable – prevents button control from being accidentally pushed by setting the enable property to false

Alternatively: Button1.Enabled = false

Write a Button control Event

Each button control event is associated with a sub routine(block of code that executes when subroutine is called(when event occurs))

Change the label of a Button Control

Change the default text property from Button1 to relevant text e.g. Exit, cancel, Submit

Font –

Text Align -

Using an image for a Button control

1.Import an image File using the properties window and image property

2.Write code that assigns image filename to image property

MyImage is an image object that has the fromFile() method to link the image file name to the image object and then to the image property.

Dim MyImage As Image

Button1.Image = Myimage.FroFile (“myimage.bmp”)

Image Align property- used to position image within the button

You can display both text and an image on a button butset the text Align and image Align properties to avoid overlapping

Make a button the Default Button

Buttons are used to activate an action

Submit button is normally associated with the enter key and cancel withEsc, when this is done you are making the submit button the default button

This is achieved by setting the Forms AcceptButton property to the button name and form’s Cancelbutton property to the name of the property

Add a radio Button

Is a control that displays a button and a label alongside button

The label describes a valid option

User chooses option by clicking the radio button e.g. gender

Related radio buttons are organized into a radio button control group(only one radio button in a control grp can be selected)

Make a default radio button by setting the checked property to true

Access a radio Button

You must write code that examines the checked property, normally its written to the submit button

Use the If then Else If statement e.g.

If rbMale.Checked = True then messagebox.Show(“you selected male”)

Else if rbFemale.Checked =True then messagebox.Show(“you selected female”)

End If

Add a check Box

Displays checkbox(used to select/ deselect) and a label(specifies an option)

You can select multiple check boxes but not multiple radio buttons

Setting the check property to true makes the check box the default

It has 3 possible states Checked, unchecked and indeterminate (Check state property)

Appearance property alters the check box control into e.g. a button

Flat style property- flat, pop up, standard and system