Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I just created a Graphics Chart by code that works with an OlrDb access base and works well.
C#
Chart Graph_121 = new Chart(); // Créer un Chart vide
ChartArea ChartArea121 = new ChartArea(); // ChartArea (zone graphique)
Graph_121.ChartAreas.Add(ChartArea121);  // Ajoute le Chart Area à la Collection ChartAreas du Chart
Series series121 = new Series();  // Création serie (qui contiendront les DataPoints)


My question is the following :

In the same form I want to add a button and delete with the method Clear my Chart, how to do?

Nothing works :
C#
Graph_121.Clear();
ChartArea121.Clear();
series121.Clear();

The name Graph_121 does not exist in the current context !
Can we declare Graph_121, ChartArea121, series121 in public?

thank you

What I have tried:

C#
  Control Page_Principale = Graph_121.Page_Principale;
Page_Principale.Controls.Remove(Graph_121);

Graph_121.Clear();
ChartArea121.Clear();
series121.Clear();
Posted
Updated 15-Mar-19 21:34pm
v2
Comments
Richard MacCutchan 15-Mar-19 11:00am    
Make sure your variable is not declared inside a method, when it is referenced in other methods.
Richard Deeming 21-Mar-19 14:42pm    
Based on the additional information in your not-a-solution solution, you've declared fields at the top of your class, and you've then declared local variables in one method which have the same name as those fields. You assign values to the local variables, but you never assign anything to the fields.

You're effectively handing an envelope to someone called "Bob", and then walking up to a different person who is also called "Bob" and expecting them to have your envelope.

Looks like your "chart" is going "out of scope" if you created it "by code" as above.

Define your chart "at the class level" as a "private or public" member so that different parts of your program can "see" chart and not just the method that first created chart (as a "local" variable).
 
Share this answer
 
Hello, 
Thanks for your help. I just created a method like this:

C#
  public void MyGraph_121()
        {
            string CentreAnnee = Filtre_Cbo_Annee.Text;

            Chart Graph_121 = new Chart();
            ChartArea ChartArea121 = new ChartArea(); 
            Graph_121.ChartAreas.Add(ChartArea121); 
                      Series series121 = new Series
            {
                IsValueShownAsLabel = true, 
                ChartArea = "ChartArea1"      
            };  
            
            Graph_121.Series.Add(series121); 

// rest of the code

      }

Then I said at the top of my page like this:

C#
public int i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14;
        public string CentreAnnee;
        public ChartAreaCollection Graph_121, ChartArea121, series121;

Then once the graph displayed I test with this button:

C#
private void button1_Click(object sender, EventArgs e)
{

    ChartArea121.Clear();
    series121.Clear();
    ChartArea121.Clear();
}

I have as an error message:

C#
System.NullReferenceException : 
'Object reference not set to an instance of an object.'
ChartArea121 was null.

How to declare Chart items?

C#
public ChartAreaCollection Graph_121, ChartArea121, series121;

Is it objects, string does not go, int either?

thank you
 
Share this answer
 
Comments
Richard Deeming 21-Mar-19 14:40pm    
If you want to update your question to add missing information, click the green "Improve question" link and edit your question. DO NOT post your update as a "solution" to your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900