在vb.net中使用进度条 [英] Using progress bar in vb.net

查看:485
本文介绍了在vb.net中使用进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有示例代码,用于进度条。我需要显示循环的每个项目的进度(百分比和条形)



对于i = 0到200000

ListBox1.Items。添加(i)

下一页



我已经尝试了所有东西(后台工作人员等),但无法弄明白。



我要求别人给我写完整的代码(背景工程,交叉线程等等)并帮助我让它运行。



非常感谢。

I have sample code below, to use with progress bar. I need to display progress with each item of loop (percentage and bar both)

For i = 0 To 200000
ListBox1.Items.Add(i)
Next

I have tried everything (background workers etc.) but could not figure it out.

I request someone to write me complete code (backgroundworkder, Cross Threading and whatever) and help me make it run.

Thanks a lot in advance.

推荐答案

当你在UI工作时为什么要使用backgroundworker - 只需更新循环中的进度条。



这篇CP文章展示了如何在进度条上显示文本(完成百分比) - 将百分比(或任何文本)添加到标准进度条控件 [ ^ ]



正如文章所在C#我已经为你转换了
As you are working in the UI anyway why use a backgroundworker - just update the progress bar within the loop.

This CP article shows how to display text (percentage complete) "on" the progress bar - Add the Percent (or Any Text) into a Standard Progress Bar Control[^]

As the article is in C# I've converted it for you
Private Sub UpdateProgressBar(ByVal percent As Integer)

    Dim percentText As String = percent.ToString() + "%"
    Dim font As Font = SystemFonts.DefaultFont
    ProgressBar1.Value = percent

    Using gr = ProgressBar1.CreateGraphics()
        Dim X As Single = ProgressBar1.Width / 2 - (gr.MeasureString(percentText, font).Width / 2.0F)
        Dim Y As Single = ProgressBar1.Height / 2 - (gr.MeasureString(percentText, font).Height / 2.0F)
        gr.DrawString(percentText, font, Brushes.Black, New PointF(X, Y))
    End Using
End Sub

我使用以下代码

ListBox1.Items.Clear()

Dim maxValue = 200000
Dim displayInterval = 5000
ProgressBar1.Maximum = 100

For i = 0 To maxValue
    ListBox1.Items.Add(i)
    If i Mod displayInterval = 0 Then
        UpdateProgressBar(i * 100 \ maxValue)
    End If
Next

请注意,我没有更改每个循环的显示 - 没有太多意义 - 请参阅上面的 displayInterval 。玩弄你的价值,直到你得到你想要的效果

Note that I don't change the display for every single loop - there is not much point - see displayInterval above. Play about with the value until you get the effect you desire


这篇关于在vb.net中使用进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