Private Sub Form_Initialize()
serverrespon.text = "loginok"
KeyPreview =True
End sub
Private Sub btlogin_Click()
Dim strURL As String
On Error Resume Next
If Trim(txtusername.Text) = "" Or Len(Trim(txtusername.Text)) = 0 Then
MsgBox "กรุณากรอก (Username)ให้ถูกต้อง.", vbOKOnly + vbExclamation, "Login"
Exit Sub
ElseIf Trim(txtpassword.Text) = "" Or Len(Trim(txtpassword.Text)) = 0 Then
MsgBox "กรุณากรอก(Password) ให้ถูกต้อง.", vbOKOnly + vbExclamation, "Login"
Exit Sub
End If
btlogin.Enabled = False
strURL = "http://localhost/login/login.php?" & "username=" + txtusername.Text & "&password=" + txtpassword.Text
Inet1.OpenURL (strURL)
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim ser As String
If Inet1.OpenURL = serverrespon.Text Then
MsgBox "สวัสดีคุณ" & txtusername.Text & " ยินดีต้อนรับสู่ โปรแกรมของเรา.", vbOKOnly + vbInformation, "Login Status"
Timer1.Enabled = False
Unload Me
main.Show // Login ถูกต้อง เข้าสู่หน้าโปรแกรมถัดไป
Else
MsgBox "ไม่สามารถเข้าสู่โปรแกรมได้.", vbOKOnly + vbInformation, "Login Status"
txtusername.Text = ""
txtpassword.Text = ""
txtusername.SetFocus
btlogin.Enabled = True
End If
Timer1.Enabled = False
End Sub
//สร้าง Sub ขึ้นมาเพื่อให้ เวลา Login สามารภกด Enter แล้วเหมือนกดปุ่ม Login เลย
Sub Enterlogin()
Dim strURL As String
On Error Resume Next
If Trim(txtusername.Text) = "" Or Len(Trim(txtusername.Text)) = 0 Then
MsgBox "กรุณากรอก (Username)ให้ถูกต้อง.", vbOKOnly + vbExclamation, "Login"
Exit Sub
ElseIf Trim(txtpassword.Text) = "" Or Len(Trim(txtpassword.Text)) = 0 Then
MsgBox "กรุณากรอก(Password) ให้ถูกต้อง.", vbOKOnly + vbExclamation, "Login"
Exit Sub
End If
btlogin.Enabled = False
strURL = "http://localhost/login/login.php?" & "username=" + txtusername.Text & "&password=" + txtpassword.Text
Inet1.OpenURL (strURL)
Timer1.Enabled = True
End Sub
// โค้ดเกี่ยวกับ Key board ให้รับค่าเฉพาะ อังกฤษ และ ต่างๆ อีกมากมาย Credits อาจารย์ท้องก้อน G2gNet
Private Sub txtPassword_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDown Then SendKeys "{TAB}"
If KeyCode = vbKeyUp Then SendKeys "+{TAB}"
End Sub
Private Sub txtPassword_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
SendKeys "{TAB}"
KeyAscii = 0
Else
KeyAscii = CheckAlphaNumeric(KeyAscii)
End If
End Sub
Private Sub txtUserName_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDown Then SendKeys "{TAB}"
If KeyCode = vbKeyUp Then SendKeys "+{TAB}"
End Sub
Private Sub txtUserName_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
SendKeys "{TAB}"
KeyAscii = 0
Else
KeyAscii = CheckAlphaNumeric(KeyAscii)
End If
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyEscape: End
Case vbKeyReturn: Call Enterlogin
End Select
End Sub
Public Function CheckAlphaNumeric(Index As Integer) As Integer
Select Case Index
Case 48 To 57, 65 To 90, 97 To 122
CheckAlphaNumeric = Index
Case 8
Case 13
Case Else
Index = 0
End Select
CheckAlphaNumeric = Index
End Function