我有一个在form_load事件上具有某些代码行的表单,因此需要一些时间才能显示出来。我想添加一个进度条,以显示已加载了多少百分比的表单。我该怎么办..?
简而言之:-就像浏览网页时一样,网络浏览器中显示的进度表明页面的值已加载。我想在vb.net中这样做。
我该怎么办??
答案 0 :(得分:0)
您需要将长时间运行的代码移到表单的.Shown
事件处理程序中。
我建议您在表单中添加StatusStrip
,并在表单中添加ToolStripProgressBar
。我在编写的程序中做到了这一点,并在代码上使用了以下代码。
这完全是自我解释:-)
''' <summary>
''' Sets minimum and maximum values for ToolStripProgressBar and shows it
''' </summary>
''' <param name="min">Minimum Value</param>
''' <param name="max">Maximum Value</param>
''' <param name="labeltext">Text to show in ToolStripLabel</param>
Private Sub SetAndShowProgressBar(min As Integer, max As Integer, labeltext As String)
ToolStripStatusLabel1.Text = labeltext
ToolStripProgressBar1.Minimum = min
ToolStripProgressBar1.Maximum = max + 5
ToolStripProgressBar1.Value = 1
ToolStripProgressBar1.Visible = True
StatusStrip1.Update()
End Sub
''' <summary>
''' Hides ToolStripProgressBar
''' </summary>
Private Sub HideProgressBar()
ToolStripStatusLabel1.Text = ""
ToolStripProgressBar1.Visible = False
StatusStrip1.Update()
End Sub
Private Delegate Sub incprogressbarDelegate()
Private Sub IncrementProgressBar()
If Me.InvokeRequired Then
Dim d As New incprogressbarDelegate(AddressOf IncrementProgressBar)
Me.Invoke(d, New Object())
Else
Me.ToolStripProgressBar1.Value += 1
StatusStrip1.Update()
End If
End Sub
Private Delegate Sub SetProgressBarDelegate(i As Integer)
Private Sub SetProgressBarValue(i As Integer)
If Me.InvokeRequired Then
Dim d As New SetProgressBarDelegate(AddressOf SetProgressBarValue)
Me.Invoke(d, New Object() {i})
Else
Me.ToolStripProgressBar1.Value = i
End If
End Sub