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) - Thtc (Procedure)
Khái nim lp trình hướng đối tượng
(OOP: Object Orientation Programming)
Lp trình theo skin (Event) và lp
trình Thuc 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 thuc tm vc ca hàm]
[Các câu lnh …]
[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óli bqua
rdb = CType(Ctrl , RadioButton) ' chuyn kiu
rdb.Visible = False
Next
GroupBox1.Text = "Sthí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