<< 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

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
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