1

If anyone has any other solutions to achieve this I would be very interested in hearing them.

I am trying to create a password checker program for a school project but am having trouble with my progressbar. Basically, when the password's strength is strong (therefore meaning the score is higher) the progressbar works perfectly fine, but when the strength of the password is not strong the progressbar does not change to the desired colour or show a value.

Here is my code:

Public Class PassCheck

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer,
                                                                ByVal wMsg As Integer, ByVal wParam As Integer,
                                                                ByVal IParam As Integer) As Integer

Dim Checked As Boolean
Dim Password As String
Dim PasswordLength As Integer
Dim PasswordStrength As String
Dim Score As Integer
Dim ProgBarVal As Double
Dim LengthScore As Integer
Dim NumberScore As Integer
Dim CapsScore As Integer
Dim LowerScore As Integer
Dim SymbolScore As Integer

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    hidepasswordOption.Checked = True
    textboxPassword.PasswordChar = "*"
End Sub

Private Sub hidepasswordOption_Click(sender As Object, e As EventArgs) Handles hidepasswordOption.Click
    hidepasswordOption.Checked = Not hidepasswordOption.Checked
    If textboxPassword.PasswordChar = "*" Then
        textboxPassword.PasswordChar = ""
    Else
        textboxPassword.PasswordChar = "*"
    End If
End Sub

Private Sub buttonCheck_Click(sender As Object, e As EventArgs) Handles buttonCheck.Click
    Score = 0
    NumberScore = 0
    SymbolScore = 0
    LowerScore = 0
    CapsScore = 0

    Password = textboxPassword.Text

    If String.IsNullOrEmpty(Password) Then
        MsgBox("You must enter a password!", 16, "Error!")
        Exit Sub
    End If

    Checked = True

    Check_Password()

    progressbarStrength.Value = 0
    progressbarStrength.Maximum = 20
    progressbarStrength.Minimum = 0
    labelScoreText.Text = Score & "/20"
    progressbarStrength.Value = Score

    Progbar_calc()

    labelStrengthText.Text = PasswordStrength

End Sub


Public Sub Progbar_calc()

    If Score <= 10 Then
        PasswordStrength = "Weak"
        SendMessage(progressbarStrength.Handle, 1040, 2, 0)
    ElseIf Score <= 15 Then
        PasswordStrength = "Medium"
        SendMessage(progressbarStrength.Handle, 1040, 3, 0)
    ElseIf Score <= 20 Then
        PasswordStrength = "Strong"
        SendMessage(progressbarStrength.Handle, 1040, 1, 0)
    End If
End Sub

