我有表格,我将在下面显示。我希望进度条在onLoad事件上慢慢运行以进行测试,以确保它有效。
我是否可以只使用此代码循环显示进度条值?
问题:为什么进度条不会在onLoad()上递增?
progressBar1.Value = 0; //start at zero value
int n = 100;
double progress = 0;
//This loop should increment the progress bar.
for (int i = 1; i <= n; i++)
{
if (progress <= 100)
{
progressBar1.Value = (int)progress;
}
}
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace TestDifferentForms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
progressBar1.Value = 0;
int n = 100;
double progress = 0;
for (int i = 1; i <= n; i++)
{
if (progress <= 100)
{
progressBar1.Value = (int)progress;
}
}
}
}
}
图像:
答案 0 :(得分:3)
存在多个问题:
progress
的值永远不会改变答案 1 :(得分:2)
因为您永远不会增加进度的值。您只需将其设置为0
,然后一次又一次将其分配给 progressBar1 的值。
答案 2 :(得分:2)
您没有递增progress
变量。试试这个:
for (int progress = 0; i <= n; progress++)
{
progressBar1.Value = progress;
}