【问题标题】:ProgressBar to increase by the value in a NumericUpDown boxProgressBar 按 NumericUpDown 框中的值增加
【发布时间】:2015-02-25 22:27:40
【问题描述】:

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

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

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

    }

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

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

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)
    {

    }
}
}

【问题讨论】:

  • 进度条的最大值是多少?肯定是 60 左右吧?
  • 它的设定值已经是 40,最大值是 100,我希望输入的数字加到这个 40
  • sixtyfree 看起来像 NumericUpDown 控件。如果文件大小从不改变,那么进度条将始终递增,直到达到最大值。我认为你想要if (sixtyfree.Value + filesize &lt;= 60)
  • 如果您要根据文件大小递增,那么您认为哪个值适合进度条的最大值。以这种方式使用它没有任何意义。正确的用法是将进度条的最大值设置为文件大小,并将进度条的值设置为您从文件中读取的字节数。并做到这一点,直到你阅读所有内容。
  • 好吧,我补充说: if (sixtyfree.Value

标签: c# winforms progress-bar numericupdown


【解决方案1】:

我使用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();
}

【讨论】:

  • 我已经添加了这个,它很有魅力,但是有 60 个空闲插槽,可以说我添加 40 进度条会增加,但是如果我之后添加另一个值,可以说 20 它应该完成进度条,但是在我按下按钮后,无论我输入什么值,都没有再次发生。
  • @user3596350 你指的是什么按钮?您在哪里添加 40 和 20 值?你能展示你的代码吗?
  • 好的,我已将我的完整代码添加到我的原始代码中,抱歉,我无意编辑您的代码并将其添加到其中。基本上在 NumericUpDown 框下方有一个按钮,当按下时,用户输入的数字将添加到进度条,直到达到最大值
【解决方案2】:

使用 ProgressBar.Value = 文件大小;

【讨论】:

    猜你喜欢
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多