Public Sub Check_Password()
    Password = textboxPassword.Text
    PasswordLength = Len(Password)
    ImprovePass.Password = textboxPassword.Text

    Dim array() As Char = Password.ToCharArray()
    For i = 0 To array.Length - 1
        If Char.IsUpper(array(i)) Then
            If CapsScore >= 3 Then
            Else
                CapsScore = CapsScore + 1
            End If
        ElseIf Char.IsLower(array(i)) Then
            If LowerScore >= 3 Then
            Else
                LowerScore = LowerScore + 1
            End If
        ElseIf Char.IsNumber(array(i)) Then
            If NumberScore >= 3 Then
            Else
                NumberScore += 1
            End If
        ElseIf Char.IsSymbol(array(i)) Then
            If SymbolScore >= 3 Then
            Else
                SymbolScore += 1
            End If
        End If
    Next

    Score += CapsScore
    Score += LowerScore
    Score += NumberScore
    Score += SymbolScore

    If PasswordLength >= 8 Then
        Score += 8
    Else
        Score += PasswordLength
    End If

    If CapsScore = 0 Then
        ImprovePass.listboxImprove.Items.Add("You should include some capital letters.")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    ElseIf CapsScore = 1 Then
        ImprovePass.listboxImprove.Items.Add("You have included 1 capital letter,")
        ImprovePass.listboxImprove.Items.Add("You should include some more capital letters.")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    ElseIf CapsScore >= 2 Then
        ImprovePass.listboxImprove.Items.Add("You have included 2 or more capital letters!")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    End If
    If LowerScore = 0 Then
        ImprovePass.listboxImprove.Items.Add("You should include some lower case letters.")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    ElseIf LowerScore = 1
        ImprovePass.listboxImprove.Items.Add("You have included 1 lower case letter,")
        ImprovePass.listboxImprove.Items.Add("You should include some more lowercase letters.")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    ElseIf LowerScore >= 2 Then
        ImprovePass.listboxImprove.Items.Add("You have included 2 or more lower case letters!")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    End If
    If NumberScore = 0 Then
        ImprovePass.listboxImprove.Items.Add("You should include some numbers.")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    ElseIf NumberScore = 1 Then
        ImprovePass.listboxImprove.Items.Add("You have included 1 number,")
        ImprovePass.listboxImprove.Items.Add("You should include some more numbers.")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    ElseIf NumberScore >= 2 Then
        ImprovePass.listboxImprove.Items.Add("You have included 2 or more numbers!")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    End If
    If SymbolScore = 0 Then
        ImprovePass.listboxImprove.Items.Add("You should include some symbols.")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    ElseIf SymbolScore = 1 Then
        ImprovePass.listboxImprove.Items.Add("You have included 1 symbol,")
        ImprovePass.listboxImprove.Items.Add("You should include some more symbols.")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    ElseIf SymbolScore >= 2 Then
        ImprovePass.listboxImprove.Items.Add("You have included 2 or more symbols!")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    End If
    If PasswordLength <= 6 Then
        ImprovePass.listboxImprove.Items.Add("You have only included" & Space(1) & PasswordLength & Space(1) & "characters,")
        ImprovePass.listboxImprove.Items.Add("You should include at least 3 more to make your password more secure!")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    ElseIf PasswordLength <= 8 Then
        ImprovePass.listboxImprove.Items.Add("You have only included" & Space(1) & PasswordLength & Space(1) & "characters,")
        ImprovePass.listboxImprove.Items.Add("You should include at least 4 more to make your password more secure!")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    ElseIf PasswordLength >= 11 Then
        ImprovePass.listboxImprove.Items.Add("You have included 11 or more characters!")
        ImprovePass.listboxImprove.Items.Add("-------------------------------------")
    End If

    'MsgBox("Upper: " & CapsScore & "Lower: " & LowerScore & "Number: " & NumberScore & "Symbol: " & SymbolScore)

End Sub


Private Sub improvePassMenu_Click(sender As Object, e As EventArgs) Handles improvePassMenu.Click
    ImprovePass.Show()
    If textboxPassword.PasswordChar = "*" Then
        ImprovePass.labelPassText.Text = "(Hidden)"
    Else
        ImprovePass.labelPassText.Text = Password
    End If
End Sub

Private Sub textboxPassword_TextChanged(sender As Object, e As EventArgs) Handles textboxPassword.TextChanged
    Score = 0
    NumberScore = 0
    SymbolScore = 0
    LowerScore = 0
    CapsScore = 0
    Password = textboxPassword.Text
    PasswordLength = Len(Password)

    Select Case PasswordLength
        Case < 6
            buttonCheck.Enabled = False
            labelScoreText.Text = "Too Short"
            labelStrengthText.Text = "Too Short"
        Case > 12
            buttonCheck.Enabled = False
            labelScoreText.Text = "Too Long"
            labelStrengthText.Text = "Too Long"
        Case Else
            buttonCheck.Enabled = True
            labelScoreText.Text = ""
            labelStrengthText.Text = ""
    End Select

End Sub
End Class

Progress bar works when strength is strong:

enter image description here

Progress bar works on medium only when it is the first (or possibly second) check since the program has been opened:

enter image description here

When it isn't the first (or possibly second) check it doesn't work:

enter image description here

Progress bar works on weak only when it is the first (or possibly second) check since the program has been opened:

enter image description here

When it isn't the first (or possibly second) check it doesn't work:

enter image description here

I need this to work EVERY TIME no matter how many passwords you enter.

Any ideas why it might not be working? Thanks,

