my windowsform in c# has a graph. when i enter the data for the first time, the graph works fine, and when i click clear, everything clears up. the problem is when i enter data for the 2nd time andtry to load the graph c# then gives me error.
private void button2_Click(object sender, EventArgs e)
{
//Graph codes
if (p == 1)
{
if (t == 1)
{
if (txtFirstActual.Text == "" || txtSecondActual.Text == "" || txtThirdActual.Text == "" || txtFourthActual.Text == "")
{
MessageBox.Show("Enter All Value Of Actual Years");
}
else
{
int m = Convert.ToInt32(txtForecastingYear.Text);
int FirstYearValue = m - 4;
txtFirstYear.Text = FirstYearValue.ToString();
int SecondYearValue = m - 3;
txtSecondYear.Text = SecondYearValue.ToString();
int ThirdYearValue = m - 2;
txtThirdYear.Text = ThirdYearValue.ToString();
int FourthYearValue = m - 1;
txtFourthYear.Text = FourthYearValue.ToString();
double FirstActual = Convert.ToDouble(txtFirstActual.Text);
double SecondActual = Convert.ToDouble(txtSecondActual.Text);
double ThirdActual = Convert.ToDouble(txtThirdActual.Text);
double FourthActual = Convert.ToDouble(txtFourthActual.Text);
if (FirstActual == 0 && SecondActual != 0 && ThirdActual != 0 && FourthActual != 0) //if the user entered 3 years instead of 4
{
double sum = (SecondActual + ThirdActual + FourthActual) / 3;
listBox1.Items.Clear();
listBox1.Items.Add(sum);
this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);
this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);
this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
this.chart1.Series["Sales"].Points.AddXY("Year", sum);
}
else if (FirstActual != 0 && SecondActual == 0 && ThirdActual != 0 && FourthActual != 0)//if the user entered 3 years instead of 4
{
double sum = (FirstActual + ThirdActual + FourthActual) / 3;
listBox1.Items.Clear();
listBox1.Items.Add(sum);
this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);
this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);
this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
this.chart1.Series["Sales"].Points.AddXY("Year", sum);
}
else if (FirstActual != 0 && SecondActual != 0 && ThirdActual == 0 && FourthActual != 0)//if the user entered 3 years instead of 4
{
double sum = (FirstActual + SecondActual + FourthActual) / 3;
listBox1.Items.Clear();
listBox1.Items.Add(sum);
this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);
this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);
this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
this.chart1.Series["Sales"].Points.AddXY("Year", sum);
}
else if (FirstActual != 0 && SecondActual != 0 && ThirdActual != 0 && FourthActual == 0)//if the user entered 3 years instead of 4
{
double sum = (FirstActual + SecondActual + ThirdActual) / 3;
listBox1.Items.Clear();
listBox1.Items.Add(sum);
this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);
this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);
this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);
this.chart1.Series["Sales"].Points.AddXY("Year", sum);
}
else if (FirstActual != 0 && SecondActual != 0 && ThirdActual != 0 && FourthActual != 0)//if the user entered 4 years
{
double sum = (FirstActual + SecondActual + ThirdActual + FourthActual) / 4;
listBox1.Items.Clear();
listBox1.Items.Add(sum);
this.chart1.Series["Sales"].Points.AddXY("Year-4", FirstActual);
this.chart1.Series["Sales"].Points.AddXY("Year-3", SecondActual);
this.chart1.Series["Sales"].Points.AddXY("Year-2", ThirdActual);
this.chart1.Series["Sales"].Points.AddXY("Year-1", FourthActual);
this.chart1.Series["Sales"].Points.AddXY("Year", sum);
}
else if (FirstActual != 0 && SecondActual != 0 && ThirdActual != 0 && FourthActual != 0)//if the user entered 4 years
{
double sum = (FirstActual + SecondActual + ThirdActual + FourthActual) / 4;
listBox1.Items.Clear();
listBox1.Items.Add(sum);
MessageBox.Show(" Can't drow graph because there are no sales");
}
}
if (t == 0)
{
MessageBox.Show("Enter Actual for Years");
}
}
if (p == 0)
{
MessageBox.Show("Enter Wanted Year");
}
and that is my clear button: private void button1_Click(object sender, EventArgs e) { // clear button
listBox1.Items.Clear();
txtForecastingYear.Clear();
txtFirstYear.Clear();
txtSecondYear.Clear();
txtThirdYear.Clear();
txtFourthYear.Clear();
chart1.Series.Clear();
txtFirstActual.Clear();
txtSecondActual.Clear();
txtThirdActual.Clear();
txtFourthActual.Clear();
if (!String.IsNullOrEmpty(txtFirstActual.Text)) //if there is entry then make clear button available, if not the button will do nothing
{
// clear data
}
chart1
control? or calling.Dispose()
? – Evan L