我在VB.Net中创建了一个启动画面,并且除了我想要在加载过程中每隔x秒更改多个文本的加载文本时,每个方法都很完美。
例如:Dim LoadingTexts As String = {"charging 1...", "charging 2...", "info1..."}
我不知道我怎么能做到这一点除了我肯定必须使用计时器......但是如何?
实际代码:
Public Class frmSplashScreen
Private stringTable() As String = {"Shovelling coal into the server...", "Programming the flux capacitor...", _
"Searching for answer to live, the universe and everything...",
"Waiting for Godot...", "Starting..."}
Private stringMove As Integer = 0
Sub New()
InitializeComponent()
End Sub
Public Overrides Sub ProcessCommand(ByVal cmd As System.Enum, ByVal arg As Object)
MyBase.ProcessCommand(cmd, arg)
End Sub
Public Enum SplashScreenCommand
SomeCommandId
End Enum
Public Sub SplashTimer_Tick(sender As Object, e As EventArgs) Handles SplashTimer.Tick
Me.SplashTimer.Enabled = False
Me.labelStarting.Text = stringTable(stringMove)
Me.labelStarting.Refresh()
stringMove += 1
If stringMove < stringTable.Length Then Me.SplashTimer.Enabled = True
End Sub
End Class
感谢。
答案 0 :(得分:3)
假设您有一个计时器,进度条,按钮和标签,进度条的步长值设置为10,您的代码可能如下所示:
Public Class Form1
Private Sub butStart_Click(sender As System.Object, e As System.EventArgs) Handles butStart.Click
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(ProgressBar1.Step) 'use method below as well
'ProgressBar1.Value+=ProgressBar1.Step
Me.Refresh()
If ProgressBar1.Value >= 0 AndAlso ProgressBar1.Value < 35 Then
Label1.Text = ProgressBar1.Value & "% Starting"
Me.Refresh()
ElseIf ProgressBar1.Value >= 35 AndAlso ProgressBar1.Value < 75 Then
Label1.Text = ProgressBar1.Value & "% Mid value"
Me.Refresh()
ElseIf ProgressBar1.Value >= 75 AndAlso ProgressBar1.Value < 100 Then
Label1.Text = ProgressBar1.Value & "% Finishing"
Me.Refresh()
ElseIf ProgressBar1.Value = 100 Then
Timer1.Enabled = False
'Move to next form or something
End If
End Sub
End Class
答案 1 :(得分:1)
如果需要每X秒更换一次,请使用计时器。更改标签的.Text
属性:
Dim iLoadText As Integer = 0
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Label1.Text = LoadingTexts(iLoadText)
Label1.Refresh()
iLoadText += 1
If iLoadText < LoadingTexts.Length Then Timer1.Enabled = True
End Sub