ProgressBar 增加 NumericUpDown 框中的值

ProgressBar to increase by the value in a NumericUpDown box

我正在用 C# 编写一个表单,您可以在其中的 NumericUpDown 框中输入一个值,当单击下方的按钮时,进度条将增加输入的值。

我为此使用了一个计时器,代码为:

 private void ProgressBar_Tick(object sender, EventArgs e)
    {
        if (filesize <= 60)
            sixtyfree.Increment(filesize);

    }

filesize 是 NumericUpDown 框的名称,我已将此值转换为 int 以便它可以使用它。我面临的问题是,无论我输入多少低于 60 的数字,它都会一直填充进度条,而不仅仅是在 "filesize" 中输入的数量,谁能帮我解决这个问题?

当然可以,这是我的完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Worst_fit_algorithm_GUI
{
public partial class Form1 : Form
{

    int filesize;
    int progressBarSetValue = 40; //The set value of first progressBar
    public Form1()
    {
        InitializeComponent();
    }

    private void downloadfile_Click(object sender, EventArgs e)
    {
        this.ProgressBar.Start();


    }

    private void ProgressBar_Tick(object sender, EventArgs e)
    {
        if (sixtyfree.Value <= sixtyfree.Maximum - progressBarSetValue)
        {
            sixtyfree.Value = progressBarSetValue + (int)filesize;
            ProgressBar.Stop();
        }


    }

    private void FileSize_ValueChanged_1(object sender, EventArgs e)
    {
        filesize = Convert.ToInt32(FileSize.Value);

    }

    private void sixtyfree_Click(object sender, EventArgs e)
    {

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }
}
}

使用 ProgressBar.Value = 文件大小;

我使用了ValueChanged事件来设置进度条。您可以像之前那样使用计时器:

int progressBarSetValue = 40; //The set value of progressBar you want

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    if (numericUpDown1.Value <= progressBar1.Maximum - progressBarSetValue)
    {
        progressBar1.Value = progressBarSetValue + (int)numericUpDown1.Value;
    }
}

代码更通用,这意味着您不必使用固定的 60 值。只要 progressBarSetValue 不大于 progressBar1.Maximum.

,您就可以更改进度条的最大值而不关心 numericupdown 值是否会溢出进度条。

编辑

numericUpDown 控件名称 = FileSize,progressBar 控件名称 = sixtyfree

private void ProgressBar_Tick(object sender, EventArgs e)
{
    if (filesize <= sixtyfree.Maximum - sixtyfree.Value)
    {
        sixtyfree.Value += filesize;
    }
    else
    {
        sixtyfree.Value = sixtyfree.Maximum;
    }

    ProgressBar.Stop();
}