Title: CSCI1406
1Lecture 15
CSCI1406 Introduction to Programming with Visual
Basic Dr Peter Yardley
2- Examples using the various programming ideas that
we have met. - Manipulating images
- Sorting
- Try Catch Clause
-
3(No Transcript)
4 Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As _ System.EventArgs)
Handles Button1.Click PictureBox1.Image
Lighter(PictureBox1.Image) ' Make Lighter End
Sub Private Sub Button2_Click(ByVal sender As
System.Object, ByVal e As _ System.EventArgs)
Handles Button2.Click PictureBox1.Image
Darker(PictureBox1.Image) ' Make Darker End
Sub Private Sub Button3_Click(ByVal sender As
System.Object, ByVal e As _ System.EventArgs)
Handles Button3.Click PictureBox1.Image
RotateRight(PictureBox1.Image) 'Rotate Right End
Sub Private Sub Button4_Click(ByVal sender As
System.Object, ByVal e As _ System.EventArgs)
Handles Button4.Click PictureBox1.Image
UpsideDown(PictureBox1.Image) 'upside down End
Sub
5Private Function Lighter(ByVal imgA As Bitmap) As
Bitmap Dim c As Integer, d As Integer
Dim intRed As Integer, intGreen As Integer,
intBlue As Integer Dim colMyColor1 As
Color, colMyColor2 As Color Dim intChange
As Integer 20 For c 0 To imgA.Width -
1 For d 0 To imgA.Height - 1
colMyColor1 imgA.GetPixel(c, d)
intRed colMyColor1.R
intGreen colMyColor1.G intBlue
colMyColor1.B If intRed lt 255 -
intChange Then intRed intChange Else intRed
255 If intGreen lt 255 - intChange
Then intGreen intChange Else intGreen 255
If intBlue lt 255 - intChange Then
intBlue intChange Else intBlue 255
colMyColor2 Color.FromArgb(intRed,
intGreen, intBlue)
imgA.SetPixel(c, d, colMyColor2) Next
d Next c Lighter imgA End
Function
6Private Function Darker(ByVal imgA As Bitmap) As
Bitmap Dim c As Integer, d As Integer
Dim intRed As Integer, intGreen As Integer,
intBlue As Integer Dim colMyColor1 As
Color, colMyColor2 As Color Dim intChange
As Integer 20 For c 0 To imgA.Width -
1 For d 0 To imgA.Height - 1
colMyColor1 imgA.GetPixel(c, d)
intRed colMyColor1.R
intGreen colMyColor1.G intBlue
colMyColor1.B If intRed gt
intChange Then intRed - intChange Else intRed
0 If intGreen gt intChange Then
intGreen - intChange Else intGreen 0
If intBlue gt intChange Then intBlue -
intChange Else intBlue 0
colMyColor2 Color.FromArgb(intRed, intGreen,
intBlue) imgA.SetPixel(c, d,
colMyColor2) Next d Next c
Darker imgA End Function
7Private Function RotateRight(ByVal imgA As
Bitmap) As _ Bitmap Dim c As
Integer, d As Integer Dim colMyColor1 As
Color Dim imgB As Bitmap imgB
New Bitmap(imgA.Height, imgA.Width) For c
0 To imgA.Width - 1 For d 0 To
imgA.Height - 1 imgB.SetPixel(d,
c, imgA.GetPixel(c, d)) Next d
Next c RotateRight imgB End Function
8Private Function UpsideDown(ByVal imgA As Bitmap)
As Bitmap Dim c As Integer, d As Integer
Dim colMyColor1 As Color Dim imgB
As Bitmap imgB New Bitmap(imgA.Width,
imgA.Height) For c 0 To imgA.Width - 1
For d 0 To imgA.Height - 1
imgB.SetPixel(c, d, imgA.GetPixel(c,
imgA.Height - 1 - d)) Next d
Next c UpsideDown imgB End Function
9Click Sort, loads the left-hand text box and
sorts alphabetically and displays in the right.
10Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
_ Handles Button1.Click Dim myStr()
As String "banana", "melon", "apple", "peach",
"lemon" Dim c As Integer, d As Integer
Dim strTemp As String For c 0 To
myStr.Length - 1 TextBox1.Text
myStr(c) vbCrLf Next c For c 0
To myStr.Length - 1 For d 0 To
myStr.Length - 2 If myStr(d) gt
myStr(d 1) Then strTemp
myStr(d) myStr(d) myStr(d 1)
myStr(d 1) strTemp
End If Next d Next c For c
0 To myStr.Length - 1 TextBox2.Text
myStr(c) vbCrLf Next c End Sub
11Type a number in and see what happens.
12Dim intMarks(4) As Byte Dim intNumberOfMarks As
Integer 0 Private Sub Button1_Click(ByVal
sender As System.Object, _ ByVal e As
System.EventArgs) Handles Button1.Click
Try TextBox1.Focus()
intMarks(intNumberOfMarks) CByte(TextBox1.Text)
TextBox1.Text ""
intNumberOfMarks 1 Catch ex As
System.InvalidCastException ' input
xyz Label1.Text ex.tostring
MsgBox("Invalid cast Exception")
13 Catch ex As System.OverflowException
'input 999 Label1.Text
ex.tostring MsgBox("Invalid cast
Exception") Catch ex As IndexOutOfRangeExce
ption ' input more that 5 elements
Label1.Text ex.tostring
MsgBox("Index out of range exception")
Catch ex As Exception ' some other
error Label1.Text ex.tostring
MsgBox("Some unknown exception happened")
Finally ' this block is always
executed whether an exception is thrown or not
If intNumberOfMarks intMarks.Length -
1 Then Application.Exit() End Try End
Sub
14Press button to copy file.
15Private Sub Button1_Click(ByVal sender As
System.Object, _ ByVal e As System.EventArgs)
Handles Button1.Click Try
FileCopy(TextBox1.Text, TextBox2.Text)
MsgBox("File copied without error")
Catch ex As System.ArgumentException
' null input or output MsgBox("Source
or destination invalid") Catch ex As
System.io.FileNotFoundException '
input file does not exist
MsgBox("File does not exist") Catch ex As
System.IO.DirectoryNotFoundException
'directory for output file does not exist
MsgBox("Directory does not exist")
Catch ex As Exception ' any other
error MsgBox("Unspecified
exception") End Try End Sub