You are on page 1of 28

CMSY 190

Form Names
Each form has a Name property Programs refer to a form by this name VB assigns name Form1 Name property allows us to change form name Standard prefix is frm Each form also has a file name (.vb extension) Forms are stored on disk using this name Right click in Solution Explorer, and select Rename to change the file name
Slide 7- 2

Adding a New Form to a Project

Click Add New Item on the toolbar Or Project on menu, then Add Windows Form Add New Item dialog box appears
Click on Windows Form Change the default name Click the Add button New form now appears in: Design window Solution Explorer
Slide 7- 3

Switching from Forms to Form Code

Design window has two tabs for each form One for form design One for the code associated with a form If you have two forms

frmMain & frmError, you may select these tabs:

Error form design Error form code Main form design Main form code
Slide 7- 4

Creating an Instance of a Form

Dim statement creates an instance of a form
Dim ObjectVariable As New ClassName()

For example, to create an instance of frmError:

Dim errorForm As New frmError()

frmError is the form design name (the class) New frmError creates an instance of the form Variable errorForm refers to the form in RAM errorForm used to perform actions on the form

The form is not yet visible, but it now exists Show or ShowDialog makes the form visible

Slide 7- 5

Modal Forms & ShowDialog Method

A modal form prevents the user from changing focus

to another form in the application as long as it remains open For example:


frmError as shown in the previous slide The ShowDialog method displays the form instance named errorForm as a modal form Must close errorForm in order to change focus to

Variable errorForm represents an instance of

another form in the application

Slide 7- 6

Closing a Form
A form may close itself using the Close method and

referring to itself using the keyword "Me":

As in

Private Sub btnClose_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnClose.Click Me.Close() End Sub

Slide 7- 7

IsNumeric Function
This function accepts a string as an argument and returns

True if the string contains a number

Dim strNumber as String strNumber = 576 If IsNumeric(strNumber)then [statements] End if strNumber = 123abc If IsNumeric(strNumber)then [statements] End if

returns true

returns false

Use IsNumeric function to determine if a given string

contains numeric data

Slide 4- 8

Components of a Menu System

Each drop-down menu has a menu name Each drop-down menu has a list of actions or menu

commands that can be performed Some commands may lead to a submenu

Slide 7- 9

MenuStrip Control
A MenuStrip control adds a menu to a form Double-click on the MenuStrip icon in the Menus & Toolbars section of the Toolbox The MenuStrip control is displayed in the component

tray (bottom of Design window) A MenuStrip can have many ToolStripMenuItem objects:
Each represents a single menu command Name property - used by VB to identify it Text property text displayed to the user

Slide 7- 10

ToolStripMenuItem Object Names

Should begin with mnu
Then by convention are named based on their text

property and position in the menu hierarchy


mnuFilePrint mnuFileExit

Slide 7- 11

ToolStripMenuItem Text Properties

The text property holds the menu item description that

is displayed to the user If an access key is assigned, that letter must be preceded with an ampersand
Object Name Access Key Text Property

mnuFile mnuFileSave mnuFilePrint mnuFileExit


&File &Save &Print E&xit

Slide 7- 12

Menu Designer
The Menu Designer allows visual menu creation by

filling in boxes with the menu text:

Enter the next menu name

Enter first command in the File menu

Slide 7- 13

Components of a Menu System

Actions may be performed using a key or key combination called a shortcut key

Slide 7- 14

Shortcut Keys
Keyboard based shortcuts that execute menu

commands without using the menu system For example, ctrl-c to Copy to the clipboard These are set via the Shortcut property of each menu item A shortcut is displayed to the user only if the ShowShortcut property is set to true

Slide 7- 15

ToolStripMenuItem Click Events

Menus and submenus require no code Commands must have a click event procedure
Double click on the menu item Event procedure created in the code window Programmer supplies the code to execute

Double click the menu item object

mnuFileExit, then add a Me.Close command as shown below:

Programmer supplied code Click event procedure created by VB

Private Sub mnuFileExit_Click(ByVal sender as System.Object, _ ByVal e as System.EventArgs) Handles mnuFileExit.Click

Me.Close() End Sub
Slide 7- 16

ListBox Items.Add Method

Items can be added to the end of a ListBox list in your

VB code using the Add method Format is


ListBox is the name of the control Item is a string value to add to the Items property Example:

Slide 5- 17

