2013-11-22 83 views
0

我試圖讓我的進度條一直到100%,但它會更新到85-95%左右,然後打開我的下一個表格。有人可以指導我做錯了什麼嗎?我試圖編輯這個步驟以及睡眠計數,但是我可以設法將它靠近進度條的末尾。是否有一些代碼干擾進度條?Visual Studio 2012 - 進度條

Public Class LoginForm 
' 
'This specifies the default value for the login attempt 
Dim Attempt = 0 
Public Function checkinput() As Boolean 
    ' 
    'This is the default username 
    Dim Uname = "Niral Mehta" 
    ' 
    'This is the default password 
    Dim pword = "Ban4na" 
    ' 
    'This assigns the value of the Username to the UserText.text variable 
    Dim Username = UserText.Text 
    ' 
    'This assigns the value of the Password to the PassText.text variable 
    Dim Password = PassText.Text 
    ' 
    'Here the Username and password are being assigned the values defined above, if the user input correctly matches the defined values then it returns as true 
    If Username = Uname And Password = pword Then 
     Return True 
    Else 
     ' 
     'If the user fails to put in the correct values on the third attempt then the program automatically shuts down after warning them 
     If Attempt = 3 Then 
      MsgBox("You have failed to login correctly three times, this program will shut down as a security measure", MsgBoxStyle.OkOnly) 
      Me.Close() 
      Return False 
     End If 
     Attempt = Attempt + 1 
     MsgBox("Incorrect username and/or password", MsgBoxStyle.OkOnly) 
     Return False 
    End If 
End Function 

Private Sub KillProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KillProcess.Click 
    ' 
    'When the end program button is clicked, a message box will pop up and give the options to end the program 
    If MsgBox("Are you sure you want to quit?", MsgBoxStyle.YesNo) = DialogResult.Yes Then 
     Me.Close() 
    End If 
End Sub 

Private Sub PassText_TextChanged(sender As Object, e As EventArgs) Handles PassText.TextChanged 
    ' 
    'This hides the password characters 
    PassText.PasswordChar = "#" 

    'This sets the character length for a password 
    ' 
    PassText.MaxLength = 8 
End Sub 

Private Sub UserText_TextChanged(sender As Object, e As EventArgs) Handles UserText.TextChanged 
    ' 
    'This sets the character length for a username 
    UserText.MaxLength = 14 
End Sub 

Private Sub StartNextForm_Click(sender As Object, e As EventArgs) Handles StartNextForm.Click 
    If checkinput() Then 
     With LoginProgress 
      .Visible = True 
      .Step = 2 
      .Maximum = 101 
      .Style = ProgressBarStyle.Blocks 
      .Value = 0 
     End With 
     Do 
      System.Threading.Thread.Sleep(100) 
      LoginProgress.PerformStep() 
     Loop Until LoginProgress.Value >= LoginProgress.Maximum 
     FrmMain.Show() 
     Me.Hide() 
    End If 
End Sub 
+0

可能重複的[進度重置爲60%](http://stackoverflow.com/questions/11494976/progressbar-resets-at-60) – GSerg

回答

0

使用計時器您可以使用此代碼:

定時器代碼:

Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick 
    If prgStatus.Value = prgStatus.Maximum Then 
     Timer.Enabled = False 
     prgStatus.Value = 0 
     Exit Sub 
    End If 
    prgStatus.Value = prgStatus.Value + 1 
    If (prgStatus.Value = 1) Then 
     'Do something 
    End If 
End Sub 

注:在做某些事情的所有代碼 - 必須下

prgStatus.Value = prgStatus.Value + 1 

啓動計時器,只是放在一個按鈕的代碼,形式負載等

Timer.Enabled = True 

你可以儘可能多的

 If (prgStatus.Value = 1) Then 
     'Do something 
    End If 

如你所願。

要延遲它只需更改進度條可以更改的最大值或更改計時器記號。

希望幫助:)

-1

,因爲你是在(佔用)您的UI線程,而不是讓UI線程消息泵隨時處理事件

嘗試增加Application.DoEvents()

運行循環您的活動不被處理
Do  
    System.Threading.Thread.Sleep(100) 
    LoginProgress.PerformStep() 
    Application.DoEvents() ' <-- this 
Loop Until LoginProgress.Value >= LoginProgress.Maximum 
+0

假設downvote是爲'DoEvents',請記住我沒有設計軟件,也不會用這樣的循環來阻止UI線程。在這種情況下,DoEvents應該解決UI未更新的問題,但我並不試圖教授任何正確的線程化應用程序。 – djv