1 / 33

Visual Basic 程式設計

Visual Basic 程式設計. 講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所. 第六章 內建函數. 字串函數 數學函數 日期與時間函數. 字串函數. 練習時,函式的頭尾請自行加入. 轉為小寫字串: LCase( 字串) LCase(“AnB123”) “anb123” 轉為大寫字串: UCase( 字串) UCase(“AnB123”) “ANB123” 計算字串長度: Len( 型別) Len(“123”)=3 計算空間: LenB( 字串). Dim A As Integer A=123

tiva
Download Presentation

Visual Basic 程式設計

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Visual Basic 程式設計 講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所

  2. 第六章 內建函數 字串函數 數學函數 日期與時間函數

  3. 字串函數 練習時,函式的頭尾請自行加入 • 轉為小寫字串:LCase(字串) • LCase(“AnB123”)“anb123” • 轉為大寫字串:UCase(字串) • UCase(“AnB123”)“ANB123” • 計算字串長度:Len(型別) • Len(“123”)=3 • 計算空間:LenB(字串) Dim A As Integer A=123 Print Len(A) 2

  4. 字串函數(cont’d) • 子字串搜尋: • InStr([搜尋的位置],字串1,字串2[,比較法]) • 傳回字串2在字串1出現的位置 • 找不到則傳回0 • InStr(“abcdecd”,”cd”)3 • InStr(4,“abcdecd”,”cd”)6 • 比較法: • vbBinaryCompare分大小寫 • vbTextCompare不分大小寫

  5. 字串函數(cont’d) • 使用比較法時,起始位置要輸入 • InStr(1,“abcd”,“Cd”,vbBinaryCompare)0 • InStr(1,“abcd”,“Cd”,vbTextCompare)3 • 擷取子字串:Mid(字串,起始位置[,長度]) • Mid(“test test”,6)”test” • Mid(“test test”,6,3)”tes”

  6. 字串函數(cont’d) • 從左邊取子字串:Left(字串,長度) • Left(“test1 test2”,5)“test1” • 從右邊取子字串:Right(字串,長度) • Right(“test1 test2”,5)“test2” • 去除左右空白字元:Trim(字串) • Trim(“ test ”)“test” • LTrim(), RTrim()

  7. 字串函數(cont’d)

  8. 解答 Private Sub Form_Click() Dim name, lastname, firstname As String Dim position As Integer Do name = InputBox(“請輸入姓名:姓,名”, _ "注意", "黃,俊龍") name = Trim(name) position = InStr(name, ",") Loop Until (name <> "" And position <> 0) lastname = Left(name, position - 1) firstname = Mid(name, position + 1) Print lastname + "先生,你好" End Sub

  9. 數學函數 • 取整數:Int(數字) • 找小於等於數字的最小整數 • Int(1.9)1 • Int(-1.9)-2 • 取整數:Fix(數字) • 去除小數部份 • Fix(1.9)1 • Fix(-1.9)-1

  10. 數學函數(cont’d) • 轉成整數:CInt(參數) • 用來把其它型別(如字串)轉成整數 • 注意:當值為*.5時,結果不同 • CInt(0.4)0 • CInt(0.6)1 • CInt(0.5)0 • CInt(1.5)2 變成最近的偶數

  11. 數學函數(cont’d) • 取絕對值函數: ABS() • X = ABS(-77) 'X = 77 • SQR(), EXP(), Log() • Sin()、Cos()、Tan()、Atn() • Sin(30 * (3.14159265358979 / 180))

  12. 檢查是否數字 • 返回 Boolean 值指明表達式的值是否為數字 • IsNumeric(expression) • 如果整個 expression被識別為數字﹐IsNumeric函數返回 True﹔否則函數返回 False。

  13. 取亂數 • Rnd(種子數) • 0值<1 • 程式重執行時,取出的亂數相同 Private Sub Form_Click() Dim i As Integer For i = 1 To 10 Print Rnd(1) Next i End Sub

  14. 取亂數(cont’d) • 加入Randomize避免有次序的亂數 • 如何取某範圍的亂數 • 在1到100之間的整數 • Int(((100-1)+1)*Rnd()+1) • Int(((最大值-最小值)+1)*Rnd()+最小值)

  15. 猜數字

  16. Private Sub Guess(answer As Integer) Dim num, choice As Integer Dim inputStr As String Do inputStr = InputBox("猜1-100的數字", "猜猜看", CStr(num)) If (inputStr = "") Then Exit Sub End If num = CInt(inputStr) If (num = answer) Then choice = MsgBox("答對了,還要玩嗎?", vbYesNo) If (choice) = vbNo Then Exit Sub End If ElseIf (num > answer) Then MsgBox "太大了" Else MsgBox "太小了" End If Loop End Sub

  17. Private Sub Form_Click() Dim answer As Integer Randomize answer = Int((100 - 1 + 1) * Rnd + 1) Guess answer MsgBox "謝謝", vbOKOnly End Sub • Project 2: • 修正原來程式的BUG—每次答案都一樣 • 加上計算猜了幾次的功能

  18. 日期與時間函數 Print Date Print Date() • Date:取得目前的日期 • Date2000/10/20 • Time:取得目前的日期 • Time上午 12:59:30 • Now: 取得目前的日期與時間 • Now2000/10/20上午 12:59:30 • 此處的Date不是型別,是函式

  19. 日期與時間函數(cont’d) • 設定日期: • Date=#2000/1/1# • Time=#1:10:00 PM# • 取年、月、日 • Year(Date變數) • Month(Date變數) • Day(Date變數) 此處的Date是型別

  20. 日期與時間函數(cont’d) Private Sub Form_Click() Print Year(Date);“年”;Month(Date);“月”; _ Day(Date);“日” End Sub • 取時、分、秒 • Hour(Date變數) • Minute(Date變數) • Second(Date變數)

  21. 日期與時間函數(cont’d) • 取得星期幾 • Weekday(日期變數[,初始星期]) • Weekday(Date,vbSunday)6 • Weekday(Date,vbMonday)5 • WeekDay(Date)6

  22. 日期與時間函數(cont’d) • DateSerial(intYr,intMo,intDay) • DateAdd(strIntrvl,intN,dteDate) • DateDiff(strIntrvl,dte1,dte2) • DatePart(strIntrvl,dteDate) • dteDue=DateSerial(2004, 2, 28)

  23. 日期與時間內定格式字串

  24. 日期與時間內定格式字串(cont’d) • Dateadd(“yyyy”,1,Date) • Datediff(“ww”,dte1,dte2) • DatePart(“q”,Date)

  25. 日期與時間函數(cont’d) • Timer 傳回午夜至今的秒數 • TimeSerial(hour,min,sec) lngBefore=Timer OXXOXXXXOX lngAfter=Timer lngTimeDiff=lngAfter-lngBefore

  26. 日期與時間函數(cont’d) • 暫停一下 Sub wait (sec as single) dim start as Single start=Timer do until (timer-start+86400) mod 86400>sec loop End Sub

  27. 計時器 • 將計時器(Timer)的元件拉到表單上 • 屬性:Interval • 觸發間隔,以千分之一秒為單位 • 當Interval為0時,不會Timer事件 • 事件處理函式:Timer • 每次觸發時會執行一次

  28. 小時鐘 Private Sub Timer1_Timer() Cls Print Date; Time If (Second(Time) = 0) Then Beep End If End Sub 整點報時: Second(Time)=0 And Minute(Time)=0

  29. 資料檢查函數 • IsDate() date1=“五月 15, 2009” date2=#5/15/2009# nodate=“HiHi” Result=IsDate(date1) Result=IsDate(date2) Result=IsDate(nodate)

  30. 資料檢查函數(cont’d) • IsEmpty() • IsNumeric() • Dim MyVar, MyCheck • MyCheck = IsEmpty(MyVar) ' Returns True.

  31. 儲存專案 • FileSave Project

  32. 製作執行檔 • FileMake 專案名.exe

  33. 製作安裝程式 • 編成的執行檔必須要在有安裝VB或部份元件的機器執行

More Related