0

我在 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
        }
4

2 回答 2

1

看起来您的问题是,当您调用chart1.Series.Clear()它时,它会清除所有索引,chart1然后当您再次单击以填充图表时,您将永远不会再次创建“销售”索引。您需要将“销售”图表添加到图表中,Series然后才能对其调用方法。

于 2013-05-17T21:30:49.130 回答
1

您的代码取决于“销售”系列,如下例所示:

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);

但是,可能当您清除图表时,您也在清除图表的系列,因此,第二次,图表无法识别系列“销售”

要更正此问题,您需要检查系列是否存在,如果为空则创建它。像这样的东西:

if (this.chart1.Series.Count == 0)
{
    // Here you need to properly create the series
    this.chart1.Series.Add(new Series("Sales"));
}

这是一个通用示例,可​​能无法编译,因为我不知道您使用的系列类型或图表控件的类型。所以一定要相应地改变。

如果你不知道如何调用本系列的构造函数,看一下 Designer.cs 文件,复制这段代码中的构造函数和属性就可以了。

于 2013-05-17T21:34:10.560 回答