<< Chapter < Page Chapter >> Page >

lblTg.Caption = gioht&":"&phutht&":"&giayht

End Sub

Bước 5: Thêm thủ tục xử lý sự kiên cho Button cmdGiophut, trong thủ tục này chèn đoạn mã sau:

Private Sub cmdGiophut_Click()

Dim gioht As String

Dim phutht As String

' Khong su dung tham so thu ba

Call Laythoigian(gioht, phutht)

lblTg.Caption = gioht&":"&phutht

End Sub

Bước 6: Lưu dự án lại và chạy chương trình. Kiểm tra kết quả.

Bài tập 1ii-6

Param array

Bước 1: Tạo thư mục Basic\Bt1II-6. Tạo dự án mới trong thư mục này.

Bước 2: Tạo Form như hình sau:

Hình I.14: Param Array

Trong đó:

ListBox: Name: lstTen

Button: Name: cmdds; Caption: Them vao danh sach

Bước 3: Chèn modul mới vào dự án tên Modul1. Sau đó, chọn Tool\Add Procedure để chèn thủ tục sau:

Name: Diends

Type: Sub

Scope: Public

Bước 3: Chèn đoạn mã sau vào thủ tục Diends

Public Sub Diends(ParamArray Ten() As Variant)

' Su dung ParamArray thi mang phai kieu Variant va

' mang nay la tham so cuoi cung cua thu tuc

Dim hten As Variant

For Each hten In Ten()

Form1.lstTen.AddItem hten

Next

End Sub

Bước 4: ParamArray cho phép không cần xác định số lượng các đối số trong một chương trình con. Bây giờ, thêm hàm xử lý sự kiện cho nút cmdds: cmdds_Click:

Private Sub cmdds_Click()

Call Diends("Huynh Xuan Hiep", "Nguyen Van Linh", "Lam Hoai Bao")

Call Diends

Call Diends("Phan Huy Cuong")

End Sub

Bước 5: Lưu dự án lại và chạy chương trình. Kiểm tra kết quả (hình bên dưới). Lưu ý đến lời gọi thủ tục trong sự kiện cmdds_Click (số lượng đối số khác nhau)

Hình I.15: Kết quả Param Array

Bài tập 1ii-7

XỬ LÝ CHUỖI

12345Bước 1: Tạo dự án mới trong thư mục Basic\Bt2-7 với giao diện như sau:

Hình I.16: Xử lý chuỗi

1: Form: Name: frmMain; MinButton: False; MaxButton: False; Font: VNI-Times.

2: Label: Name: lblTen.

3: TextBox: Name: txtTen.

4: CommandButton: Name: cmdTen; Caption: Tách tên.

5: CommandButton: Name: cmdCKT; Caption: Cắt khoảng trắng.

Bước 2: Tạo một hàm cắt khoảng trắng như sau:

Private Function ATrim(ByVal Name As String) As String

Name = LTrim(RTrim(Name))

Do While InStr(Name, "")<>0

Name = Replace(Name, "", "")

Loop

ATrim = Name

End Function

Bước 3: Trong cửa sổ thiết kế Form; nhấp đúp vào Tách tên, ta xử lý đoạn mã cho sự kiện này:

Private Sub cmdTen_Click()

Dim sName As String, Name As String

sName = ATrim(StrConv(txtTen.Text, vbProperCase))

Dim i As Long

i = InStrRev(sName, "")

Name = Right(sName, Len(sName) - i)

MsgBox Name&": "&Str(Len(Name))

End Sub

Bước 4: Sau đó, trở lại cửa sổ thiết kế, nhấp đúp vào Cắt khoảng trắng, ta xử lý:

Private Sub cmdCKT_Click()

Dim sName As String

sName = ATrim(StrConv(txtTen.Text, vbProperCase))

MsgBox sName, , "Kieu du lieu chuoi"

End Sub

Bước 5: Lưu dự án và chạy chương trình.

Bài tập 1ii-8

Xử lý lỗi

Bước 1: Tạo một dự án mới. Dùng Tools\Add Procedure thêm một thủ tục mới tên GoiThuTuc vào Form1 với nội dung như sau:

Public Sub GoiThuTuc()

Dim bien As Integer

MsgBox "Truoc khi gan tri cho bien"

bien = "Bien nguyen khong nhan gia tri la chuoi"

MsgBox "Sau khi gan tri cho bien: "&"Bien = "&Format(bien)

End Sub

Bước 2: Thủ tục xử lý sự kiện Form_Load có nội dung như sau:

Private Sub Form_Load()

Questions & Answers

A golfer on a fairway is 70 m away from the green, which sits below the level of the fairway by 20 m. If the golfer hits the ball at an angle of 40° with an initial speed of 20 m/s, how close to the green does she come?
Aislinn Reply
cm
tijani
what is titration
John Reply
what is physics
Siyaka Reply
A mouse of mass 200 g falls 100 m down a vertical mine shaft and lands at the bottom with a speed of 8.0 m/s. During its fall, how much work is done on the mouse by air resistance
Jude Reply
Can you compute that for me. Ty
Jude
what is the dimension formula of energy?
David Reply
what is viscosity?
David
what is inorganic
emma Reply
what is chemistry
Youesf Reply
what is inorganic
emma
Chemistry is a branch of science that deals with the study of matter,it composition,it structure and the changes it undergoes
Adjei
please, I'm a physics student and I need help in physics
Adjanou
chemistry could also be understood like the sexual attraction/repulsion of the male and female elements. the reaction varies depending on the energy differences of each given gender. + masculine -female.
Pedro
A ball is thrown straight up.it passes a 2.0m high window 7.50 m off the ground on it path up and takes 1.30 s to go past the window.what was the ball initial velocity
Krampah Reply
2. A sled plus passenger with total mass 50 kg is pulled 20 m across the snow (0.20) at constant velocity by a force directed 25° above the horizontal. Calculate (a) the work of the applied force, (b) the work of friction, and (c) the total work.
Sahid Reply
you have been hired as an espert witness in a court case involving an automobile accident. the accident involved car A of mass 1500kg which crashed into stationary car B of mass 1100kg. the driver of car A applied his brakes 15 m before he skidded and crashed into car B. after the collision, car A s
Samuel Reply
can someone explain to me, an ignorant high school student, why the trend of the graph doesn't follow the fact that the higher frequency a sound wave is, the more power it is, hence, making me think the phons output would follow this general trend?
Joseph Reply
Nevermind i just realied that the graph is the phons output for a person with normal hearing and not just the phons output of the sound waves power, I should read the entire thing next time
Joseph
Follow up question, does anyone know where I can find a graph that accuretly depicts the actual relative "power" output of sound over its frequency instead of just humans hearing
Joseph
"Generation of electrical energy from sound energy | IEEE Conference Publication | IEEE Xplore" ***ieeexplore.ieee.org/document/7150687?reload=true
Ryan
what's motion
Maurice Reply
what are the types of wave
Maurice
answer
Magreth
progressive wave
Magreth
hello friend how are you
Muhammad Reply
fine, how about you?
Mohammed
hi
Mujahid
A string is 3.00 m long with a mass of 5.00 g. The string is held taut with a tension of 500.00 N applied to the string. A pulse is sent down the string. How long does it take the pulse to travel the 3.00 m of the string?
yasuo Reply
Who can show me the full solution in this problem?
Reofrir Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Giáo trình visual basic. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10777/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Giáo trình visual basic' conversation and receive update notifications?

Ask