
1
1
Windows Form programming with VB.Net 2005.
Bu
Buổ
ổi 5: K
i 5: Kỹ
ỹthu
thuậ
ật l
t lậ
ập tr
p trì
ình trong VB.NET
nh trong VB.NET
Hàm (Function) - Thủtục (Procedure)
Khái niệm lập trình hướng đối tượng
(OOP: Object Orientation Programming)
Lập trình theo sựkiện (Event) và lập
trình Thuộc tính (Property)
2
Windows Form programming with VB.Net 2005.
H
Hà
àm (Function)
m (Function)
[Public, Protected, Private] Function <Tên hàm> _
[(Danh sách các tham số)] AS <ReturnType> _ [Handles
eventlist]
[Khai báo biến thuộc tầm vực của hàm]
[Các câu lệnh …]
[Exit Fucntion]
Return <ReturnType>
End Fucntion
-
-M
Mộ
ột H
t Hà
àm
ml
là
àdãy c
dãy cá
ác câu l
c câu lệ
ệnh đ
nh để
ểth
thự
ực thi m
c thi mộ
ột công
t công
vi
việ
ệc, m
c, mộ
ột ch
t chứ
ức năng đ
c năng đặ
ặc th
c thù
ùn
nà
ào đ
o đó
ó
-
-H
Hà
àm
m đư
đượ
ợc g
c gọ
ọi đ
i để
ểthi h
thi hà
ành th
nh thông
ông qua tên th
qua tên thủ
ủt
tụ
ục
c
-
-H
Hà
àm
mth
thư
ườ
ờng
ng c
có
ógi
giá
átr
trị
ịtr
trả
ảv
về
ề

2
3
Windows Form programming with VB.Net 2005.
Private Function Max(ByVal aAs Integer, ByVal _
bAs Integer) As Integer
If a > b Then
Return a
Else
Return b
End If
End Function
Dim C as Integer
Dim C as Integer
C = Max(a,b)
C = Max(a,b)
VD: H
VD: Hà
àm
m
4
Windows Form programming with VB.Net 2005.
Th
Thủ
ủt
tụ
ục (Procedure)
c (Procedure)
[Public, Protected, Private] Sub
[Public, Protected, Private] Sub <Tên Th
<Tên Thủ
ủt
tụ
ục> _
c> _
[(Danh s
[(Danh sá
ách c
ch cá
ác tham s
c tham số
ố)] [
)] [Handles
Handles eventlist
eventlist]
]
[Khai b
[Khai bá
áo bi
o biế
ến thu
n thuộ
ộc t
c tầ
ầm v
m vự
ực c
c củ
ủa th
a thủ
ủt
tụ
ục]
c]
[C
[Cá
ác câu l
c câu lệ
ệnh
nh …
…]
]
[Exit Sub]
[Exit Sub]
End Sub
End Sub
-
-Ch
Chứ
ức năng c
c năng củ
ủa th
a thủ
ủt
tụ
ục gi
c giố
ống như H
ng như Hà
àm,
m, nhưng
nhưng
Th
Thủ
ủt
tụ
ục
c không
không c
có
ógi
giá
átr
trị
ịtr
trả
ảv
về
ề

3
5
Windows Form programming with VB.Net 2005.
Private Sub bntAn_Click(.. , ..) Handles bntAn.Click
AnGioiTinh()
End Sub
6
Windows Form programming with VB.Net 2005.
Private Sub AnGioiTinh()
Dim rdb As RadioButton
For Each Ctrl As Control In GroupBox1.Controls
On Error Resume Next 'Cólỗi bỏqua
rdb = CType(Ctrl , RadioButton) ' chuyển kiểu
rdb.Visible = False
Next
GroupBox1.Text = "Sởthích:“
End Sub
VD: Th
VD: Thủ
ủt
tụ
ục
c

