C# 根据进度条生成随机数的步骤

C# 根据进度条生成随机数的步骤,c#,C#,我正在尝试显示与进度条关联的随机数。我有2个进度条和2个labelbox。当第一个进度条完成到100%时,相应的labelbox应使用该函数显示随机数,第二个进度条应启动。一旦第二个进度条完成到100%,相应的标签盒应显示随机数。 我有一个代码,但它首先处理两个进度条,然后显示两个随机数。我想一次做一个。即第一个进度条和第一个labelbox,然后是第二个进度条和第二个labelbox public partial class Form1 : Form { public Form1()

我正在尝试显示与进度条关联的随机数。我有2个进度条和2个labelbox。当第一个进度条完成到100%时,相应的labelbox应使用该函数显示随机数,第二个进度条应启动。一旦第二个进度条完成到100%,相应的标签盒应显示随机数。 我有一个代码,但它首先处理两个进度条,然后显示两个随机数。我想一次做一个。即第一个进度条和第一个labelbox,然后是第二个进度条和第二个labelbox

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        progressBar1.Visible = false;
        progressBar2.Visible = false;




    }

    Random rnd = new Random();
    private void random1()
    {

        int t = rnd.Next(200);
        label1.Text = t.ToString();

    }
    private void random2()
    {
        int t = rnd.Next(1500);
        label2.Text = t.ToString();

     }

    private void button1_Click(object sender, EventArgs e)
    {

        int i;

        progressBar1.Minimum = 0;
        progressBar1.Maximum = 4000;
        progressBar1.Visible = true;



            for (i = 0; i <= 4000; i++)
            {

                progressBar1.Value = i;

                int percent = (int)(((double)(progressBar1.Value - progressBar1.Minimum) /
 (double)(progressBar1.Maximum - progressBar1.Minimum)) * 100);
                using (Graphics gr = progressBar1.CreateGraphics())
                {
                    gr.DrawString(percent.ToString() + "%",
                        SystemFonts.MessageBoxFont,
                        Brushes.Black,
                        new PointF(progressBar1.Width / 2 - (gr.MeasureString(percent.ToString() + "%",
                            SystemFonts.MessageBoxFont).Width / 2.0F),
                        progressBar1.Height / 2 - (gr.MeasureString(percent.ToString() + "%",
                            SystemFonts.MessageBoxFont).Height / 2.0F)));
                }


            }
            progressBar1.Visible = false;
            random1();





                progressBar2.Minimum = 0;
                progressBar2.Maximum = 3000;
                progressBar2.Visible = true;



                for (i = 0; i <= 3000; i++)
                {

                    progressBar2.Value = i;

                    int percent = (int)(((double)(progressBar2.Value - progressBar2.Minimum) /
     (double)(progressBar2.Maximum - progressBar2.Minimum)) * 100);
                    using (Graphics gr = progressBar2.CreateGraphics())
                    {
                        gr.DrawString(percent.ToString() + "%",
                            SystemFonts.MessageBoxFont,
                            Brushes.Black,
                            new PointF(progressBar2.Width / 2 - (gr.MeasureString(percent.ToString() + "%",
                                SystemFonts.MessageBoxFont).Width / 2.0F),
                            progressBar2.Height / 2 - (gr.MeasureString(percent.ToString() + "%",
                                SystemFonts.MessageBoxFont).Height / 2.0F)));
                    }

                }
                progressBar2.Visible = false;
                random2();    

    }
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
progressBar1.Visible=false;
progressBar2.Visible=false;
}
随机rnd=新随机();
私人1(
{
int t=下一个rnd(200);
label1.Text=t.ToString();
}
私人图书馆2(
{
int t=下一个rnd(1500);
label2.Text=t.ToString();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
int i;
progressBar1.最小值=0;
progressBar1.最大值=4000;
progressBar1.Visible=true;

对于(i=0;i我已经查看了您的整个代码,发现您只需要在random1()方法中刷新页面。我现在已经完成了,在第一个标签中完成第一个进度条后,标签中将显示random no,在完成第二个进度条后,第二个标签中将显示random number

public Form1()
    {
        InitializeComponent();              
        progressBar1.Visible = false;
        progressBar2.Visible = false;
    }

     Random rnd = new Random();
     private void random1()
     {
        int t = rnd.Next(200);
        label1.Text = t.ToString();
        this.Refresh();
     }
     private void random2()
     {
        int t = rnd.Next(1500);
        label2.Text = t.ToString();
     }

     private void button1_Click_1(object sender, EventArgs e)
     {
        int i;
        progressBar1.Minimum = 0;
        progressBar1.Maximum = 4000;
        progressBar1.Visible = true;
        for (i = 0; i <= 4000; i++)
        {
            progressBar1.Value = i;
            int percent = (int)(((double)(progressBar1.Value - progressBar1.Minimum) /(double)(progressBar1.Maximum - progressBar1.Minimum)) * 100);
            using (Graphics gr = progressBar1.CreateGraphics())
            {
                gr.DrawString(percent.ToString() + "%",
                    SystemFonts.MessageBoxFont,
                    Brushes.Black,
                    new PointF(progressBar1.Width / 2 - (gr.MeasureString(percent.ToString() + "%",
                        SystemFonts.MessageBoxFont).Width / 2.0F),
                    progressBar1.Height / 2 - (gr.MeasureString(percent.ToString() + "%",
                        SystemFonts.MessageBoxFont).Height / 2.0F)));
            }


        }
        progressBar1.Visible = false;         
        random1();

            progressBar2.Minimum = 0;
            progressBar2.Maximum = 3000;
            progressBar2.Visible = true;
            for (i = 0; i <= 3000; i++)
            {
                progressBar2.Value = i;
                int percent = (int)(((double)(progressBar2.Value - progressBar2.Minimum) / (double)(progressBar2.Maximum - progressBar2.Minimum)) * 100);
                using (Graphics gr = progressBar2.CreateGraphics())
                {
                    gr.DrawString(percent.ToString() + "%",
                        SystemFonts.MessageBoxFont,
                        Brushes.Black,
                        new PointF(progressBar2.Width / 2 - (gr.MeasureString(percent.ToString() + "%",
                            SystemFonts.MessageBoxFont).Width / 2.0F),
                        progressBar2.Height / 2 - (gr.MeasureString(percent.ToString() + "%",
                            SystemFonts.MessageBoxFont).Height / 2.0F)));
                }

            }
            progressBar2.Visible = false;

            random2();                      

     }
public Form1()
{
初始化组件();
progressBar1.Visible=false;
progressBar2.Visible=false;
}
随机rnd=新随机();
私人1(
{
int t=下一个rnd(200);
label1.Text=t.ToString();
这个。刷新();
}
私人图书馆2(
{
int t=下一个rnd(1500);
label2.Text=t.ToString();
}
私有无效按钮1\u单击\u 1(对象发送者,事件参数e)
{
int i;
progressBar1.最小值=0;
progressBar1.最大值=4000;
progressBar1.Visible=true;

对于(i=0;i您要查找的搜索词是。先生,我尝试在random1()中添加此.Refresh();但现在它根本不显示labelbox1的任何内容。2个进度条将完成到100%,然后只有Labelbox2的随机数显示,但labelbox1SORRY中没有显示任何内容…实际上我没有调用random1()…我知道了…它现在正在工作…非常感谢…你是我的导师…-)亲爱的,没问题,最后你得到了你的答案是很重要的。唯一的一件事是你先投反对票,所以即使答案是正确的,但它显示了反对票,如果可能的话,请更改。我没有这样做…我试图投反对票…但由于你需要15个声誉,所以会出现错误-(