我在 c# 中的 windowsform 有一个图表。当我第一次输入数据时,图表工作正常,当我单击清除时,一切都清除了。问题是当我第二次输入数据并尝试加载图形 c# 然后给我错误。
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");
}
那是我的清除按钮: 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
}