C# - 在 4 秒内显示加载 1-100%

发布于 2024-08-08 23:48:58 字数 132 浏览 4 评论 0原文

我在启动屏幕上有一个标签,显示时间为 4 秒。我试图让标签以百分比形式显示加载过程。显然,这只是为了向用户表明程序实际上正在启动,而不是实际“加载”任何东西。有没有办法让标签在 4 秒内显示百分比(从 1% 到 100%)?有点不知道如何做到这一点。

I have a label on a splash screen that is displayed for 4 seconds. I am trying to make the label display the loading process as a percentage. Obviously, this is just to show the user that the program is actually starting up and not actually "loading" anything. Is there a way that I can have the label display the percentage (going from 1% to 100%) within 4 seconds? A bit lost on how to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半仙 2024-08-15 23:48:58

在窗体上放置一个 Timer 控件,并将其 Interval 属性设置为 40,将其 Enabled 属性设置为 true。创建一个如下所示的表单级变量:

private int _Progress = 0;

在 Timer 的 Tick 事件中,输入以下代码:

if (_Progress < 100)
{
    _Progress++;
    label1.Text = _Progress.ToString() + "%";
}
else
{
    timer1.Enabled = false;
}

Timers 并不是真正精确到毫秒,因此这不会花费 4 秒,但它会完成这项工作。

Put a Timer control on the form, and set its Interval property to 40 and its Enabled property to true. Create a form-level variable like this:

private int _Progress = 0;

In the Timer's Tick event, put this code:

if (_Progress < 100)
{
    _Progress++;
    label1.Text = _Progress.ToString() + "%";
}
else
{
    timer1.Enabled = false;
}

Timers aren't really accurate to the millisecond, so this won't take exactly 4 seconds, but it will do the job.

红焚 2024-08-15 23:48:58

假设您正在谈论 WinForms(而不是 WPF),最简单的方法是计时器控件。将超时设置为 40 毫秒(4 秒 = 4000 毫秒。4000 毫秒/100 次更新 = 40 毫秒)。创建一个类级整数来跟踪进度。那么 OnTick 事件的代码将如下所示......

if(progress < 100)
{
  progress++;
  progessLabel.Text = String.Format("Loading...  Progress: {0}%", progress);
}
else
{
  timer.Enabled = false;
}

Assuming you're talking WinForms (not WPF), the simplest way would be a timer control. Set the timeout for 40 ms (4 secs = 4000 ms. 4000 ms/100 updates = 40 ms). Create a class-level integer for tracking progress. Then your code for the OnTick event would look something like this...

if(progress < 100)
{
  progress++;
  progessLabel.Text = String.Format("Loading...  Progress: {0}%", progress);
}
else
{
  timer.Enabled = false;
}
浅笑轻吟梦一曲 2024-08-15 23:48:58

将时间间隔设置为 100 毫秒的计时器将是最简单的方法。记录调用计时器事件的次数,并每次更新进度条 2.5%。

虽然这可行,但我想说进度条对于这种情况并不理想。相反,只有动画图形会更好,因为它可以指示您的程序正在启动,但不会像进度条那样产生误导。

我认为微软经常犯这样的错误:在某些应用程序中使用误导性的进度条。

A timer with the interval set to say 100 milliseconds would be the simplest approach. Keep a count of the number of times the timer event is called and update the progress bar by 2.5 percent each tick.

While this would work, I'd say that a progress bar is not ideal for this situation. Instead just an animated graphic would be better as it gives an indication that your program is starting up, but does not mislead like a progress bar can.

I think Microsoft regularly make this mistake of using misleading progress bars in certain applications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文