0

I have a graph that has lines plotted on it from different lists. I have added several checkboxes and used if statements to check if the checkbox is clicked. Like so:

public Form1()
{           
    InitializeComponent();
    CreateGraph();
    this.Text = "HRM File Analyser";

    if (hrCheck.Checked)
    {
        drawHR();
        chart1.Update();
    }
    else if (!hrCheck.Checked)
    {
        chart1.Series["Heart Rate"].Points.Clear();
        chart1.Update();
    }
}  

However, even after calling the update method nothing changes at all.

Anyone have any ideas?

TaW
  • 53,122
  • 8
  • 69
  • 111
Matt Murphy
  • 265
  • 2
  • 11
  • Is the else condition functioning properly? Can you show the contents of drawHR()? – Bobby Mar 03 '16 at 23:31
  • Also, are these checkboxes declared on this form? If so, you can't use their check-state in a conditional expression in the form's constructor. You would want to add a checked event handler for the checkboxes and put your conditional code in there. – Bobby Mar 03 '16 at 23:42

1 Answers1

0

The first problem with the code you show us is that is seems to be interactive but everything (we see) really happens only once upon startup.

  • Instead you should code the CheckChanged event of the Checkbox! (You get to it by double-clicking the checkbox in the designer..)

  • Next I suggest not to clear away i.e. delete all the data. Instead simply disable the series. Now you can turn the series off or on..

  • The checkbox checking logic is also overblown; one check will be sufficient!

So the new and improved version would look like this:

public Form1()
{           
    InitializeComponent();
    CreateGraph();
    this.Text = "HRM File Analyser";
    hrCheck_CheckedChanged(null, null);   // set initial state
    ..
}

private void hrCheck_CheckedChanged(object sender, EventArgs e)
{
    chart1.Series["Heart Rate"].Enabled = hrCheck.Checked;
}

If you didn't create it by double-clicking, do not forget to hook up the hrCheck_CheckedChanged event!

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111