Bài 5 7Nhập mật khẩu Sửa chương trình: - Khi nhập đúng user và pass thì nút Log in chuyển thành Log out.. Sửa chương trình: - Định thời 30s thì tự động Log out.. Sửa chương trình: - Định
Trang 11 Chương trình đồng hồ
2 Tạo khung nhập mật khẩu
3 Định thời thoát user
4 Tạo nhiều form
BÀI 5: TIMER
1 Bài 5
Chương trình đồng hồ
09:56:11
Giao diện thiết kế:
Enabled = True: cho phép Timer chạy Interval = 1000: mỗi 1s thì chạy Timer
1
2
Trang 2Bài 5 3
Chương trình đồng hồ
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles
Timer1.Tick
lblClock.Text = Format(Now, "dd/MM/yyyy") + vbCrLf +
Format(Now, "hh:mm:ss")
End Sub
09:56:11 Thiết kế ứng dụng đếm ngược thời gian:
Thiết kế ứng dụng đồng hồ thể thao:
3
Trang 3Bài 5 5
Nhập mật khẩu
label
Label lblUser
Textbox txtUser Textbox txtPassword
PictureBox
15:54:14
Private Sub frmTimer_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
PictureBox1.Image = System.Drawing.Image.FromFile _
("E:\Visual Studio 2015\Projects\TimerSlide\Timer\Timer\noimage.jpg")
End Sub
Private Sub bttLogIn_Click(sender As Object, e As EventArgs)
Handles bttLogIn.Click
Dim user As String = txtUser.Text
Dim pass As String = txtPassword.Text
If (user = "abc") And (pass = "1") Then
lblUser.Text = "User đăng nhập: " + user
PictureBox1.Image = System.Drawing.Image.FromFile _
("E:\Visual Studio 2015\Projects\TimerSlide\Timer\Timer\bike.png")
ElseIf (user = "bcd") And (pass = "2") Then
lblUser.Text = "User đăng nhập: " + user
PictureBox1.Image = System.Drawing.Image.FromFile _
("E:\Visual Studio 2015\Projects\TimerSlide\Timer\Timer\car.png")
Else
MessageBox.Show("Sai user hay password")
End If
End Sub
5
6
Trang 4Bài 5 7
Nhập mật khẩu
Sửa chương trình:
- Khi nhập đúng user và pass thì nút Log in chuyển thành Log out.
- Nhấn Log out thì gán lblUser = “” và nút Log out chuyển thành
Log in.
Sửa chương trình:
- Định thời 30s thì tự động Log out
Sửa chương trình:
- Định thời 30s nếu không làm gì cả thì tự động Log out
15:54:14
Nhập mật khẩu
Đọc nội dung file text:
Dim FileNum As Integer
Dim user_r, pass_r As String
FileNum = FreeFile()
FileOpen(FileNum, Application.StartupPath + "\user.txt",
OpenMode.Input)
Do Until EOF(FileNum)
user_r = LineInput(FileNum)
pass_r = LineInput(FileNum)
Loop
FileClose(FileNum)
Các bước đọc nội dung file text:
- Tìm file number còn trống bằng hàm FreeFile
- Mở file bằng hàm FileOpen(File_Number,Tên_file,Chế độ)
- Đọc nội dung file cho đến khi kết thúc file
- Đóng file
7
Trang 5Bài 5 9
Câu lệnh While End While
q Dùng để lặp đi lặp lại công việc khi ĐiềuKiện là True
q Cú pháp:
While ĐiềuKiện
CácCâuLệnh
[Exit While]
End While
q ĐiềuKiện là một biểu thức sẽ được tính toán tại đầu vòng lặp Nó trả về
True hoặc False Nếu trả về True thìCácCâuLệnh được thực hiện End
While đánh dấu kết thúc vòng lặp Exit While là một tùy chọn, được dùng
để kết thúc vòng lặp
15:54:14
Dim Counter As Integer=1
While Counter <= 5 MessageBox.Show("Value is: " & Counter) Counter =Counter + 1
End While
Dim Counter As Integer=1
While Counter <= 5 MessageBox.Show("Value is: " & Counter) Counter =Counter + 1
If Counter = 4 Then
Exit While End If
End While
9
10
Trang 6Bài 5 11
[Exit Do]
Loop
q Từ khóaWhile -> Lặp khi ĐiềuKiện = True
q Từ khóaUntil -> Lặp khi ĐiềuKiện = False
Dim Counter As Integer = 1
Do While Counter <= 5 MessageBox.Show("Value is :" & Counter) Counter=Counter + 1
Loop Do
CácCâuLệnh
[Exit Do]
Loop While|UntilĐiềuKiện
Dim Counter As Integer = 1 Do
MessageBox.Show("Value is: " & Counter) Counter=Counter + 1
Loop While Counter<=5
15:54:14
Nhập mật khẩu
Sửa chương trình:
- Kiểm tra thông tin đăng nhập từ file text trong đó thông tin
user và password lưu trên từng dòng.
Sửa chương trình:
- Nếu không tìm thấy user thì xuất thông báo ‘User hay pass không
đúng’
Sửa chương trình:
- Nếu nhập sai pass 3 lần thì thoát chương trình
11
Trang 7Bài 5 13
Nhập mật khẩu
Sửa chương trình:
- Nếu nhập sai pass 3 lần thì thoát chương trình và sau 5’ mới
cho phép đăng nhập lại
Các bước ghi nội dung file text:
- Tìm file number còn trống bằng hàm FreeFile
- Mở file bằng hàm FileOpen(File_Number,Tên_file,Chế độ)
- Ghi nội dung file bằng hàm PrintLine
- Đóng file
Gợi ý: lưu thời gian được đăng nhập vào file text và cho phép
đăng nhập khi thời gian hiện tại > thời gian cho phép
15:54:14
Tạo nhiều form
Sửa chương trình:
- Nếu nhập đúng mật khẩu thì mở thêm 1 form mới (frmUser) Nếu
đóng frmUser thì hiện lại form đăng nhập
Click Project > Add Windows Form Đặt tên form là frmUser
Hiện form frmUser: frmUser.Show
Ẩn form frmUser: frmUser.Hide
Đóng form frmUser: frmUser.Close
13
14