String Concatenation
We often need to combine two or more strings into a

longer one This operation is called Concatenation Concatenation is signaled by the '&' operator in the same way addition is signaled by a '+ Assume the user has entered their name into the TextBox txtName Listbox lstGreeting can say, Hello to any name found in the TextBox
lstGreeting.Items.Add("Hello " &

Appends user name in txtName.Text to Hello and

writes the result in the lstGreeting listbox

Slide 3- 18

A Full List of Conversion Functions

There are conversion functions for each data type CBool ( expr ) CByte ( expr ) CChar ( expr ) CInt ( expr ) CLng ( expr ) CObj ( expr )

CDate ( expr )
CDbl ( expr ) CDec ( expr )

CShort ( expr )
CSng ( expr ) CStr ( expr )

Slide 3- 19

Creating Files with StreamWriter Objects

Add Imports System.IO before class declared Makes StreamWriter classes available in code A StreamWriter object is used to create a sequential

text file in the following way:

Declare an object variable of type StreamWriter
Dim phoneFile As System.IO.StreamWriter

Object is assigned to a StreamWriter variable

Variable phoneFile now defines a stream of data that

can be written to phonelist.txt

Slide 9- 20

Appending Text with StreamWriter

A StreamWriter object is used to append data to a

sequential text file in the following way:

Declare an object variable of type StreamWriter Dim phoneFile As StreamWriter Call AppendText method passing the filename
phoneFile = File.AppendText(phonelist.txt)

Method returns a StreamWriter object Object is assigned to a StreamWriter variable

Variable phoneFile now defines a stream of data that

can be added to the end of phonelist.txt

Slide 9- 21

Appending to a File
When opening an existing file with AppendText Existing contents are retained New text adds on to the end of the old text Note: If the file does not exist, a new file will be created i.e., If adding a new number to phoneList, use:
phoneList = File.AppendText(PhoneNum.txt")

Slide 9- 22

Writing Data to a File

The WriteLine method of the StreamWriter class writes a line of data to a file. The following is the general format of the method: ObjectVar.WriteLine(Data) ObjectVar is the name of a StreamWriter object variable. Data represents constants or variables whose contents will be written to the file.

If writing a line of data to the phoneFile, use code similar to: phoneFile.WriteLine(PhoneNum.ToString)

Closing a StreamWriter Object

Should close files when finished with them Avoids losing data Data is initially written to a buffer Writes unsaved data from the buffer to the file The Close method of a StreamWriter object clears the

buffer and closes the file

Streamwriter object identified by ObjectVar

Slide 9- 24

StreamReader Objects
Use StreamReader objects to read from a file

Define and open similar to StreamWriter:

Dim ObjectVar As StreamReader ObjectVar = File.OpenText(Filename)

Sample code:
Dim phoneFile As StreamReader phoneFile = File.OpenText(phonelist.txt")

Variable phoneFile now defines a stream of data that

can be read from phonelist.txt Must have Imports System.IO before class declaration as was done with StreamWriter
Slide 9- 25

Detecting the End of a File

The Peek method tests if youve reached end of file

(no more characters to read)

Format is objectvar.Peek If no more characters, the value -1 is returned
Dim scoresFile As StreamReader Dim strInput As String scoresFile = File.OpenText("Scores.txt") Do Until scoresFile.Peek = -1 strInput = scoresFile.ReadLine() lstResults.Items.Add(strInput) Loop scoresFile.Close()

Slide 9- 26

Determining Whether a File Exists

The File.OpenText method issues a runtime error if

the file does not exist Avoid this by using the File.Exists method
Format is File.Exists(filename) Returns a boolean result that can be tested:
If File.Exists(filename) Then ' Open the file. inputFile = File.OpenText(filename) Else MessageBox.Show(filename & " does not exist.") End If

Slide 9- 27

ReadLine Method
The ReadLine in the StreamReader class reads a line of data from a file. The general format of the method is as follows: ObjectVar.ReadLine() Dim inputFile As StreamReader Dim intAnswer as Integer If File.Exists(Example.txt) Then 'Open the file inputFile = File.OpenText(Example.txt) Do While inputFile.Peek <> -1 intAnswer = CInt(intNum.ReadLine()) lstOutput.Items.Add(The answer is & intAnswer.ToString) Loop

'Close the file. inputfile.Close()

Else MessageBox.Show("That file does not exist") End If
Slide 9- 28