Title: Array Sort: synchronized sort
1Array Sort synchronized sort
2Array Synchronized Sort
Public Class Form2 Dim fruit(2) As String
Dim price(2) As Decimal Private Sub
Form2_Load() Handles MyBase.Load
fruit(0) "Banana" 'load arrays
fruit(1) "Apple" fruit(2) "Orange"
price(0) 0.39 price(1) 0.67
price(2) 0.52 Dim i As Short
'display arrays in listbox1 For i 0 To
2 ListBox1.Items.Add(fruit(i) " "
price(i)) Next End Sub
3Array Synchronized Sort
Private Sub Button1_Click() Handles
Button1.Click Array.Sort(fruit, price)
'synchronized sort Dim i As Short
For i 0 To 2 ListBox2.Items.Add(fr
uit(i) " " price(i)) Next End Sub
4- Learning Objectives
- Menu and context menu
- Open and close files
- Common dialog controls
- Exceptions handling
-
5Menu
- Function to organize controls
- Important properties
- Text
- Checked (Boolean) put a check mark in front of
text
- Context Menu
- Menu associated with controls
6(No Transcript)
7 Private Sub mnuRed_Click() Handles
mnuRed.Click TextBox1.BackColor
Color.Red End Sub
Private Sub mnuVisible_Click() Handles
mnuVisible.Click mnuVisible.Checked
Not mnuVisible.Checked If
mnuVisible.Checked Then
TextBox1.Visible True Else
TextBox1.Visible False End If End
Sub
8How to Open Files
Fileopen(1, amyfile1.txt, OpenMode.Input)
Fileopen(2, amyfile2.txt, OpenMode.Output)
Previous content will be erased.
Fileopen(3, amyfile3.txt, OpenMode.Append)
FileClose (1) FileClose (2) FileClose (3)
close files
9Write Writeline
Dim st as string Dim num as integer Fileopen(1,
amyfile.txt, OpenMode.Output) st AMIS
532 num 48 Write(1, st)
Writeline(1, num) move to a new line
after num is written Write(1, st) FileClose(1)
AMIS 532, 48 AMIS 532
10Input and LineInput
Dim st as string FileOpen (1, atest1.txt,
OpenMode.Input) While Not EOF(1) Input(1,
st) ListBox1.Items.Add(st) End
While FileClose(1)
ListBox1
apple orange peach
apple, orange, peach
test1.txt
11Common Dialog Controls
access to operating systems functions such as
open files, save files, print files, specify
fonts, and colors.
VB project
Common dialog controls
Windows operating system
12The common dialog controls
- OpenFileDialog
- SaveFileDialog
- FontDialog
- ColorDialog
- PrintDialog
13OpenFileDialog
- property Filename
- method ShowDialog
- e.g.,
- If OpenFileDialog1.ShowDialog ltgt
DialogResult.Cancel then - FileOpen(1, OpenFileDialog1.Filename,
OpenMode.Input) - End if
Public Function ShowDialog( ) as DialogResult
14Common Dialog Control Demo
15OpenFileDialog Code Example
Private Sub LoadList_Click() Handles
Button1.Click If OpenFileDialog1.ShowDialo
g ltgt DialogResult.Cancel Then
FileOpen(1, OpenFileDialog1.FileName,
OpenMode.Input) End If Dim st as
String While Not EOF(1)
Input(1, st) ListBox1.Items.Add(st)
End While FileClose(1) End
Sub
16FontDialog Code Example
Private Sub ChangeFont_Click() Handles
Button2.Click If FontDialog1.ShowDialog
ltgt DialogResult.Cancel Then
ListBox1.Font FontDialog1.Font End
If End Sub
17Exception Handling
- Exceptions abnormal conditions
- Examples of exceptions file not found,
division by zero, disk full, disk not ready, out
of memory, - Unhandled exceptions lead to abrupt termination
of programs without warnings and explanations. - The goals of exception handling are to
anticipate potential problems and provide
opportunities to address the problems when they
occur.
18Try/Catch Blocks
Try statements that may cause
error Catch what to do if an error
occurs Finally do this regardless of whether
error occurs or not End Try
19A Simple Example
Private Sub Button1_Click() Handles
Button1.Click Dim x, y, z As Short
x 1000 y 1000 Try
z x y Catch
MessageBox.Show("Error has occurred", "Error")
End Try End Sub
20Catch As
Exception hierarchy
Catch ex as Exception create ex as an object of
Exception class
Exception
Message property
ArithmeticException
IOException
DataException
OverflowException
DiskNotReadyException
MissingPrimaryKeyException
DivisonByZeroException
FileNotFoundException
21Catch As example
Private Sub Button1_Click() Handles
Button1.Click Dim x, y, z As Short
x 1000 y 1000 Try
z x y Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
End Try End Sub
22Another example
Private Sub Openfile_Click() Handles
Button1.Click Try
FileOpen(1, "a\version.txt", OpenMode.Input)
Catch ex As Exception
MessageBox.Show(ex.Message, "error")
Exit Sub End Try Me.Text "Open
file successful" End Sub
If the user selects Try Again, the program
flows back to the FileOpen statement.
23Multiple Catch Clauses
Catch err as ArithmeticException what to
do Catch err as Exception most general
exception what to do
Sequential search for error Only the block that
first catches an error executes.
24Incorrect Catch Clauses
- Catch err as Exception most general
exception - what to do
- Catch err as ArithmeticException more specific
exception - what to do
The Catch err as ArithmeticException block will
never execute.
25Some Exception Classes