Maximum value in the track bar

2.2k views Asked by At

I have a button that adds +100 to the track Bar. Maximum value 43000, if the value is at 43000 and clicking the button will give error.

Value '43001' is not valid for 'Value'. 'Value' must be between 'Minimum' and 'Maximum'.

private void button41_Click(object sender, EventArgs e)     
{
    trackBar1.Value = trackBar1.Value += 100;      
    label27.Text = "" + trackBar1.Value; 
} 

Issue resolved:

  public Form1()
        {
            me = this;
            InitializeComponent();
            trackBar1.Maximum = 43000;
            trackBar1.Minimum = 40;


        }

button

private void button41_Click(object sender, EventArgs e)
{
    if (trackBar1.Value + 100 <= trackBar1.Maximum)
    {
         trackBar1.Value = trackBar1.Value += 100;
        label27.Text = "Frequency = " + trackBar1.Value;
    }
    else
    {
        MessageBox.Show("Max value = " + trackBar1.Maximum);
    }
}

42990 + 100 without errors if I click add

Message displayed when trying add more than the supported value

1

There are 1 answers

1
Flat Eric On BEST ANSWER

The message already says all: The value may not be greater than the max-value.

Just add a condition before you increment the value:

if (trackBar1.Value < trackBar1.Maximum)
    trackBar1.Value++; 

Or here your complete event handler:

private void button41_Click(object sender, EventArgs e)
{
    if (trackBar1.Value < trackBar1.Maximum)
    {
        trackBar1.Value++;
        label27.Text = trackBar1.Value;
    }
    else
    {
        MessageBox.Show("Max value = " + trackBar1.Maximum);
    }
}