0

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

  • Check this: https://stackoverflow.com/questions/21457737/how-to-use-placeholder-for-textbox-in-vb-net-2010-like-html –  Sep 19 '19 at 12:08
  • 1
    The proper term is "cue" in Windows-speak. It is a built-in feature for TextBox, [this Q+A](https://stackoverflow.com/questions/4902565/watermark-textbox-in-winforms) shows how to enable it. Possibly not "new to programming" material, keep it simple by putting a Label next to the box. – Hans Passant Sep 19 '19 at 12:18
  • Take passwords out of your connection strings, when posting on-line. – HardCode Sep 19 '19 at 16:20
  • Unless screen real estate is really at a premium, can't you just sneak in a couple of labels Username, Password next to the text boxes? Would save some coding. – Mary Sep 20 '19 at 07:07
  • Your data access code has nothing to do with your question but since you included it, I will include my usual database rants. Use Using...End Using blocks for your database objects so they will be closed and disposed even if there is an error. Never concatenate strings to build an sql command text. Use Parameters. Don't pull down unnecessary data. If all you need is the count then Select Count(*) From .... NEVER store passwords as plain text. – Mary Sep 20 '19 at 07:19

2 Answers2

0

Try playing with a combination of mouseclick/lostFocus

just copy this code as is, having Username on textbox1 and Password on textbox2

Edit:

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    TextBox1.ForeColor = Color.Gray
    TextBox2.ForeColor = Color.Gray

End Sub

Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
    If TextBox1.Text = String.Empty Then
        TextBox1.ForeColor = Color.Gray
        TextBox1.Text = "Username"
    End If
End Sub

Private Sub TextBox1_MouseClick(sender As Object, e As EventArgs) Handles TextBox1.MouseClick
    If TextBox1.Text = "Username" Then
        TextBox1.Text = String.Empty
        TextBox1.ForeColor = Color.Black
    End If
End Sub

Private Sub TextBox2_LostFocus(sender As Object, e As EventArgs) Handles TextBox2.LostFocus
    If TextBox2.Text = String.Empty Then
        TextBox2.ForeColor = Color.Gray
        TextBox2.Text = "Password"
    End If
End Sub

Private Sub TextBox2_MouseClick(sender As Object, e As EventArgs) Handles TextBox2.MouseClick
    If TextBox2.Text = "Password" Then
        TextBox2.Text = String.Empty
        TextBox2.ForeColor = Color.Black
    End If
End Sub


End Class
OctaCode
  • 635
  • 1
  • 4
  • 10
  • I'm afraid this is not sufficient for an answer. Answers should be complete, self-contained and actually provide an _answer_ to the question. If your intent is merely to point the OP in the right direction or give him/her advice then you should write a comment instead. – Visual Vincent Sep 20 '19 at 07:06
  • 1
    you're right Visual Vincent it should have been a comment, my bad, now hope a complete code would be a good answer :) – OctaCode Sep 20 '19 at 07:32
  • Thank you for updating! It's definitely better. I have retracted my flag. – Visual Vincent Sep 20 '19 at 08:31
0

You could use the .KeyPressed event to check if the text "Username" is in the text box and remove it.

For the password box, the same, and change change the .PasswordChar property to "*" so that anything typed is hidden

 Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    TextBox1.Text = TextBox1.Text.Replace("Username", "")
End Sub

Private Sub TextBox4_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox4.KeyPress
    TextBox4.Text = TextBox4.Text.Replace("Password", "")
    TextBox4.PasswordChar = "*"c
End Sub
David Wilson
  • 4,369
  • 3
  • 18
  • 31
  • If i want to choose a password like "!@#$Password)(*^^%" my password would be converted to "!@#$)(*^^%" using your code and i wouldn't know cause it's all converted to stars. You should check if the text is only "Password" then replace it, not replace whenever you see the instance of "Password" inside that box. Please correct me if i'm wrong. Also your code does not revert empty boxes to "Username" and "Password" when losing focus. – OctaCode Sep 20 '19 at 08:44
  • the user never asked to blank the textboxes when losing focus and the user *did* ask that Password be removed and not accidentally included in the final password. Read the question. – David Wilson Sep 20 '19 at 09:34
  • I did read the question very well.. due to the fact he has the textboxes's texts as "username" and "password" he's checking `if first textbox is "username" OR second textbox is "password"` then he doesn't want it to be submitted and give an error, in your given solution if i click on a textbox if will erase it and never revert it back which will lead to him submitting will null values. Btw as i said in a previous comment, if the word "password" means another think in my language and i want to include it in the pass field you will be erasing it for me like "!@#$Password)(*^^%" => "!@#$)(*^^%". – OctaCode Sep 20 '19 at 09:53
  • your solution might work with some people but some people just click on stuff and forget to fill them again, you are right to replace the word password with blank but you must check if it's only word password or is it combined with some text. – OctaCode Sep 20 '19 at 09:58
  • I know you have lots of reps and you want to have the ultimate (shortest) solution but this is just not a good solution and you know it. checking for the word on each keypress is not an ultimate solution. Open VS have your solution in there click textbox1 don't fill it, click textbox2 insert "jgasdPasswordhgj" in it, tell me what is submitted. The user would be "" and pass "jgasdhgj". If your checking for blanks then if you leave username untouchable it should be submitted. If people still think this is how it should be done, then ok :) fine with me. – OctaCode Sep 20 '19 at 10:22