Jack Pollock
  • 344
  • 1
  • 13
  • What is this doing? `SendMessage(progressbarStrength.Handle, 1040, 2, 0)` I have no idea what message 1040 is. – Eric J. Jan 20 '16 at 20:11
  • @EricJ. This is code to change the colour of the progressbar. I am not sure entirely what the 1040 means as I got that line of code from a friend who is doing the same project who it works for. Is there a better way I could be going about changing the progressbar's colour? – Jack Pollock Jan 20 '16 at 20:13
  • the PInvoke signature is not quite right. see http://stackoverflow.com/a/33438255/1070452 – Ňɏssa Pøngjǣrdenlarp Jan 20 '16 at 20:22
  • @Plutonix No, that didn't fix it. The progressbar still refuses to show a value or colour after a few checks unless the password is strong. I had a thought that maybe it could be something to do with the if's that select the strength and set the colour? – Jack Pollock Jan 20 '16 at 20:37
  • Thats not exactly how that control is meant to be used (which is why you have to PInvoke). It would be just as easy to color in a portion of a PictureBox or Label – Ňɏssa Pøngjǣrdenlarp Jan 20 '16 at 21:05
  • @Plutonix Right... So would you maybe be able to correct my code so it works for me? I am not very good at VB and your answer that you linked me kind of confused me a little. Would this be possible? I would greatly appreciate it if you could. – Jack Pollock Jan 20 '16 at 21:09
  • I cant fix it - it works may 99% of the time on mine. I cant tell what the conditions are for failure. But like I said it isnt supposed to be used that way (notice that yellow and red do not animate? it doesnt look that good). If you are open to *other solutions* you have to put that in the question. – Ňɏssa Pøngjǣrdenlarp Jan 20 '16 at 21:21
  • @Plutonix I am perfectly happy to look into other solutions if you have any? I have updated the question. – Jack Pollock Jan 20 '16 at 21:24

1 Answers1

2

If anyone has any other solutions...

That control is not meant to be used that way. It seems to work ok for me, but I dont know what values you are using before the time it fails.

A simple rectangular meter using a PictureBox:

'form level variables:
Private pValue As Double
Private pColor1 As Color

Then when you evaluate the score (I used a track bar):

    pValue = track1.Value / 20

    Select Case track1.Value
        Case Is <= 10
            pColor1 = Color.Red           
        Case Is <= 15
            pColor1 = Color.Yellow     
        Case Else
            pColor1 = Color.LimeGreen 
    End Select
    ' pb2 is the picturebox
    pb2.Invalidate()

Then in the paint event:

If pValue = 0 Then Exit Sub

Dim rect = New Rectangle(0, 0,
                        Convert.ToInt32(pb2.Width * pValue),
                        pb2.Height)
' single color version
Using br As New SolidBrush(pColor1)
    e.Graphics.FillRectangle(br, rect)
End Using

Its kind of wonky because it is always red for the first HALF of the fill, Yellow or Green for about 25% (but I left your scaling alone).


For a gradient - no way your teacher will think you figured this out - you need a few small changes. The vars:

Private pColor1 As Color
Private pColor2 As Color
Private pValue As Double

The evaluation:

pValue = track1.Value / 20
Select Case track1.Value
    Case Is <= 10
        pColor1 = Color.Red
        pColor2 = Color.MistyRose

    Case Is <= 15
        pColor1 = Color.Red
        pColor2 = Color.Yellow

    Case Else
        pColor1 = Color.Yellow
        pColor2 = Color.LimeGreen

End Select
pb2.Invalidate()

pColor1 is the gradient start color, pcolor2 is the end color. Play with them to see what looks best. The paint event:

If pValue = 0 Then Exit Sub

Dim rect = New Rectangle(0, 0,
                        Convert.ToInt32(pb2.Width * pValue),
                        pb2.Height)

Using br As New LinearGradientBrush(rect, pColor1, pColor2, LinearGradientMode.Horizontal)
    e.Graphics.FillRectangle(br, rect)
End Using

Result:

enter image description here

