Jack Pollock
Jack Pollock

Reputation: 334

Temperamental progressbar in VB.NET

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,

Upvotes: 1

Views: 699

Answers (1)

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.

Upvotes: 2

Related Questions