4
7
Windows Form programming with VB.Net 2005.
Public
Public Function
Function GetCommand()
GetCommand() As
As OleDbCommand
OleDbCommand
Dim
Dim cn
cn As New
As New OleDbConnection
OleDbConnection
Dim
Dim cmd
cmd As New
As New OleDbCommand
OleDbCommand
Dim
Dim strConnection
strConnection As String =
As String =
"Provider=Microsoft.Jet.OLEDB.4.0;Data
"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:
Source=D:\
\QLNhanSu.mdb"
QLNhanSu.mdb"
cn.ConnectionString
cn.ConnectionString =
= strConnection
strConnection
cn.Open()
cn.Open()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText =
cmd.CommandText = “
“sp_NhanVien
sp_NhanVien”
”
cmd.Connection = cn
cmd.Connection = cn
return
return cmd
cmd
End Function
End Function
VD: H
VD: Hà
àm tr
m trả
ảv
về
ề 1
1 đ
đố
ối tư
i tượ
ợng Command
ng Command
8
Windows Form programming with VB.Net 2005.
VD: DataAdapter & Fill cho DataTable
VD: DataAdapter & Fill cho DataTable
p
public
ublic f
function
unction G
Get
etD
Data
ataT
Table
able()
() a
as
sD
Data
ataT
Table
able
Dim
Dim da
da As New
As New OleDbDataAdapter()
OleDbDataAdapter()
Dim
Dim tb
tb As New
As New DataTable
DataTable()
()
Tr
Try
y ‘
‘ Cmd đ
Cmd đố
ối tư
i tượ
ợng đã đư
ng đã đượ
ợc khai b
c khai bá
áo & kh
o & khở
ởi t
i tạ
ạo
o
da.
da.SelectCommand
SelectCommand =
= C
Cmd
md ‘
‘= GetCommand()
= GetCommand()
da.Fill(tb)
da.Fill(tb)
Return
Return tb
tb
Catch
Catch ex
ex As
As Exception
Exception
MsgBox(ex.Message)
MsgBox(ex.Message)
Return Nothing
Return Nothing
End Try
End Try
End Function
End Function

5
9
Windows Form programming with VB.Net 2005.
VD: DataAdapter & Fill cho DataSet
VD: DataAdapter & Fill cho DataSet
P
Public Function
ublic Function GetData
GetDataSet
Set()
() a
as
sD
Data
ataSet
Set
Dim
Dim da
da As New
As New OleDbDataAdapter()
OleDbDataAdapter()
Dim
Dim ds
ds As New
As New Data
DataSet()
Set()
Try
Try ‘
‘ Cmd đ
Cmd đố
ối tư
i tượ
ợng đã đư
ng đã đượ
ợc khai b
c khai bá
áo & kh
o & khở
ởi t
i tạ
ạo
o
da.
da.SelectCommand
SelectCommand =
= C
Cmd
md ‘
‘= GetCommand()
= GetCommand()
da.Fill(
da.Fill(ds
ds)
)‘
‘da.Fill(
da.Fill(ds,
ds, “
“TableName
TableName”
”)
)
Return
Return ds
ds
Catch
Catch ex
ex As
As Exception
Exception
MsgBox(ex.Message)
MsgBox(ex.Message)
Return Nothing
Return Nothing
End Try
End Try
End Function
End Function
10
Windows Form programming with VB.Net 2005.
Public Function GetDataReader(ByVal StoreProceureName As
String, Optional ByVal arrParams() As String = Nothing,
Optional ByVal arrValues() As Object = Nothing) As
OleDbDataReader
Dim rd As OleDbDataReader
Dim
Dim cmd
cmd As New
As New OleDbCommand
OleDbCommand
cmd = me.GetCommand()
cmd = me.GetCommand()
If Not (arrParams Is Nothing) Then
For nIndex As Integer = 0 To (arrParams.Length - 1)
cmd.Parameters.AddWithValue(arrParams(nIndex),
arrValues(nIndex))
Next
End If
rd = cmd.ExecuteReader()
Return rd ‘cmd.ExecuteReader()
End Function

