Good Day!
I am new to vb.net as well as new to programming so i'm not very familiar with codes this might be easy but I really don't know how to do this.
So I have a log in form, if the textbox1 (username) and textbox2 (password) has no values and the user enters it or clicks the log-in button an error saying the user must complete the required fields will appear. I have put placeholders to it "Username" in textbox1 and "Password" in textbox2 and i wanted visual basic to still treat them as "without values" so the error will still come up if user don't type anything. Also when i put the cursor into the textbox, leave it, then type anything the placeholders stays together with what I typed e.g; if type "asdf" the textbox shows "asdfUsername" how do I get rid of this?
This is the current code im using
Public Class frmLogIn
Private Sub BtnLOGIN_Click(sender As Object, e As EventArgs) Handles btnLOGIN.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MessageBox.Show("Please complete the required fields!", "Authentication Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox1.Text = ""
TextBox2.Text = ""
Else
Dim MyConn As New MySqlConnection
Dim COMMAND As MySqlCommand
Dim READER As MySqlDataReader
MyConn.ConnectionString = "server=localhost;user id =root;password=**********;database=eeldatabase"
Try
MyConn.Open()
Dim query As String
query = "select * from eeldatabase.logininfo where username='" & TextBox1.Text & "' and password ='" & TextBox2.Text & "'"
COMMAND = New MySqlCommand(query, MyConn)
READER = COMMAND.ExecuteReader
Dim count As Integer
count = 0
While READER.Read
count = count + 1
End While
If count = 1 Then
MDIMain.Show()
Me.Hide()
ElseIf count < 1 Then
MessageBox.Show("Username and/or Password is incorrect!", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
TextBox1.Text = "Username"
TextBox2.Text = "Password"
TextBox1.ForeColor = Color.Gray
TextBox2.ForeColor = Color.Gray
End If
MyConn.Close()
Catch ex As Exception
MessageBox.Show("Failed to connect to Database..", "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Private Sub BtnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Private Sub TextBox2_KeyPress(ByVal sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
If e.KeyChar = Chr(13) Then
BtnLOGIN_Click(Me, EventArgs.Empty)
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(13) Then
BtnLOGIN_Click(Me, EventArgs.Empty)
End If
End Sub
Private Sub TextBox1_MouseEnter(sender As Object, e As EventArgs) Handles TextBox1.MouseEnter
If TextBox1.Text = "Username" Then
TextBox1.Text = ""
TextBox1.ForeColor = Color.Black
End If
End Sub
Private Sub TextBox1_MouseLeave(sender As Object, e As EventArgs) Handles TextBox1.MouseLeave
Dim user
If TextBox1.Text = "" Then
TextBox1.Text = "Username"
TextBox1.ForeColor = Color.Gray
ElseIf TextBox1.Text =
End If
End Sub
Private Sub TextBox2_MouseEnter(sender As Object, e As EventArgs) Handles TextBox2.MouseEnter
If TextBox2.Text = "Password" Then
TextBox2.Text = ""
TextBox2.ForeColor = Color.Black
End If
End Sub
Private Sub TextBox2_MouseLeave(sender As Object, e As EventArgs) Handles TextBox2.MouseLeave
If TextBox2.Text = "" Then
TextBox2.Text = "Password"
TextBox2.ForeColor = Color.Gray
End If
End Sub
End Class
Sorry for bad english. Thanks in advance!
EDIT: It works now i just used create grhapics as the cue banners and used these codes
MouseMove for Panel1 containing the textboxes so when the cursor is away in the panel, cue banners will show
Private Sub Panel1_MouseMove(sender As Object, e As EventArgs) Handles Panel1.MouseMove
If TextBox1.Text = "" Then
TextBox1.CreateGraphics.DrawString("Username", New Font("Microsoft Sans Serif", 12), New SolidBrush(Color.LightGray), 0, 0)
End If
If TextBox2.Text = "" Then
TextBox2.CreateGraphics.DrawString("Password", New Font("Microsoft Sans Serif", 12), New SolidBrush(Color.LightGray), 0, 0)
End If
End Sub
MouseMove event for LOGIN form so when the cursor is away in the form, cue banners will show
Private Sub frmLogIn_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If TextBox1.Text = "" Then
TextBox1.CreateGraphics.DrawString("Username", New Font("Microsoft Sans Serif", 12), New SolidBrush(Color.LightGray), 0, 0)
End If
If TextBox2.Text = "" Then
TextBox2.CreateGraphics.DrawString("Password", New Font("Microsoft Sans Serif", 12), New SolidBrush(Color.LightGray), 0, 0)
End If
End Sub
and MenuStart event so upon entering the LOG IN form the cue banners will already be displayed
Private Sub frmLogIn_MenuStart(sender As Object, e As EventArgs) Handles Me.MenuStart
If TextBox1.Text = "" Then
TextBox1.CreateGraphics.DrawString("Username", New Font("Microsoft Sans Serif", 12), New SolidBrush(Color.LightGray), 0, 0)
End If
If TextBox2.Text = "" Then
TextBox2.CreateGraphics.DrawString("Password", New Font("Microsoft Sans Serif", 12), New SolidBrush(Color.LightGray), 0, 0)
End If
End Sub
However I'm still having problems with the Keypress event i used for LOG IN button using Enter so i just removed it and just used the button click. Also the password character for textbox2 won't appear although the SystemPasswordChar is set to true