vb test 4 review
TRANSCRIPT
-
7/30/2019 Vb Test 4 Review
1/28
CMSY 190
-
7/30/2019 Vb Test 4 Review
2/28
Slide 7- 2
Form Names Each form has aName property
Programs refer to a form by this name
VB assigns name Form1
Name property allows usto 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 tochange the file name
-
7/30/2019 Vb Test 4 Review
3/28
Slide 7- 3
Adding a New Form to a Project ClickAdd New Item on the toolbar
Or Project on menu, thenAdd Windows Form
Add New Item dialog box appears Click on Windows Form
Change the default name
Click theAddbutton
New form now appears in:
Design window
Solution Explorer
-
7/30/2019 Vb Test 4 Review
4/28
Slide 7- 4
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 formsfrmMain & frmError, youmay select these tabs: Error form design
Error form code
Main form design
Main form code
-
7/30/2019 Vb Test 4 Review
5/28
Slide 7- 5
Creating an Instance of a Form
Dim statement creates an instance of a form
For example, to create an instance of frmError:
frmError is the form design name (the class)
NewfrmError creates an instance of the form
Variable errorFormrefers to the form in RAM
errorFormused to perform actions on the form The form is not yet visible, but it now exists
Show or ShowDialog makes the form visible
DimObjectVariableAs New ClassName()
Dim errorForm As New frmError()
-
7/30/2019 Vb Test 4 Review
6/28
Slide 7- 6
Modal Forms & ShowDialog
MethodAmodal form prevents the user from changing focus
to another form in the application as long as it remainsopen
For example:
Variable errorFormrepresents an instance offrmError as shown in the previous slide
The ShowDialog method displays the form instancenamed errorFormas a modal form
Must close errorFormin order to change focus toanother form in the application
errorForm.ShowDialog()
-
7/30/2019 Vb Test 4 Review
7/28Slide 7- 7
Closing a FormA form may close itself using the Close method and
referring to itself using the keyword "Me":
As inMe.Close()
Private Sub btnClose_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _Handles btnClose.Click
Me.Close()
End Sub
-
7/30/2019 Vb Test 4 Review
8/28Slide 4- 8
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 returns true[statements]
End if
strNumber = 123abc
If IsNumeric(strNumber)then returns false
[statements]End if
Use IsNumeric function to determine if a given stringcontains numeric data
-
7/30/2019 Vb Test 4 Review
9/28Slide 7- 9
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
-
7/30/2019 Vb Test 4 Review
10/28Slide 7- 10
MenuStrip ControlAMenuStrip control adds a menu to a form
Double-click on theMenuStrip icon in theMenus &Toolbars section of the Toolbox
TheMenuStrip controlis displayed in the componenttray (bottom of Design window)
A MenuStrip can have manyToolStripMenuItemobjects: Each represents a single menu command
Name property - used by VB to identify it Text property text displayed to the user
-
7/30/2019 Vb Test 4 Review
11/28Slide 7- 11
ToolStripMenuItem Object Names Should begin with mnu
Then by convention are named based on their textproperty and position in the menu hierarchy
mnuFile
mnuFileSave
mnuFilePrint
mnuFileExit
-
7/30/2019 Vb Test 4 Review
12/28
Slide 7- 12
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 precededwith an ampersand
Object Name Access Key Text Property
mnuFile F &File
mnuFileSave S &SavemnuFilePrint P &PrintmnuFileExit X E&xit
-
7/30/2019 Vb Test 4 Review
13/28
Slide 7- 13
Menu Designer TheMenu Designerallows visual menu creation by
filling in boxes with the menu text:
Enter firstcommand inthe File menu
Enter thenext menuname
-
7/30/2019 Vb Test 4 Review
14/28
Slide 7- 14
Components of a Menu System
Actions may be performed using a key or keycombination called a shortcut key
-
7/30/2019 Vb Test 4 Review
15/28
Slide 7- 15
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 menuitem
A shortcut is displayed to the user only if theShowShortcut property is set to true
-
7/30/2019 Vb Test 4 Review
16/28
Slide 7- 16
ToolStripMenuItem Click Events
Menus and submenus require no codeCommands 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 objectmnuFileExit, then add a Me.Close command asshown below:
Private Sub mnuFileExit_Click(ByVal sender as System.Object, _ByVal e as System.EventArgs) Handles mnuFileExit.Click
Me.Close()
End Sub
Programmer supplied code
Click event procedure created by VB
-
7/30/2019 Vb Test 4 Review
17/28
Slide 5- 17
ListBox Items.Add Method Items can be added to the end of a ListBox list in your
VB code using theAdd method
Format isListBox.Items.Add(Item)
ListBox is the name of the control
Item is a string value to add to the Items property
Example:lstStudents.Items.Add("Sharon")
-
7/30/2019 Vb Test 4 Review
18/28
Slide 3- 18
String ConcatenationWe often need to combine two or more strings into a
longer one This operation is called Concatenation
Concatenation is signaled by the '&' operator in thesame way addition is signaled by a '+
Assume the user has entered their name into theTextBox txtName
Listbox lstGreeting can say, Hello to any name foundin the TextBox
lstGreeting.Items.Add("Hello " &txtName.Text)
Appends user name in txtName.Text to Hello andwrites the result in the lstGreeting listbox
-
7/30/2019 Vb Test 4 Review
19/28
Slide 3- 19
A Full List of Conversion Functions There are conversion functions for each data type
CBool ( expr)
CByte ( expr)
CChar ( expr)
CDate ( expr)
CDbl ( expr)
CDec ( expr)
CInt ( expr)
CLng ( expr)
CObj ( expr)
CShort ( expr)
CSng ( expr)
CStr ( expr)
-
7/30/2019 Vb Test 4 Review
20/28
Slide 9- 20
Creating Files with StreamWriter Objects
Add Imports System.IObefore class declared Makes StreamWriter classes available in code
AStreamWriterobject is used to create a sequential
text file in the following way: Declare an object variable of type StreamWriter
Object is assigned to a StreamWriter variable
VariablephoneFile now defines a stream of data thatcan be written tophonelist.txt
DimphoneFileAs System.IO.StreamWriter
-
7/30/2019 Vb Test 4 Review
21/28
Slide 9- 21
Appending Text with StreamWriter
AStreamWriterobject is used to append data to asequential text file in the following way: Declare an object variable of type StreamWriter
CallAppendTextmethod passing the filename
Method returns a StreamWriter object
Object is assigned to a StreamWriter variable
VariablephoneFile now defines a stream of data thatcan be added to the end ofphonelist.txt
DimphoneFileAs StreamWriter
phoneFile = File.AppendText(phonelist.txt)
-
7/30/2019 Vb Test 4 Review
22/28
Slide 9- 22
Appending to a FileWhen opening an existing file withAppendText
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 tophoneList, use:
phoneList = File.AppendText(PhoneNum.txt")
-
7/30/2019 Vb Test 4 Review
23/28
Writing Data to a FileThe WriteLine method of the StreamWriter class writes
a line of data to a file. The following is the generalformat of the method: ObjectVar.WriteLine(Data)
ObjectVaris the name of a StreamWriter object variable.Data represents constants or variables whose contentswill be written to the file.
If writing a line of data to the phoneFile, use code similarto: phoneFile.WriteLine(PhoneNum.ToString)
-
7/30/2019 Vb Test 4 Review
24/28
Slide 9- 24
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 StreamWriterobject clears thebuffer and closes the file
ObjectVar.Close() Streamwriter object identified byObjectVar
-
7/30/2019 Vb Test 4 Review
25/28
Slide 9- 25
StreamReader Objects Use StreamReaderobjects to read from a file
Define and open similar to StreamWriter:
Sample code:
VariablephoneFile now defines a stream of data thatcan be read fromphonelist.txt
Must have Imports System.IObefore classdeclaration as was done with StreamWriter
DimObjectVarAs StreamReader
ObjectVar =File.OpenText(Filename)
Dim phoneFile As StreamReader
phoneFile = File.OpenText(phonelist.txt")
-
7/30/2019 Vb Test 4 Review
26/28
Slide 9- 26
Detecting the End of a File The Peekmethod 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()
-
7/30/2019 Vb Test 4 Review
27/28
Slide 9- 27
Determining Whether a File Exists
The File.OpenText method issues a runtime error ifthe file does not exist
Avoid this by using the File.Exists method
Format isFile.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
-
7/30/2019 Vb Test 4 Review
28/28
Slide 9 28
ReadLine MethodThe 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 StreamReaderDim 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