-
Mar 28th, 2018, 07:56 AM
#1
Thread Starter
Addicted Member
ProgressBar with negative numbers
I have an application that controls an environmental test chamber and I'm using a progress bar to show the progress of the temperature changing from its current value to the set point. I have two functions to update the progressbar, depending on whether the temperature is increasing or decreasing (I want left to right indication in either case). One of these functions is called once per second as the current temperature is read from the chamber:
Code:
// methods to display progress bar, going up or down
void UpdateProgressbarIncreasing(float start_temp, float end_temp, float current_temp) // increasing temperature
{
progressBar1.Minimum = (int)start_temp;
progressBar1.Maximum = (int)end_temp;
// if current value is > maximum, abort
if(current_temp >= progressBar1.Maximum) // this is because the temperature can overshoot setpoint a bit
{
progressBar1.Value = progressBar1.Maximum;
return;
}
progressBar1.Value = (int)current_temp;
}
void UpdateProgressBarDecreasing(float start_temp, float end_temp, float current_temp) // decreasing value
{
progressBar1.Minimum = (int)end_temp; // decreasing value
progressBar1.Maximum = (int)start_temp; // upper value
int invertedvalue = progressBar1.Maximum - (int)current_temp;
// cover for over temp
if (progressBar1.Minimum + invertedvalue > progressBar1.Maximum)
{
progressBar1.Value = progressBar1.Maximum;
return;
}
progressBar1.Value = progressBar1.Minimum + invertedvalue;
}
This works perfectly as long as the temperatures are above zero. But if I want to go from +25 to -40 or -40 to +25 I get an exception. It seems you can't assign ProgressBar.Minimum to a negative value.
Can someone look at my code please and suggest a solution?
Thanks.
-
Mar 28th, 2018, 08:00 AM
#2
Re: ProgressBar with negative numbers
If the ProgressBar doesn't support negative values then you can also normalize the temperatures to Kelvin.
I don't know whether you are using the Celsius or Fahrenheit scales for your current values.
But converting from one to another is just a basic formula.
-
Mar 28th, 2018, 05:45 PM
#3
Re: ProgressBar with negative numbers
You should be able to set the min max of the progress bar to a fixed range and normalize the value yourself. Shouldn't need two procedures, just pass the start and end temperatures. When the range is calculated it will either be a positive or negative range, but will always progress start to end (left to right) when normalized.
Code:
void UpdateProgressbar(float start_temp, float end_temp, float current_temp) // changing temperature
{
float temp_range = end_temp - start_temp;
int bar_value;
//This could be set in the IDE rather than here
progressBar1.Minimum = 0;
progressBar1.Maximum = 1000;
bar_value = (int)((current_temp - start_temp) / temp_range) * 1000;
bar_value = Math.Min(Math.Max(bar_value, 0), 1000); //limit value to 0 to 1000 range
progressBar1.Value = bar_value;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|