I am coding a form in C# in which you enter a value in a NumericUpDown box and when the button is clicked underneath the progress bar will increase by the value entered.
I used a timer for this with the code being :
private void ProgressBar_Tick(object sender, EventArgs e)
{
if (filesize <= 60)
sixtyfree.Increment(filesize);
}
filesize is the name of the NumericUpDown box, i have converted this value to an int so that it would work with this. The problem i face is that no matter what number under 60 i enter it fills the progress bar all the way, instead of only by the amount that is entered in "filesize", can anyone help me solve this ?
Yeah ofcourse, here is my full code:
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)
{
}
}
}
I used the ValueChanged
event to set the progressbar. You can use a timer as you have done:
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;
}
}
The code is more generic, meaning you don't have to use a fixed 60
value. You can change the progressbar maximum without caring if the numericupdown value will overflow the progressbar, as long as the progressBarSetValue
is not larger than the progressBar1.Maximum
.
EDIT
numericUpDown control name = FileSize, progressBar control name = 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();
}