user3516803
user3516803

Reputation:

Button must be clicked twice for second event to happen vb

I have a program which generates a password when a button is clicked. The code for the button is as follows:

Private Sub generate_Click(sender As Object, e As EventArgs) Handles generate.Click
    Module1.firstname = firstnameb.Text
    Module1.surname = secondnameb.Text
    Module1.colour = colourb.Text
    Module1.dob = dobb.Text
    Module1.dod = dodb.Text
    Module1.password = ((Mid$(Module1.firstname, 1, 1)) & (Mid$(Module1.surname, 1, 1)) & (Mid$(Module1.colour, 1, 1)) & (Mid$(Module1.dob, 1, 1)) & (Mid$(Module1.dod, 1, 1)))

    Timer1.Start()
    If ProgressBar1.Value < 100 Then
        ProgressBar1.Value += 1
    ElseIf ProgressBar1.Value = ProgressBar1.Maximum Then
        passwordresultb.Text = Module1.password

    End If


End Sub

However, I have to press the button twice: once for the progress bar and a second time for the password to come in a box.

Any help would be much appreciated.

Upvotes: 0

Views: 922

Answers (2)

Bradley Uffner
Bradley Uffner

Reputation: 17001

You need to move that whole If block inside the timer's Tick event.

Upvotes: 1

Michael
Michael

Reputation: 1833

When you click your button, this method will be called once. If the ProgressBar.Value is less than 100, it gets incremented. Otherwise, the text box gets updated with the password. Only one of these will happen per call, since you use the If..ElseIf pattern.

Your intentions are not very clear given the provided code. I can't tell how you are using the progress bar, or what for. But the root of your problem is the If..ElseIf statement. If you simply want the password to show in the text box when the button is clicked, remove the ProgressBar bit and just update the textbox.

If you want your progress bar to update to 'maximum' before displaying the password, try this (My VB is a little rusty... but this should be it):

While ProgressBar1.Value < ProgressBar1.MaximumValue
    ProgressBar.Value += 1
End While

passwordresultb.Text = Module1.password

Upvotes: 0

Related Questions