On mine, I also have a text indicator for the result. It's still Red/WEAK for 50% rather than 33% of the time. Easy to scale if you wanted to.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • That looks like a really good alternative. (Btw I was never going to tell my teacher that I figured it out on my own, I was of course going to tell her i had some help from stack overflow) Although, do you think you would be able to show me (by editing my code) where each bit of code would go? Sorry, I am not that good at Visual Basic. xD Thanks again. – Jack Pollock Jan 20 '16 at 21:45
  • No I cannot edit the question to fix it. The vars would go up at the top with your `Dim` statements. The evaluation to set them replaces your ProgressBar code. the drawing part goes in the PictureBox paint event. Open the menus at the top of the code editor - pick your picturebox from the left, the paint event from the right – Ňɏssa Pøngjǣrdenlarp Jan 20 '16 at 21:49
  • Ok, I will try that, Thank You – Jack Pollock Jan 20 '16 at 21:50
  • It is giving me errors saying that 'pb2' , 'LinearGradientBrush' and 'LinearGradientMode' are all not defined? – Jack Pollock Jan 20 '16 at 22:02
  • you have to add `Imports System.Drawing.Drawing2D` to the top - I forgot about that, sorry. As for pb2 - look at the code and my comments: `pb2 is the picturebox` use whatever the name is for yours – Ňɏssa Pøngjǣrdenlarp Jan 20 '16 at 22:04
  • When I enter a password neither the trackbar nor the picture box are changed to the password strength... (I will leave my code in a new answer) – Jack Pollock Jan 20 '16 at 22:13
  • It's fine I figured it out. I forgot to assign the trackbar the value of the score! Thank you very much for your help. – Jack Pollock Jan 20 '16 at 22:28
  • Quick follow up question, how did you go about putting the text into the picture box? I am trying `g.DrawString("Strong", New Font("Arial", 5), Brushes.Black, x, y)` at the moment but it is not working. (Yes I have declared g, x and y. `Dim g As Graphics = pictureboxStrength.CreateGraphics() Dim x As Integer = 121 Dim y As Integer = 11` – Jack Pollock Jan 20 '16 at 22:45
  • I cheated - I added a label to the control. I can see your deleted post below - you have a lot of unused variables and code in your app now. You might want to clean that up. To draw it you'd have to calculate the location each time – Ňɏssa Pøngjǣrdenlarp Jan 20 '16 at 22:55
  • I didn't even think of just using a label. That makes much more sense than the code I was using before. Thanks. – Jack Pollock Jan 20 '16 at 23:02
  • Well it isnt that simple - you have to add it to picbox.controls, then set it to dock.fill (in form load) – Ňɏssa Pøngjǣrdenlarp Jan 20 '16 at 23:04
  • Oh, I was just going to put a label over the top of it but I see now that the background is not transparent... Even when I change the label BackColor to Transparent it still has the grey behind the text.... How would I go about implementing the code you suggested? – Jack Pollock Jan 20 '16 at 23:06
  • I already told you - add it to picbox.controls == `pb2.Controls.Add(lblScore)` set it to dock.fill == `lblScore.Dock = DockStyle.Fill` do that in form load event (also set the text align to center middle) – Ňɏssa Pøngjǣrdenlarp Jan 20 '16 at 23:09
  • Ok, Thank You, Sorry I completely missed when you said it before. (I'm really tired) Also how do I make it so the label shows in the center of the picture box? I have done `pb2.Controls.Add(lblScore)` and `lblScore.Dock = DockStyle.Fill` (both in form_load) and set the label's text align to middle center. – Jack Pollock Jan 20 '16 at 23:16
  • thats all I did. adding to controls makes the PB the parent so it's BG shows thru; DockFill helps with centering, makes the label the same size as the PB. MiddleCenter puts the text in the center of the label == Center of picture box. double check everything – Ňɏssa Pøngjǣrdenlarp Jan 20 '16 at 23:19
  • Everything is set as you said. I have got it looking correct now. I basically have just added a load of spaces before the label's text is put in so the text from the label is just pushed to the center. Thank you very much for your help. – Jack Pollock Jan 20 '16 at 23:28