visual basic學習筆記
DESCRIPTION
Visual BasicTRANSCRIPT
第 1頁/共 85頁
Visual Basic
學習檔案
老師:張銘棋
學生:黃新雅
座號:32
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 2頁/共 85頁
目 錄
第四章 表單的程式設計
4-1 溫度轉換 -----------------------------------------p6
4-2 國旗轉換 -----------------------------------------p7
第五章 使用資料
5-1 顯示時間與日期 --------------------------------p8
5-2 顯示編碼 ----------------------------------------p9
5-3 簡易計算機 --------------------------------------p10
第六章 流程控制 選擇性執行程式
6-1 計算折扣 ------------------------------------------p12
6-2 猜數字遊戲 ---------------------------------------p13
6-3 數字比大小 ---------------------------------------p15
6-4 美餓美小吃店-點餐程式 -----------------------p16
第七章 流程控制 重複執行同一段程式
7-1 數字沙漏 ------------------------------------------p17
7-2 蟑螂進行曲 ---------------------------------------p18
7-3 電子時鐘 ------------------------------------------p19
7-4 統計成績 ------------------------------------------p20
7-5 數字金字塔 ---------------------------------------p21
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 3頁/共 85頁
第八章 陣列
8-1 成績登錄 ------------------------------------------p22
8-2 主控台資料輸入 ---------------------------------p23
8-3 查詢成績 ------------------------------------------p24
8-4 計算所得稅金額 ---------------------------------p25
8-5 設定字型1 ----------------------------------------p26
8-6 資料互換 ------------------------------------------p27
8-7 設定字型2 ----------------------------------------p28
8-8 歌唱大賽排名 ------------------------------------p29
8-9 移動人名 ------------------------------------------p30
8-10 排隊系統 ------------------------------------------p31
第九章 程序與函數
9-1 計算階層 -----------------------------------------p34
9-2 等加速度運動 -----------------------------------p34
9-3 傳值呼叫 -----------------------------------------p35
9-4 圓盤張數 -----------------------------------------p36
9-5 擲骰子遊戲 --------------------------------------p37
9-6 自由落體距離計算 -----------------------------p38
第十章 事件驅動程式設計
10-1 英文字母一律大寫&千分位&滑鼠移入背景變白
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 4頁/共 85頁
p39
第十一章 繪圖與多媒體
11-1繪圖---------------------------------------------------p40
11-2 亂數圓------------------------------------------------p41
11-3 任意縮放
11-4 黑白矩形+畫出指定大小&顏色的圓-----------p42
11-5 MediaPalyer控制項-------------------------------p43
第十二章 視窗介面進階
12-1 功能表&記事本&小算盤-------------------------p47
12-2 長度、重量轉換+變換顏色+顯示字型大小----p49
第十三章 檔案存取
13-1 讀取檔案------------------------------------------p50
13-2 讀取檔案 2---------------------------------------p53
13-3 菜單點餐管理 -----------------------------------p56
13-4 成績輸入 -----------------------------------------p57
13-5 檔案總管 -----------------------------------------p59
第十四章 設計資料庫程式
14-1 書籍資料庫 ------------------------------------p60
14-2 書籍資料庫 2 ---------------------------------p61
14-3 書籍資料庫 3 ---------------------------------p62
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 5頁/共 85頁
14-4 書籍資料庫 4 ---------------------------------p64
14-5 書籍詳細資料 --------------------------------p65
14-6 成衣資料庫 -----------------------------------p66
14-7 成衣資料庫 2 ---------------------------------p67
第十五章 EXAM
15-1 加減乘除計算機 -----------------------------p68
15-2 計算 BMI值 ----------------------------------p69
15-3 薪資計算--------------------------------------p71
15-4 計算費氏函數--------------------------------p72
15-5 輾轉相除法球最大公因數與最小公倍數 p73
15-6 骰子比大小------------------------------------p73
15-7 輸入英文字元一律小寫---------------------p74
15-8 中心十字圓------------------------------------p76
15-9 音樂播放器------------------------------------p78
15-10 檔案總管 ------------------------------------p82
15-11 便利貼 ---------------------------------------p83
15-12 求互質&畫數字三角形&數字部份加總&身分證
字號驗算 -----------------------------------------p83
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 6頁/共 85頁
第四章
4-1 溫度轉換
程式碼:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim c, f
f = Val(TextBox1.Text)
c = f * 9 / 5 + 32
Label1.Text = ("攝氏 " & f & " 度 = 華氏 " & c & "度。")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button2.Click
Dim c, f
f = Val(TextBox1.Text)
c = (f - 32) * 5 / 9
c = Math.Round(c, 2)
Label1.Text = ("華氏 " & f & " 度 = 攝氏 " & c & "度。")
End Sub
Private Sub Label1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Label1.Click
End Sub
Private Sub 溫度轉換_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
End Sub
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 7頁/共 85頁
End Class
4-2 國旗轉換
程式碼:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
PictureBox1.Size = New Size(PictureBox1.Size.Width + 10, PictureBox1.Size.Height + 10)
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button7.Click, Button11.Click
PictureBox1.Image = New Bitmap("..\..\fCanada2.gif")
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button10.Click
PictureBox1.Image = New Bitmap("..\..\fusa2.gif")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button3.Click
Me.BackgroundImage = New Bitmap("..\..\fBg1.gif")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button2.Click
PictureBox1.Size = New Size(PictureBox1.Size.Width - 10, PictureBox1.Size.Height - 10)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button4.Click
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 8頁/共 85頁
Me.BackgroundImage = New Bitmap("..\..\fBg2.gif")
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button5.Click
Me.BackgroundImage = New Bitmap("..\..\fBg3.gif")
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles PictureBox1.Click
End Sub
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button12.Click
PictureBox1.Image = New Bitmap("..\..\ffrance2.gif")
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button9.Click
PictureBox1.Image = New Bitmap("..\..\fjapan2.gif")
End Sub
End Class
第五章
5-1 顯示時間與日期
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 9頁/共 85頁
程式碼:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Label1.Text = "按鈕時間為" & Now
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
End Sub
End Class
5-4 顯示編碼
程式碼:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim a
a = AscW(TextBox1.Text & ChrW(0))
Label1.Text = "Unicode:" & a & " (十六進位: " & Hex(a) & ")"
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
End Sub
End Class
5-5 簡易計算機
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 10頁/共 85頁
程 式碼:
Dim a As String
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button10.Click
TextBox1.Text = TextBox1.Text & "0"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
TextBox1.Text = TextBox1.Text & "1"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
TextBox1.Text = TextBox1.Text & "2"
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
TextBox1.Text = TextBox1.Text & "3"
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button4.Click
TextBox1.Text = TextBox1.Text & "4"
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button5.Click
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 11頁/共 85頁
TextBox1.Text = TextBox1.Text & "5"
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button6.Click
TextBox1.Text = TextBox1.Text & "6"
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button7.Click
TextBox1.Text = TextBox1.Text & "7"
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button8.Click
TextBox1.Text = TextBox1.Text & "8"
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button9.Click
TextBox1.Text = TextBox1.Text & "9"
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button11.Click
TextBox1.Text = TextBox1.Text & "."
End Sub
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button16.Click
a = "/" & Val(TextBox1.Text)
TextBox1.Text = ""
End Sub
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button13.Click
a = "+" & Val(TextBox1.Text)
TextBox1.Text = ""
End Sub
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button14.Click
a = "-" & Val(TextBox1.Text)
TextBox1.Text = ""
End Sub
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button12.Click
If a.Substring(0, 1) = "+" Then TextBox1.Text = Mid(a, 2, Len(a) - 1) + Val(TextBox1.Text)
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 12頁/共 85頁
If a.Substring(0, 1) = "-" Then TextBox1.Text = Mid(a, 2, Len(a) - 1) - Val(TextBox1.Text)
If a.Substring(0, 1) = "x" Then TextBox1.Text = Mid(a, 2, Len(a) - 1) * Val(TextBox1.Text)
If a.Substring(0, 1) = "/" Then TextBox1.Text = Mid(a, 2, Len(a) - 1) / Val(TextBox1.Text)
End Sub
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button15.Click
a = "*" & Val(TextBox1.Text)
TextBox1.Text = ""
End Sub
End Class
第六章
6-1 計算折扣
程式 碼:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim buy As Integer, pay As Integer
Dim msg As String
buy = Val(TextBox1.Text)
If buy > 1000 Then
pay = buy * 0.8
If buy > 2000 Then
msg = "超過2000元,先打8折再減100元"
pay = pay - 100
Else
msg = "超過1000元,先打8折再減50元"
pay = pay - 50
End If
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 13頁/共 85頁
Else
If buy > 500 Then
msg = "金額超過500元打9折"
pay = buy * 0.9
Else
msg = "金額未超過500打95折"
pay = buy * 0.95
End If
End If
TextBox2.Text = pay
Label3.Text = msg
TextBox1.Focus()
TextBox1.SelectAll()
End Sub
End Sub
End Class
6-2 猜數字遊戲
程式碼:
Dim min, max, ans, guess, count As Byte
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
If TextBox1.Text = "" Or Val(TextBox1.Text) > 100 Then
MsgBox("請輸入一個1到100的整數", 0 + 16, "錯誤")
Else
guess = Val(TextBox1.Text)
If guess = ans Then
vMin.Text = "請猜一個介於" & min & "到" & max & "之間的整數"
count += 1
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 14頁/共 85頁
Label2.Text = "您目前已經猜了" & count & "次"
End If
If guess < ans Then
min = guess
vMin.Text = "請猜一個介於" & min & "到" & max & "之間的整數"
count += 1
Label2.Text = "您目前已經猜了" & count & "次"
Else
max = guess
vMin.Text = "請猜一個介於" & min & "到" & max & "之間的整數"
count += 1
Label2.Text = "您目前已經猜了" & count & "次"
End If
End If
TextBox1.Focus()
TextBox1.SelectAll()
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Label2.Click
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
min = 1
max = 100
vMin.Text = "請猜一個介於" & min & "到" & max & "之間的整數"
ans = CInt(Rnd() * 100 + 1)
'MsgBox(ans)
End Sub
End Class
6-3 數字比大小
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 15頁/共 85頁
程式碼:
Dim a, b, c As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
c = Val(TextBox3.Text)
If a > b And a > c Then
Label2.Text = "最大值是" & a & ""
Else
If b > a And b > c Then
Label2.Text = "最大值是" & b & ""
Else
If c > a and c > b Then
Label2.Text = "最大值是" & c & ""
End If
End If
End If
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
End Sub
End Class
6-3 美餓美小吃店-點餐程式
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 16頁/共 85頁
程式碼:
Dim a, b, c, d As Integer
Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Label7.Click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
a = Val(TextBox1.Text) * 30
b = Val(TextBox2.Text) * 70
c = Val(TextBox3.Text) * 65
d = Val(TextBox4.Text) * 99
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RadioButton2.CheckedChanged
End Sub
Private Sub ex2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
End Sub
End Class
第七章
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 17頁/共 85頁
7-1 數字沙漏
程式碼:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Dim i As Integer
For i = 25 To 1 Step -4
Label1.Text = Label1.Text & vbCrLf & StrDup(i, CStr(i \ 4 + 1))
Next
For i = 5 To 25 Step 4
Label1.Text = Label1.Text & vbCrLf & StrDup(i, CStr(i \ 4 + 1))
Next
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Label1.Click
End Sub
End Class
7-2 蟑螂進行曲
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 18頁/共 85頁
程式碼:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
play.Click
play.Visible = False
roach.Left = 0 - roach.Size.Width
roach.Visible = True
Do
roach.Left = roach.Left + 1
If roach.Left Mod 10 = 0 Then
roach.Top = roach.Top + CInt(Int(7 * Rnd())) - 3
End If
System.Threading.Thread.Sleep(5)
Application.DoEvents()
Loop Until (roach.Left > Me.ClientSize.Width)
play.Visible = True
roach.Visible = False
End Sub
End Class
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 19頁/共 85頁
7-3 電子時鐘
程式碼:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Timer1.Tick
Label2.Text = Format(Now(), _
If(CheckBox1.Checked, "hh:mm:ss", "HH:mm:ss"))
Label3.Text = Format(Now(), "yyyy/MM/dd")
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Label2.Text = Format(Now(), "HH:mm:ss")
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles CheckBox2.CheckedChanged
Timer1.Enabled = Not CheckBox2.Checked
End Sub
End Class
7-4 統計成績
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 20頁/共 85頁
程式碼:
Dim n, pass, fail, total As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
If Val(TextBox1.Text) > 100 Or Val(TextBox1.Text) < -2 Then
MsgBox("請輸入0~100之間的分數,未輸入表示0分,輸入-1表示結束。")
TextBox1.Text = ""
Else
If Val(TextBox1.Text) = -1 Then
MsgBox("共" & n & "人," & pass & "人及格," & fail & "人不及格,平均" & Format(total
/ n, "#.#") & "分。", 0 + 64, "結果")
End
Else
If Val(TextBox1.Text) < 60 Then
fail += 1
Else
pass += 1
End If
total = total + Val(TextBox1.Text)
n = n + 1
TextBox1.Text = ""
Label3.Text = n + 1
TextBox1.Focus()
End If
End If
End Sub
End Class
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 21頁/共 85頁
7-5 數字金字塔
程式碼:
Private Sub ex2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
For i As Integer = 1 To 10
Label1.Text = Label1.Text & vbCrLf
For j As Integer = 1 To i
Label1.Text = Label1.Text & j
Next j
Next i
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Label1.Click
End Sub
End Class
第八章
8-1 成績登錄
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 22頁/共 85頁
程式碼:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim 總分 As Double
Dim 成績陣列() As String = Split(TextBox1.Text, ",")
Dim 筆數 = UBound(成績陣列) + 1
For Each s In 成績陣列
總分 = 總分 + Val(s)
Next
MsgBox("成績筆數:" & 筆數 & vbCrLf & "總成績為:" & 總分 & vbCrLf & "平均成績:" & _
Math.Round(總分 / 筆數, 2))
End Sub
End Class
8-2 主控台資料輸入
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 23頁/共 85頁
程式碼:
Module Form3
Sub Main()
Dim 資料, 陣列(0) As String
Dim 筆數 As Integer
'不斷讓使用者輸入資料並儲存,直到輸入空白字串為止
Do
Console.Write("請數入資料:")
資料 = Console.ReadLine()
If 資料 = "" Then Exit Do
ReDim Preserve 陣列(筆數)
陣列(筆數) = 資料
筆數 = 筆數 + 1
Loop
Console.WriteLine("您一共輸入了" & 筆數 & "筆資料 。")
Array.Sort(陣列)
For i = 1 To 筆數
Console.WriteLine(陣列(i - 1))
Next
Console.ReadLine()
End Sub
End Module
8-3 查詢成績
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 24頁/共 85頁
程式碼:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim 姓名() = {"陳桶一", "黃光權", "胡生妙", "王為全", _
"李日正", "劉德菖", "方正一", "劉康寶", _
"謝掬花", "王美蘭", "徐小噹", "葉小毛"}
Dim 數學() = {90, 58, 41, 100, 59, 28, 100, 0, 100, 41, 91, 0}
Dim 國文() = {76, 77, 14, 97, 66, 11, 98, 0, 74, 46, 99, 10}
Dim 英文() = {98, 75, 33, 87, 57, 33, 100, 0, 89, 49, 84, 0}
Dim 索引 = Array.IndexOf(姓名, TextBox1.Text)
If 索引 < 0 Then
Label5.Text = "查無此人!"
Label6.Text = ""
Label7.Text = ""
Else
Label5.Text = 數學(索引)
Label6.Text = 國文(索引)
Label7.Text = 英文(索引)
End If
Dim math100$, name$, i%
i = Array.IndexOf(數學, 100)
While i > 0
name = 姓名(i)
math100 = math100 + " " + 姓名(i)
i = Array.IndexOf(數學, 100, i + 1)
End While
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 25頁/共 85頁
MsgBox(math100, , "數學100分")
End Sub
End Class
8-4 計算所得稅金額
程式碼:
Public Class Form4
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim 稅率(,) As Integer = _
{{410000, 6, 0}, {1090000, 13, 28700}, {2180000, 21, 115900}, {4090000, 30, 312100},
{4090001, 40, 721100}}
Dim 所得淨額 As Integer = Val(TextBox1.Text)
Dim i As Integer
For i = 0 To UBound(稅率, 1) - 1
If 所得淨額 <= 稅率(i, 0) Then
Exit For
End If
Next
Label3.Text = 所得淨額 & " * " & _
稅率(i, 1) & "%" & " - " & _
稅率(i, 2) & " - " & _
(所得淨額 * 稅率(i, 1) * 0.01 - 稅率(i, 2))
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Label1.Click
End Sub
End Class
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 26頁/共 85頁
8-5 設定字型 1
程式碼:
Imports System.Drawing
Public Class Form5
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
If ListBox1.SelectedIndex = -1 Then _
ListBox1.SelectedItem = Label1.Font.Name
If ListBox2.SelectedIndex = -1 Then _
ListBox2.SelectedIndex = 0
If ListBox3.SelectedIndex = -1 Then
ListBox3.SelectedItem = "12"
End If
Dim 字型名稱 As String = ListBox1.SelectedItem
Dim 字型樣式 As FontStyle
Dim 字型大小 As Integer = Val(ListBox3.SelectedItem)
Select Case ListBox2.SelectedItem
Case "標準"
字型樣式 = FontStyle.Regular
Case "粗體"
字型樣式 = FontStyle.Bold
Case "斜體"
字型樣式 = FontStyle.Italic
Case "粗斜體"
字型樣式 = FontStyle.Bold Or FontStyle.Italic
End Select
Label1.Font = New Font(字型名稱, 字型大小, 字型樣式)
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 27頁/共 85頁
End Sub
End Class
8-6 資料互換
程式碼:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim i As Integer
For i = 0 To ListBox1.SelectedItems.Count - 1
ListBox2.Items.Add(ListBox1.SelectedItems(i))
Next
For i = ListBox1.SelectedIndices.Count - 1 To 0 Step -1
ListBox1.Items.RemoveAt(ListBox1.SelectedIndices(i))
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
Dim i As Integer
For i = 0 To ListBox2.SelectedItems.Count - 1
ListBox1.Items.Add(ListBox2.SelectedItems(i))
Next
For i = ListBox2.SelectedIndices.Count - 1 To 0 Step -1
ListBox2.Items.RemoveAt(ListBox2.SelectedIndices(i))
Next
End Sub
End Class
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 28頁/共 85頁
8-7 設定字型 2
程式碼:
Imports System.Drawing
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
If ComboBox1.Text = "" Then ComboBox1.Text = Label1.Font.Name()
If ComboBox2.Text = "" Then ComboBox2.SelectedIndex = 0
If ComboBox3.Text = "" Then ComboBox3.Text = 12
Dim 字型名稱 As String = ComboBox1.Text
Dim 字型樣式 As FontStyle
Dim 字型大小 As Integer = Val(ComboBox3.Text)
Select Case ComboBox2.Text
Case "標準"
字型樣式 = FontStyle.Regular
Case "粗體"
字型樣式 = FontStyle.Bold
Case "斜體"
字型樣式 = FontStyle.Italic
Case "粗斜體"
字型樣式 = FontStyle.Bold Or FontStyle.Italic
End Select
Label1.Font = New Font(字型名稱, 字型大小, 字型樣式)
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 29頁/共 85頁
End Sub
End Class
8-8 歌唱大賽排名
程式碼:
Public Class EX1
Private Sub EX1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim name() = {"吳國民", "李得勝", "蔡可申", "陳夢嬌", "方十全"}
Dim sum(4) As Short
Dim score(,) = {{77, 78, 78, 84, 84, 79, 77, 76, 76, 81}, _
{89, 89, 92, 88, 91, 87, 86, 90, 85, 89}, _
{89, 82, 87, 83, 82, 89, 86, 86, 84, 80}, _
{86, 86, 90, 89, 81, 86, 90, 85, 87, 86}, _
{89, 88, 87, 88, 86, 84, 86, 93, 84, 87}}
Dim min, max As Short
For i = 0 To 4
min = score(i, 0)
max = score(i, 0)
sum(i) = score(i, 0)
For j = 1 To 9
If score(i, j) < min Then
min = score(i, j)
End If
If score(i, j) > max Then
max = score(i, j)
End If
sum(i) = sum(i) + score(i, j)
Next
sum(i) = sum(i) - min - max
Next
Array.Sort(sum, name)
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 30頁/共 85頁
For i = 4 To 0 Step -1
Label1.Text = Label1.Text & name(i) & " " & sum(i) / 8 & vbCrLf
Next
End Sub
End Class
8-9 移動人名
程式碼:
Public Class EX2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim i%
If ListBox1.SelectedIndex = 0 Then '已經移到最上面了(第0筆)
ListBox1.Items.Insert(ListBox1.Items.Count, ListBox1.Items(0)) '新增在最後一筆
ListBox1.Items.RemoveAt(0) '刪掉第一筆
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
Else
i = ListBox1.SelectedIndex
If i = -1 Then
MsgBox("尚未選取資料項目!", , "注意")
Else
ListBox1.Items.Insert(i - 1, ListBox1.SelectedItem)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex())
ListBox1.SelectedIndex = i - 1
End If
End If
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 31頁/共 85頁
Label1.Text = ListBox1.SelectedIndex
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button2.Click
Dim i%
If ListBox1.SelectedIndex = ListBox1.Items.Count - 1 Then '已經移到最下面了(第n-1筆)
ListBox1.Items.Insert(0, ListBox1.Items(ListBox1.Items.Count - 1)) '新增在最前面
ListBox1.Items.RemoveAt(ListBox1.Items.Count - 1) '刪掉最後一筆
ListBox1.SelectedIndex = 0
Else
i = ListBox1.SelectedIndex
If i = -1 Then
MsgBox("尚未選取資料項目!", , "注意")
Else
ListBox1.Items.Insert(i + 2, ListBox1.SelectedItem)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex())
ListBox1.SelectedIndex = i + 1
End If
End If
Label1.Text = ListBox1.SelectedIndex
End Sub
Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles
ListBox1.Click
Label1.Text = ListBox1.SelectedIndex
End Sub
End Class
8-10 排隊系統
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 32頁/共 85頁
程式碼:
Public Class EX3
Dim count%
Private Sub EX3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
TextBox1.Focus()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click '加入項目,直接加在末端即可
If TextBox1.Text = "" Then
MsgBox("尚未輸入資料", 0 + 16, "錯誤")
Else
count += 1
ListBox1.Items.Add(Format(count, "00#") & "-" & TextBox1.Text)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
'修改項目,亦即插入一筆在指定位置之前,再刪除該指定位置之資料
If ListBox1.SelectedIndex = -1 Then
MsgBox("尚未選取清單方塊任何一個項目", 0 + 16, "錯誤")
Else
ListBox1.Items.Insert(ListBox1.SelectedIndex(), Mid(ListBox1.SelectedItem, 1, 3) & "-" &
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 33頁/共 85頁
TextBox1.Text)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex())
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
Dim i As Integer
' 逐一將 ListBox1 被選取的選項加入於 ListBox2
For i = 0 To ListBox1.SelectedItems.Count - 1
ListBox2.Items.Add(ListBox1.SelectedItems(i))
Next
' 逐一刪除 ListBox1 被選取的選項
For i = ListBox1.SelectedIndices.Count - 1 To 0 Step -1
ListBox1.Items.RemoveAt(ListBox1.SelectedIndices(i))
Next
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button4.Click
Dim i As Integer
'逐一將 ListBox2 被選取的選項加入於 ListBox1
For i = 0 To ListBox2.SelectedItems.Count - 1
ListBox1.Items.Add(ListBox2.SelectedItems(i))
Next
'逐一刪除 ListBox2 被選取的選項
For i = ListBox2.SelectedIndices.Count - 1 To 0 Step -1
ListBox2.Items.RemoveAt(ListBox2.SelectedIndices(i))
Next
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button5.Click
ListBox2.Items.Clear()
End Sub
End Class
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 34頁/共 85頁
第九章
9-1 計算階層
程式碼:
Sub Main()
Fact(5)
End Sub
Sub Fact(ByVal n As Integer)
Dim total As Double = 1
For i = 2 To n
total *= i
Next
Console.WriteLine(n & "!的值為" & total)
End Sub
End Module
9-2 等加速度運動
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 35頁/共 85頁
程式碼:
Sub Main()
Finalv(10, 15, 3)
Console.WriteLine("----------")
Finalv(10, 20)
Console.WriteLine("----------")
Finalv(10)
End Sub
Sub Finalv(ByVal seconds As Single, _
Optional ByVal V0 As Single = 0, _
Optional ByVal Accelaration As Single = 9.8)
Console.WriteLine("初速" & V0 & ", 加速度" & Accelaration & _
", 經" & seconds & "秒後,")
'速度計算公式 v=v0+at
Dim Velocity = V0 + Accelaration * seconds
Console.WriteLine("速度為" & Velocity)
End Sub
End Module
9-3 傳值呼叫
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 36頁/共 85頁
程式碼:
Sub DoubleArrayValue(ByVal b() As Integer)
For i = 0 To b.Length - 1
b(i) *= 2
Next
End Sub
Sub Main()
Dim a() As Integer = {1, 2, 3, 4}
DoubleArrayValue(a)
For i = 0 To a.Length - 1
Console.WriteLine(a(i))
Next
End Sub
End Module
9-4 圓盤張數
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 37頁/共 85頁
程式碼:
Module Module4
Dim number As Integer '移動次數
Sub Main()
Dim p
Dim n As Integer = 0
number = 0
Console.Write("圓盤的張數 = ")
n = Console.ReadLine()
p = Timer
aa(n, "A", "B", "C")
Console.WriteLine("花了" & Timer - p & "秒")
Console.ReadLine()
End Sub
Sub aa(ByVal n, ByVal a, ByVal b, ByVal c)
If n > 0 Then
' 把 n-1 張圓盤從 a 移到 b
aa(n - 1, a, c, b)
' 把最下面的圓盤從 a 移到 c
number += 1
Console.WriteLine("{0} : {1} --> {2}", number, a, c)
' 把 n-1 張圓盤從 b 移到 c
aa(n - 1, b, a, c)
End If
End Sub
End Module
9-5 擲骰子遊戲
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 38頁/共 85頁
程式碼:
Public Class EX1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Randomize() '這很重要,以時間來當作亂數種子,否則每次亂數產生的數列都會一樣
Label1.Text = Int(Rnd() * 6) + 1
Label2.Text = Int(Rnd() * 6) + 1
PictureBox1.Image = New Bitmap("..\..\" & Label1.Text & ".gif")
PictureBox2.Image = New Bitmap("..\..\" & Label2.Text & ".gif")
End Sub
End Class
9-5 自由落體距離計算
程式碼:
Public Class EX2
Function freefall(ByVal t) As Single
Return 1 / 2 * 9.8 * t ^ 2
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Label3.Text = "自由落體掉落距離" & freefall(Val(TextBox1.Text)) & "米"
End Sub
End Class
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 39頁/共 85頁
第十章
10-1 轉換大小寫
程式碼:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case e.KeyChar
Case "a" To "z"
TextBox1.AppendText(Chr(Asc(e.KeyChar) - 32))
e.Handled = True
End Select
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
End Sub
Private Sub Form1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.MouseEnter
Me.BackColor = Color.White
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 40頁/共 85頁
End Sub
Private Sub Form1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.MouseLeave
Me.BackColor = SystemColors.Control
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim a$, c$, i%
a = TextBox2.Text
If Len(a) > 3 Then
If Len(a) Mod 3 <> 0 Then
c = Mid(a, 1, Len(a) Mod 3)
For i = Len(a) Mod 3 + 1 To Len(a) Step 3
c = c + "," + Mid(a, i, 3)
Next
End If
End If
第十一章
11-1 繪圖
程式碼:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim g = e.Graphics
g.DrawRectangle(Pens.Yellow, 10, 10, 40, 60)
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 41頁/共 85頁
Dim bigpen = New Pen(Color.Pink, 3)
g.DrawRectangle(bigpen, 60, 10, 40, 60)
Dim biggerpen = New Pen(Color.Gold, 9)
g.DrawRectangle(biggerpen, 110, 10, 40, 60)
g.DrawLine(Pens.Black, 10, 80, 80, 160)
g.DrawLine(Pens.Red, New Point(80, 80), New Point(10, 160))
g.DrawLine(bigpen, 260, 100, 290, 125)
g.DrawLine(bigpen, 290, 125, 280, 160)
g.DrawLine(bigpen, 280, 160, 240, 160)
g.DrawLine(bigpen, 240, 160, 230, 125)
g.DrawLine(bigpen, 230, 125, 260, 100)
g.DrawEllipse(Pens.DarkBlue, 170, 10, 180, 50)
End Sub
End Class
11-2 亂數圓
程式碼:
Private Sub Form2_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim width = Me.ClientSize.Width / 10
Dim height = Me.ClientSize.Height / 10
Dim mybrushes() = {Brushes.Red, Brushes.Orange, _
Brushes.Yellow, Brushes.Blue, _
Brushes.LightPink, Brushes.LightBlue, _
Brushes.Purple, Brushes.Green}
Randomize()
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 42頁/共 85頁
For i = 0 To 9
For j = 0 To 9
Dim index = Fix(Rnd() * 8)
Dim size = Fix(Rnd() * width) + 1
Dim rect = New Rectangle(i * width + index, _
j * height + index, _
size, size)
e.Graphics.FillEllipse(mybrushes(index), rect)
Next
Next
End Sub
Private Sub Form2_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.SizeChanged
Me.Invalidate()
End Sub
End Class
11-3 任意縮放
程式碼:
Dim img As Image
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Try
img = Image.FromFile("..\..\sample.jpg")
Catch ex As Exception
MsgBox(ex.Message)
End
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 43頁/共 85頁
End Try
End Sub
Private Sub Form3_MouseClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
If e.Button = Windows.Forms.MouseButtons.Left Then
img.RotateFlip(RotateFlipType.Rotate270FlipNone)
ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
img.RotateFlip(RotateFlipType.Rotate90FlipNone)
End If
Me.Invalidate()
End Sub
Private Sub Form3_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawImage(img, 0, 0, Me.ClientSize.Width, _
Me.ClientSize.Height)
End Sub
Private Sub Form3_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.SizeChanged
Me.Invalidate()
End Sub
End Class
第十二章
12-1 功能表&記事本&小算盤
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 44頁/共 85頁
程式碼:
Public Class Form1
Dim x As Integer
Private Sub 放大ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 放大ToolStripMenuItem.Click
Me.Width *= 1.2
Me.Height *= 1.2
End Sub
Private Sub 縮小ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 縮小ToolStripMenuItem.Click
Me.Width *= 0.8
Me.Height *= 0.8
End Sub
Private Sub 還原ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 還原ToolStripMenuItem.Click
Me.Width = 300
Me.Height = 300
End Sub
Private Sub ToolStripMenuItem3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripMenuItem3.Click
x = 200
Me.Invalidate()
ToolStripMenuItem4.Checked = False
ToolStripMenuItem5.Checked = False
End Sub
Private Sub ToolStripMenuItem4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripMenuItem4.Click
x = 300
Me.Invalidate()
ToolStripMenuItem3.Checked = False
ToolStripMenuItem5.Checked = False
End Sub
Private Sub ToolStripMenuItem5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripMenuItem5.Click
x = 400
Me.Invalidate()
ToolStripMenuItem5.Checked = False
ToolStripMenuItem5.Checked = False
End Sub
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 45頁/共 85頁
Private Sub ToolStripMenuItem4_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles ToolStripMenuItem4.Paint
Dim g = e.Graphics
g.Clear(BackColor)
g.FillEllipse(Brushes.Black, 10, 60, x, x)
End Sub
Private Sub REDToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles REDToolStripMenuItem.Click
Me.BackColor = Color.Red
Me.Invalidate()
End Sub
Private Sub GREENToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles GREENToolStripMenuItem.Click
Me.BackColor = Color.Green
Me.Invalidate()
End Sub
Private Sub BLUEToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BLUEToolStripMenuItem.Click
Me.BackColor = Color.Blue
Me.Invalidate()
End Sub
Private Sub 一般ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 一般ToolStripMenuItem.Click
Label1.Font = New Font(Label1.Font, FontStyle.Regular)
End Sub
Private Sub 粗體ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 粗體ToolStripMenuItem.Click
Label1.Font = New Font(Label1.Font, FontStyle.Bold)
End Sub
Private Sub 斜體ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 斜體ToolStripMenuItem.Click
Label1.Font = New Font(Label1.Font, FontStyle.Italic)
End Sub
Private Sub 粗斜體ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 粗斜體ToolStripMenuItem.Click
Label1.Font = New Font(Label1.Font, FontStyle.Italic Or FontStyle.Bold)
End Sub
Private Sub ToolStripMenuItem6_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripMenuItem6.Click
Label1.Font = New Font(Label1.Font.Name, 14, Label1.Font.Style)
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 46頁/共 85頁
End Sub
Private Sub ToolStripMenuItem7_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripMenuItem7.Click
Label1.Font = New Font(Label1.Font.Name, 18, Label1.Font.Style)
End Sub
Private Sub ToolStripMenuItem8_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripMenuItem8.Click
Label1.Font = New Font(Label1.Font.Name, 22, Label1.Font.Style)
End Sub
Private Sub 新細明體ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 新細明體ToolStripMenuItem.Click
Label1.Font = New Font("新細明體", Label1.Font.Size, Label1.Font.Style)
End Sub
Private Sub 標楷體ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 標楷體ToolStripMenuItem.Click
Label1.Font = New Font("標楷體", Label1.Font.Size, Label1.Font.Style)
End Sub
Private Sub ToolStripLabel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles ToolStripLabel1.Click
Shell("c:\windows\notepad.exe", AppWinStyle.NormalFocus)
End Sub
Private Sub ToolStripLabel2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles ToolStripLabel2.Click
Shell("C:\WINDOWS\system32\calc.exe", AppWinStyle.NormalFocus)
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripButton1.Click
Label1.ForeColor = Color.Orange
End Sub
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripButton2.Click
Label1.ForeColor = Color.Magenta
End Sub
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripButton3.Click
Label1.ForeColor = Color.DeepSkyBlue
End Sub
Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
End Sub
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 47頁/共 85頁
End Class
12-2 長度、重量轉換+變換顏色+顯示字型大小
程式碼:
Public Class ex1
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripButton1.Click
Label2.ForeColor = Color.Khaki
Label3.ForeColor = Color.Khaki
Label4.ForeColor = Color.Khaki
Label5.ForeColor = Color.Khaki
Label6.ForeColor = Color.Khaki
Label7.ForeColor = Color.Khaki
End Sub
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripButton2.Click
Label2.ForeColor = Color.GreenYellow
Label3.ForeColor = Color.GreenYellow
Label4.ForeColor = Color.GreenYellow
Label5.ForeColor = Color.GreenYellow
Label6.ForeColor = Color.GreenYellow
Label7.ForeColor = Color.GreenYellow
End Sub
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripButton3.Click
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 48頁/共 85頁
Label2.ForeColor = Color.MediumTurquoise
Label3.ForeColor = Color.MediumTurquoise
Label4.ForeColor = Color.MediumTurquoise
Label5.ForeColor = Color.MediumTurquoise
Label6.ForeColor = Color.MediumTurquoise
Label7.ForeColor = Color.MediumTurquoise
End Sub
Private Sub 轉換成英尺ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 轉換成英尺ToolStripMenuItem.Click
Dim x, y As String
x = Val(TextBox1.Text)
y = x * 3.2808
TextBox2.Text = y
End Sub
Private Sub ToolStripMenuItem6_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripMenuItem6.Click
44: Label2.Font = New Font(Label2.Font.Name, 14, Label2.Font.Style)
Label3.Font = New Font(Label3.Font.Name, 14, Label3.Font.Style)
Label4.Font = New Font(Label4.Font.Name, 14, Label4.Font.Style)
Label5.Font = New Font(Label5.Font.Name, 14, Label5.Font.Style)
Label6.Font = New Font(Label6.Font.Name, 14, Label6.Font.Style)
Label7.Font = New Font(Label7.Font.Name, 14, Label7.Font.Style)
End Sub
Private Sub ToolStripMenuItem7_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripMenuItem7.Click
Label2.Font = New Font(Label2.Font.Name, 18, Label2.Font.Style)
Label3.Font = New Font(Label3.Font.Name, 18, Label3.Font.Style)
Label4.Font = New Font(Label4.Font.Name, 18, Label4.Font.Style)
Label5.Font = New Font(Label5.Font.Name, 18, Label5.Font.Style)
Label6.Font = New Font(Label6.Font.Name, 18, Label6.Font.Style)
Label7.Font = New Font(Label7.Font.Name, 18, Label7.Font.Style)
End Sub
Private Sub ToolStripMenuItem8_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripMenuItem8.Click
Label2.Font = New Font(Label2.Font.Name, 20, Label2.Font.Style)
Label3.Font = New Font(Label3.Font.Name, 20, Label3.Font.Style)
Label4.Font = New Font(Label4.Font.Name, 20, Label4.Font.Style)
Label5.Font = New Font(Label5.Font.Name, 20, Label5.Font.Style)
Label6.Font = New Font(Label6.Font.Name, 20, Label6.Font.Style)
Label7.Font = New Font(Label7.Font.Name, 20, Label7.Font.Style)
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 49頁/共 85頁
End Sub
Private Sub 英尺公尺ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 英尺公尺ToolStripMenuItem.Click
Dim x, y As String
y = Val(TextBox2.Text)
x = y * 0.3048
TextBox1.Text = x
End Sub
Private Sub 轉換成公尺ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 轉換成公尺ToolStripMenuItem.Click
Dim x, y As String
x = Val(TextBox3.Text)
y = x * 0.4535924
TextBox4.Text = y
End Sub
Private Sub 英鎊公斤ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 英鎊公斤ToolStripMenuItem.Click
Dim x, y As String
y = Val(TextBox4.Text)
x = y / 0.4535924
TextBox3.Text = x
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox3.TextChanged
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
End Sub
End Class
第十三章
13-1 讀取檔案
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 50頁/共 85頁
程式碼:
Public Class Form1
Dim filechanged As Boolean = False
Private Sub 開啟舊檔ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 開啟舊檔ToolStripMenuItem.Click
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
End Class
13-2 讀取檔案 2
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 51頁/共 85頁
程式碼:
Public Class Form2
Dim writer As System.IO.StreamWriter
Sub Logging(ByVal Message As String)
writer.WriteLine(Now & "," & Message)
End Sub
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 52頁/共 85頁
Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Not (writer Is Nothing) Then
Logging("程式啟動")
writer.Close()
End If
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Try
writer = My.Computer.FileSystem.OpenTextFileWriter("d:\LogFile.log", True)
Logging("程式啟動")
Catch ex As Exception
MsgBox("無法開啟記錄檔," & ex.Message)
Me.Close()
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim LineNum As Integer = 1
Try
Dim reader =
My.Computer.FileSystem.OpenTextFileReader(OpenFileDialog1.FileName)
While reader.EndOfStream = False
Dim singleLine = reader.ReadLine()
TextBox1.Text &= LineNum.ToString("D3") & " " & singleLine & vbCrLf
LineNum += 1
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Try
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text,
False)
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 53頁/共 85頁
Logging("儲存檔案:" & SaveFileDialog1.FileName)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
End Class
13-3 菜單點餐管理
程式碼:
Public Class Form3
Dim menufilepath = "D:\ch13\menu.csv"
Dim menuitems As New ArrayList
Dim orderdetail As New ArrayList()
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Try
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 54頁/共 85頁
Dim menureader = My.Computer.FileSystem _
.OpenTextFieldParser(menufilepath, ",")
While Not menureader.EndOfData
Dim onerow() = menureader.ReadFields()
menuitems.Add(onerow)
ListBox1.Items.Add(onerow(0) & "-" & _
onerow(1) & "元")
End While
menureader.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim index = ListBox1.SelectedIndex
If index >= 0 Then
TextBox1.Text = menuitems(index)(0)
TextBox2.Text = menuitems(index)(1)
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
Dim index = ListBox1.SelectedIndex
If index > 0 Then
menuitems(index)(0) = TextBox1.Text
menuitems(index)(1) = TextBox2.Text
ListBox1.Items(index) = menuitems(index)(0) & "-" & _
menuitems(index)(1) & "元"
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
If TextBox2.Text <> "" And Val(TextBox2.Text) > 0 Then
Dim newitem() = {TextBox1.Text, TextBox2.Text}
menuitems.Add(newitem)
ListBox1.Items.Add(TextBox1.Text & "-" & _
TextBox2.Text & "元")
Else
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 55頁/共 85頁
MsgBox("輸入錯誤")
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
menuitems.RemoveAt(ListBox1.SelectedIndex)
ListBox1.Items.RemoveAt(listbox1.SelectedIndex)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button4.Click
Try
Dim menuwriter = My.Computer.FileSystem _
.OpenTextFileWriter(menufilepath, False)
For Each item In menuitems
menuwriter.writeline(item(0) & "," & item(1))
Next
menuwriter.close()
MsgBox("儲存成功")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button7.Click
Dim index = ListBox1.SelectedIndex
If index >= 0 And Val(TextBox3.Text) > 0 Then
Dim entry() = {menuitems(index)(0), TextBox3.Text, TextBox3.Text * menuitems(index)(1)}
orderDetail.add(entry)
ListBox2.Items.Add(menuitems(index)(0) & "," & TextBox3.Text & "份, " &
Val(TextBox3.Text) * menuitems(index)(1) & "元")
Else
MsgBox("需選菜色且數量大於0")
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button6.Click
Dim index = ListBox2.SelectedIndex
If index >= 0 Then
ListBox2.Items.RemoveAt(index)
orderdetail.RemoveAt(index)
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 56頁/共 85頁
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button5.Click
If ListBox2.Items.Count > 0 Then
Dim orderwriter = My.Computer.FileSystem _
.OpenTextFileWriter("D:\ch13\Order.csv", True)
Dim msg As String = "你點了:" & vbCrLf
Dim subtotal As Integer = 0
For i = 0 To orderdetail.Count - 1
orderwriter.WriteLine(Now & orderdetail(i)(0) & "," & orderdetail(i)(1) & "," &
orderdetail(i)(2))
msg &= ListBox2.Items(i) & vbCrLf
subtotal += orderdetail(i)(2)
Next
orderwriter.Close()
ListBox2.Items.Clear()
orderdetail.Clear()
MsgBox(msg & vbCrLf & "共" & subtotal & "元")
Else
MsgBox("你還沒點菜")
End If
End Sub
End Class
13-4 成績數入
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 57頁/共 85頁
程式碼:
Public Class ex1
Dim menufilepath = "D:\ch13\menu.csv"
Dim menuitems As New ArrayList
Dim orderdetail As New ArrayList()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
menuitems.RemoveAt(ListBox1.SelectedIndex)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
If TextBox2.Text <> "" And Val(TextBox2.Text) > 0 Then
Dim newitem() = {TextBox1.Text, TextBox2.Text}
menuitems.Add(newitem)
ListBox1.Items.Add(TextBox1.Text & "-" & _
TextBox2.Text & "元")
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
Dim index = ListBox1.SelectedIndex
If index > 0 Then
menuitems(index)(0) = TextBox1.Text
menuitems(index)(1) = TextBox2.Text
ListBox1.Items(index) = menuitems(index)(0) & "-" & _
menuitems(index)(1) & "元"
End If
End Sub
Private Sub ex1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
End Class
13-5 檔案總管
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 58頁/共 85頁
程式碼:
Public Class ex1
Dim menufilepath = "D:\ch13\menu.csv"
Dim menuitems As New ArrayList
Dim orderdetail As New ArrayList()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
menuitems.RemoveAt(ListBox1.SelectedIndex)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
If TextBox2.Text <> "" And Val(TextBox2.Text) > 0 Then
Dim newitem() = {TextBox1.Text, TextBox2.Text}
menuitems.Add(newitem)
ListBox1.Items.Add(TextBox1.Text & "-" & _
TextBox2.Text & "元")
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 59頁/共 85頁
Handles Button3.Click
Dim index = ListBox1.SelectedIndex
If index > 0 Then
menuitems(index)(0) = TextBox1.Text
menuitems(index)(1) = TextBox2.Text
ListBox1.Items(index) = menuitems(index)(0) & "-" & _
menuitems(index)(1) & "元"
End If
End Sub
Private Sub ex1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
End Class
第十四章
14-1 書籍資料庫
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 60頁/共 85頁
程式碼:
Public Class Form1
Private Sub 書籍BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 書籍BindingNavigatorSaveItem.Click
Me.Validate()
Me.書籍BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.產品DataSet)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
'TODO: 這行程式碼會將資料載入 '產品DataSet.書籍' 資料表。您可以視需要進行移動或移除。
Me.書籍TableAdapter.Fill(Me.產品DataSet.書籍)
End Sub
End Class
14-2 書籍資料庫 2
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 61頁/共 85頁
程式碼:
Public Class Form3
Private Sub 書籍BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 書籍BindingNavigatorSaveItem.Click
Me.Validate()
Me.書籍BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.產品DataSet1)
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
'TODO: 這行程式碼會將資料載入 '產品DataSet1.書籍' 資料表。您可以視需要進行移動或移除。
Me.書籍TableAdapter.Fill(Me.產品DataSet1.書籍)
End Sub
End Class
14-3 書籍資料庫 3
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 62頁/共 85頁
程式碼:
Public Class Form4
Private Sub Form4_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
DataGridView1.EndEdit()
書籍TableAdapter.Update(產品DataSet.書籍)
End Sub
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
'TODO: 這行程式碼會將資料載入 '產品DataSet.書籍' 資料表。您可以視需要進行移動或移除。
Me.書籍TableAdapter.Fill(Me.產品DataSet.書籍)
End Sub
End Class
14-4 書籍資料庫 4
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 63頁/共 85頁
程式碼:
Public Class Form5
Private Sub 書籍BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 書籍BindingNavigatorSaveItem.Click
Me.Validate()
Me.書籍BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.產品DataSet1)
End Sub
Private Sub Form5_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
DataGridView.EndEdit()
Me.書籍TableAdapter.Update(產品DataSet1.書籍)
Me.書籍TableAdapter.Update(產品DataSet1.書籍)
End Sub
Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
'TODO: 這行程式碼會將資料載入 '產品DataSet1.書籍' 資料表。您可以視需要進行移動或移除。
Me.書籍TableAdapter.Fill(Me.產品DataSet1.書籍)
End Sub
Private Sub DataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 64頁/共 85頁
System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView.CellContentClick
End Sub
End Class
14-5 書籍詳細資料
程式碼:
Public Class Form7
Private Sub 書籍BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 書籍BindingNavigatorSaveItem.Click
Me.Validate()
Me.書籍BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.產品DataSet1)
End Sub
Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
'TODO: 這行程式碼會將資料載入 '產品DataSet1.類別' 資料表。您可以視需要進行移動或移除。
Me.類別TableAdapter.Fill(Me.產品DataSet1.類別)
'TODO: 這行程式碼會將資料載入 '產品DataSet1.書籍' 資料表。您可以視需要進行移動或移除。
Me.書籍TableAdapter.Fill(Me.產品DataSet1.書籍)
End Sub
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 65頁/共 85頁
Private Sub 書號Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ToolStripButton1.Click
OpenFileDialog1.Filter = _
"圖檔|*.jpg;*.gif;*.jpeg;*.png;*.tif|所有檔案|*.*"
If OpenFileDialog1.ShowDialog() = _
System.Windows.Forms.DialogResult.OK Then
圖片PictureBox.Image = New Bitmap(OpenFileDialog1.FileName)
End If
End Sub
End Class
14-6 成衣資料庫
程式碼:
Public Class Form2
Private Sub TshirtBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TshirtBindingNavigatorSaveItem.Click
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 66頁/共 85頁
Me.Validate()
Me.TshirtBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.成衣DataSet)
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
'TODO: 這行程式碼會將資料載入 '成衣DataSet.Tshirt' 資料表。您可以視需要進行移動或移除。
Me.TshirtTableAdapter.Fill(Me.成衣DataSet.Tshirt)
End Sub
End Class
14-7 成衣資料庫 2
程式碼:
Public Class Form6
Private Sub TshirtBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TshirtBindingNavigatorSaveItem.Click
Me.Validate()
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 67頁/共 85頁
Me.TshirtBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.成衣DataSet1)
End Sub
Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
'TODO: 這行程式碼會將資料載入 '成衣DataSet1.Tshirt' 資料表。您可以視需要進行移動或移
除。
Me.TshirtTableAdapter.Fill(Me.成衣DataSet1.Tshirt)
End Sub
End Class
第十五章
15-1 加減乘除計算機
程式碼:
Public Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
Label5.Text = "+"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
TextBox3.Text = Val(TextBox1.Text) - Val(TextBox2.Text)
Label5.Text = "-"
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
TextBox3.Text = Val(TextBox1.Text) * Val(TextBox2.Text)
Label5.Text = " "╳
End Sub
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 68頁/共 85頁
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button4.Click
TextBox3.Text = Val(TextBox1.Text) / Val(TextBox2.Text)
Label5.Text = "÷"
End Sub
End Class
15-2 計算 BMI值
程式碼:
Public Class Form3
Dim ans ‘ans是全域變數,才能在三個副程式區段都發揮作用
Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ans = MsgBox("您快樂嗎?", 4 + 32, "問答題") ‘MsgBox( )接收到資料傳給ans
If ans = 6 Then
Label1.Text = "請輸入體重(公斤)="
Label2.Text = "請輸入身高(公分)="
Me.Text = "計算BMI值"
Else
Label1.Text = "請輸入底 ="
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 69頁/共 85頁
Label2.Text = "請輸入高 ="
Me.Text = "計算三角形面積"
End If
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles TextBox1.TextChanged
If ans = 6 Then
Label3.Text = "BMI值 = " & Math.Round(Val(TextBox1.Text) / (Val(TextBox2.Text) / 100)
^ 2, 1)
Else
Label3.Text = "三角形面積 = " & Math.Round((Val(TextBox1.Text) * Val(TextBox2.Text) /
2), 1)
End If
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles TextBox2.TextChanged
If ans = 6 Then
Label3.Text = "BMI值 = " & Math.Round(Val(TextBox1.Text) / (Val(TextBox2.Text) / 100) ^
2, 1)
Else
Label3.Text = "三角形面積 = " & Math.Round((Val(TextBox1.Text) * Val(TextBox2.Text) / 2),
1)
End If
End Sub
End Class
15-3 薪資計算
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 70頁/共 85頁
程式碼:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click End End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim year%, salary% TextBox1.Focus() year = Val(TextBox1.Text) If year < 0 Then MsgBox("年資須大於零", , "注意") Else If year >= 10 Then salary = 50000 Else If year >= 5 Then salary = 40000 Else salary = 30000 End If End If If RadioButton1.Checked Then salary += 10000 Else If RadioButton2.Checked Then salary += 5000 Else salary += 3000 End If End If
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 71頁/共 85頁
If CheckBox1.Checked Then salary -= 1000 End If If CheckBox2.Checked Then salary -= 600 End If If year = 0 Then salary = 0 End If TextBox2.Text = salary End Sub End Class
15-4 計算費氏函數
程式碼:
Public Class Form1_1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
If Val(TextBox1.Text) < 1 Then
MsgBox("請輸入大於0的數", 0 + 16, "錯誤")
TextBox1.Text = ""
Else
TextBox2.Text = Fib(Val(TextBox1.Text))
End If
End Sub
Function Fib(ByVal K As Integer) As Decimal 'Decimal是最大的正整數數值型態了
If K <= 2 Then
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 72頁/共 85頁
Return 1 '解決遞迴問題,如何設定停止狀態很重要
Else
Return Fib(K - 1) + Fib(K - 2)
End If
End Function
End Class
15-5 輾轉相除法球最大公因數與最小公倍數
程式碼:
Public Class Form1_2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim a, b As Integer
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
TextBox3.Text = gcd(a, b)
TextBox4.Text = a * b / gcd(a, b)
End Sub
Function gcd(ByVal a, ByVal b) As ULong '遞迴解法
If b = 0 Then
Return a
Else
Return gcd(b, a Mod b)
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 73頁/共 85頁
End If
End Function
End Class
15-6 輸入英文字元一律小寫
程式碼:
Public Class Form1
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case e.KeyChar
Case "A" To "Z"
TextBox1.AppendText(Chr((AscW(e.KeyChar) + 32)))
e.Handled = True '讓控制項不再出現大寫字母
End Select
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
End Sub
End Class
15-7 中心十字圓
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 74頁/共 85頁
程式碼:
Public Class Form2
Private Sub Form2_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim k = e.Graphics
k.FillEllipse(Brushes.Black, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height)
k.FillEllipse(Brushes.White, 2, 2, Me.ClientSize.Width - 4, Me.ClientSize.Height - 4)
'k.DrawEllipse(Pens.Black, f, g, h)
Dim a = New Point(ClientSize.Width / 2, 0)
Dim b = New Point(ClientSize.Width / 2, ClientSize.Height)
Dim c = New Point(0, ClientSize.Height / 2)
Dim d = New Point(ClientSize.Width, ClientSize.Height / 2)
k.DrawLine(Pens.Black, a, b)
k.DrawLine(Pens.Black, c, d)
End Sub
Private Sub Form2_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.SizeChanged
Me.Invalidate()
End Sub
End Class
15-8 音樂播放器
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 75頁/共 85頁
程式碼:
Public Class Form3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Select Case ListBox1.SelectedIndex
Case 0
AxWindowsMediaPlayer1.URL = "c:\Exam4\1.mp3"
AxWindowsMediaPlayer1.Ctlcontrols.play()
Case 1
AxWindowsMediaPlayer1.URL = "c:\Exam4\2.mp3"
AxWindowsMediaPlayer1.Ctlcontrols.play()
Case 2
AxWindowsMediaPlayer1.URL = "c:\Exam4\3.mp3"
AxWindowsMediaPlayer1.Ctlcontrols.play()
Case Else
MsgBox("未選取檔案!", 0 + 64, "注意")
End Select
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
AxWindowsMediaPlayer1.Ctlcontrols.stop()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Timer1.Tick
Label1.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentPositionString
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 76頁/共 85頁
End Sub
End Class
15-9 螢幕保護程式
程式碼:
Public Class Form1
Dim x%, y%, xd%, yd%
Dim g As Graphics
Dim brush As SolidBrush
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Timer1.Tick
Dim font As Font = New Font("標楷體", 22)
g = Me.CreateGraphics
Randomize()
Select Case Int(Rnd() * 10)
Case 0
g.Clear(Color.LightSkyBlue)
Case 1
g.Clear(Color.LightGreen)
Case 2
g.Clear(Color.Cyan)
Case 3
g.Clear(Color.Chocolate)
Case 4
g.Clear(Color.Plum)
Case 5
g.Clear(Color.Pink)
Case 6
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 77頁/共 85頁
g.Clear(Color.Aqua)
Case 7
g.Clear(Color.DarkOrange)
Case 8
g.Clear(Color.Azure)
Case 9
g.Clear(Color.Beige)
End Select
Select Case Int(Rnd() * 10)
Case 0
brush = New SolidBrush(Color.LightSalmon)
Case 1
brush = New SolidBrush(Color.Purple)
Case 2
brush = New SolidBrush(Color.Red)
Case 3
brush = New SolidBrush(Color.PeachPuff)
Case 4
brush = New SolidBrush(Color.DarkGreen)
Case 5
brush = New SolidBrush(Color.DarkBlue)
Case 6
brush = New SolidBrush(Color.Brown)
Case 7
brush = New SolidBrush(Color.Turquoise)
Case 8
brush = New SolidBrush(Color.PaleVioletRed)
Case 9
brush = New SolidBrush(Color.Teal)
End Select
g.DrawString("展現變化莫測的色彩", font, brush, x, y)
If xd = 1 Then
x -= 30
Else
x += 30
End If
If x > (Me.ClientSize.Width - 250) Then
xd = 1
End If
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 78頁/共 85頁
If x < 1 Then
xd = 0
End If
If yd = 1 Then
y -= 15
Else
y += 15
End If
If y > (Me.ClientSize.Height - 30) Then
yd = 1
End If
If y < 1 Then
yd = 0
End If
End Sub
Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
End
End Sub
End Class
15-10 檔案總管
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 79頁/共 85頁
程式碼:
Public Class Form2
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
TextBox1.Text = CurDir() '傳回當前路徑
TextBox1.SelectionStart = Len(TextBox1.Text)
TextBox1.Focus()
End Sub
Function formatDir(ByVal strDir As String)
If Microsoft.VisualBasic.Right(strDir, 1) <> "\" Then
strDir &= "\"
End If
Return strDir
End Function
Sub Display()
ChDir(TextBox1.Text) '先讀取路徑
Dim strItem$
ListBox1.Items.Clear()
ListBox2.Items.Clear()
strItem = Dir("*.*", 18) '連資料夾(16)和隱藏檔(2)都讀取
Do Until strItem = ""
If (GetAttr(strItem) And vbDirectory) = 16 Then
ListBox1.Items.Add(strItem)
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 80頁/共 85頁
Else
ListBox2.Items.Add(strItem)
End If
strItem = Dir()
Loop
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
Display()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
Try
MkDir(TextBox1.Text)
Catch ex As Exception
MsgBox("該目錄已存在!", 0 + 48, "")
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
ChDir("..")
TextBox1.Text = CurDir()
Display()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button4.Click
TextBox1.Text = formatDir(TextBox1.Text)
Dim CurDir$
CurDir = TextBox1.Text & ListBox1.SelectedItem
My.Computer.FileSystem.DeleteDirectory(CurDir,
FileIO.DeleteDirectoryOption.DeleteAllContents)
Display()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button5.Click
TextBox1.Text = formatDir(TextBox1.Text)
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 81頁/共 85頁
Dim CurFile$
CurFile = TextBox1.Text & ListBox2.SelectedItem
Try
My.Computer.FileSystem.DeleteFile(CurFile)
Catch ex As Exception
MsgBox("錯誤: 檔案已不存在或無法刪除!", 0 + 48, "")
End Try
Display()
End Sub
Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
Handles ListBox1.DoubleClick
TextBox1.Text = formatDir(TextBox1.Text)
TextBox1.Text = TextBox1.Text & ListBox1.SelectedItem
Display()
End Sub
Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ListBox2.SelectedIndexChanged
TextBox1.Text = formatDir(TextBox1.Text)
ToolStripStatusLabel1.Text = "檔案大小:" & FileLen(TextBox1.Text & ListBox2.SelectedItem)
& " Bytes "
ToolStripStatusLabel2.Text = "修改日期:" & FileDateTime(TextBox1.Text &
ListBox2.SelectedItem)
Select Case GetAttr(TextBox1.Text & ListBox2.SelectedItem)
Case 39
Label4.Text = "檔案屬性:一般、系統、隱藏、唯讀"
Case 36
Label4.Text = "檔案屬性:一般、系統"
Case 35
Label4.Text = "檔案屬性:一般、 隱藏、唯讀"
Case 34
Label4.Text = "檔案屬性:一般、 隱藏"
Case 32
Label4.Text = "檔案屬性:一般"
Case 7
Label4.Text = "檔案屬性: 系統、隱藏、唯讀"
Case 6
Label4.Text = "檔案屬性: 系統、隱藏"
Case 5
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 82頁/共 85頁
Label4.Text = "檔案屬性: 系統、 唯讀"
Case 4
Label4.Text = "檔案屬性: 系統"
Case 3
Label4.Text = "檔案屬性: 隱藏、唯讀"
Case 2
Label4.Text = "檔案屬性: 隱藏"
Case 1
Label4.Text = "檔案屬性: 唯讀"
End Select
End Sub
End Class
15-11 便利貼
程式碼:
Public Class Form3
Dim filename As String = "note.txt"
Private Sub Form3_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
FileOpen(1, filename, OpenMode.Binary)
FilePut(1, TextBox1.Text)
FileClose(1)
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Dim len%
FileOpen(1, filename, OpenMode.Binary)
len = LOF(1)
Dim content As New String("", len)
FileGet(1, content)
FileClose(1)
TextBox1.Text = content
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 83頁/共 85頁
TextBox1.SelectionStart = len
TextBox1.SelectionLength = 0
My.Computer.FileSystem.DeleteFile(filename)
End Sub
Private Sub 字型ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 字型ToolStripMenuItem.Click
FontDialog1.ShowColor = True
If FontDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBox1.Font = FontDialog1.Font
TextBox1.ForeColor = FontDialog1.Color
End If
End Sub
Private Sub 背景顏色ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles 背景顏色ToolStripMenuItem.Click
ColorDialog1.AllowFullOpen = False
ColorDialog1.ShowHelp = False
If ColorDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBox1.BackColor = ColorDialog1.Color
End If
End Sub
End Class
15-12 求互質&畫數字三角形&數字部份加總&身分證
字號驗算
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 84頁/共 85頁
程式碼:
Public Class Form1
Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles GroupBox1.Enter
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim i%, n%
Label5.Text = ""
n = Val(TextBox1.Text)
For i = 2 To n - 1
If (gcd(i, n) = 1) Then
Label5.Text &= i & " "
End If
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint
第 85頁/共 85頁
Next
End Sub
Function gcd(ByVal a, ByVal b) As ULong '遞迴解法
If b = 0 Then
Return a
Else
Return gcd(b, a Mod b)
End If
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
Dim i%, j%, n%
Label6.Text = ""
n = Val(TextBox2.Text)
For i = 1 To n
For j = 1 To i
Label6.Text &= j
Next
Label6.Text &= vbCrLf
Next
End Sub
End Class
PDF 檔案使用 "pdfFactory Pro" 試用版本建立 www.ahasoft.com.tw/FinePrint