0

I need to draw a graph in C# by ZedGraph in VS2013. I have created a windowns application project and added Form1.cs as win forms file. But, when I ran the code, only an empty form was created but no graph. In debug mode, I found that Form1_Load() is not executed. Why ?

This is the C# code.

using System.Windows.Forms;
using ZedGraph;
namespace zedgraph_test
{
  public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }
    // it is not executed.
    private void Form1_Load(object sender, EventArgs e)
    {
        Console.WriteLine("Form1_Load is called");
        ZedGraph.ZedGraphControl zg1 = new ZedGraphControl();
        CreateChart(zg1);
    }
    // definition of CreateChart
    ...
}

Any help would be appreciated.

UPDATE

I have tried the solution but I only got an empty form and no charts displayed in the form.

This is code of From1.cs.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
namespace zedgraph_test1
{
  public partial class Form1 : Form
  {
      public Form1()
      {
         InitializeComponent();
      }

      private void Form1_Load(object sender, EventArgs e)
      {
        Console.WriteLine("Form1_Load is called");
        ZedGraph.ZedGraphControl zg1 = new ZedGraphControl();
        CreateChart(zg1);
        SetSize();
      }

      private void Form1_Resize(object sender, EventArgs e)
      {
        SetSize();
      }

      private void SetSize()
      {
         zg1.Location = new Point(10, 10);
        // Leave a small margin around the outside of the control
        zg1.Size = new Size(this.ClientRectangle.Width - 20, this.ClientRectangle.Height - 20);
      }


      // Call this method from the Form_Load method, passing your ZedGraphControl
     public static void CreateChart(ZedGraphControl zgc)
     {
         GraphPane myPane = zgc.GraphPane;

        // Set the title and axis labels
        myPane.Title.Text = "Vertical Bars with Value Labels Above Each Bar";
        myPane.XAxis.Title.Text = "Position Number";
        myPane.YAxis.Title.Text = "Some Random Thing";

        PointPairList list = new PointPairList();
        PointPairList list2 = new PointPairList();
        PointPairList list3 = new PointPairList();
        Random rand = new Random();

        // Generate random data for three curves
        for (int i = 0; i < 5; i++)
        {
            double x = (double)i;
            double y = rand.NextDouble() * 1000;
            double y2 = rand.NextDouble() * 1000;
            double y3 = rand.NextDouble() * 1000;
            list.Add(x, y);
            list2.Add(x, y2);
            list3.Add(x, y3);
        }

        // create the curves
        BarItem myCurve = myPane.AddBar("curve 1", list, Color.Blue);
        BarItem myCurve2 = myPane.AddBar("curve 2", list2, Color.Red);
        BarItem myCurve3 = myPane.AddBar("curve 3", list3, Color.Green);

        // Fill the axis background with a color gradient
        myPane.Chart.Fill = new Fill(Color.White,
           Color.FromArgb(255, 255, 166), 45.0F);

        zgc.AxisChange();

        // expand the range of the Y axis slightly to accommodate the labels
        myPane.YAxis.Scale.Max += myPane.YAxis.Scale.MajorStep;

        // Create TextObj's to provide labels for each bar
        BarItem.CreateBarLabels(myPane, false, "f0");
        //Console.ReadLine();
    }
    private void zg1_Load(object sender, EventArgs e)
    {

    }
  }
}

More update I have added zedgraphControl by adding zedgraph.dll under object relational design in toolbox of VS2013.

But, all components in my toolbox are all grayed out in VS2013.

When I ran the code, I still got an empty form.

I tried the project at http://www.codeproject.com/Articles/5431/A-flexible-charting-library-for-NET

I can get the chart even though all components in my toolbox are also all grayed out in VS2013.

user3601704
  • 753
  • 1
  • 14
  • 46
  • 1
    Did you assign the event handler in the properties of the form? It's not enough to just write a method called `Form1_Load`.. – Blorgbeard Sep 03 '15 at 21:55
  • Copy the guts of your Form1_Load, delete the whole method and then double click the form and add the event again to make sure the event got wired. Then copy the guts back in. – thewisegod Sep 03 '15 at 21:56
  • I am new to win form. How to add the event ? and how to make sure that the event got wired ? – user3601704 Sep 03 '15 at 22:22

2 Answers2

0

Remove the following lines from Form1_Load and you are good to go:

Console.WriteLine("Form1_Load is called");
ZedGraph.ZedGraphControl zg1 = new ZedGraphControl();
thewisegod
  • 1,524
  • 1
  • 10
  • 11
  • Have you placed a breakpoint on your form load and confirmed that the code is being run now? – thewisegod Sep 04 '15 at 03:45
  • Yes, I put a breakpoint at private void Form1_Load() and confirmed that it was executed, but still an empty form. – user3601704 Sep 04 '15 at 03:59
  • When do you actually add the ZedGraphControl to your form? Did you drag and drop it there? – thewisegod Sep 04 '15 at 04:04
  • In toolbox, I drag and drop zedgraphControl to the form1 when I opened the Form1.cs by double clicking it. Then, I changed the added graph size. But, the charts cannot be displayed in the graph. It is still empty. – user3601704 Sep 04 '15 at 04:17
  • Please see my updated answer and it should work for you. I copied your code and downloaded Zed and found the issue. Please up-vote and mark as answer. – thewisegod Sep 04 '15 at 12:15
  • No, it does not work. I still got an empty graph with XY scales but no chart shown. Also, for a win-form Console.WriteLine() doesnot work. thanks – user3601704 Sep 04 '15 at 13:10
  • OK, could you please zip your project and send it to me at, will4job@yahoo.com. I will get it working and then send it back to you. – thewisegod Sep 04 '15 at 13:14
  • I don't have it in my inbox. Did you send it to will4job@yahoo.com? – thewisegod Sep 04 '15 at 14:23
-1

I guess you didn't add the event handler to Form1_Load event in Form1's Properties window (at the right bottom of the designer window), you can either do that or move CreateChart(zg1) into Form1's constructor, like this:

    public Form1()
    {
        InitializeComponent();
        CreateChart(zg1);
    }
obnews
  • 602
  • 5
  • 13
  • 3
    This is just a suggestion, not an answer. Besides that, there's little chance that the user still needs it or will respond. – Gert Arnold Jan 02 '21 at 14:50
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Donald Duck Jan 02 '21 at 16:52
  • 1
    @Kenly It does attempt to provide an answer to the question. It might not be very high quality, but that's not a reason to delete it. If you don't like it, you can downvote and/or comment, but deletion is only for stuff that doesn't even attempt to answer the question (like "thanks" or "I'm having this problem too"). See [this post by a moderator](https://meta.stackoverflow.com/q/287563/4284627). – Donald Duck Jan 02 '21 at 16:54
  • @DonaldDuck This does not answer the question and should be deleted. The options are listed when you click on the `Recommend Deletion` button. It is always possible to add an answer after this one is deleted. – Kenly Jan 02 '21 at 17:05
  • @Kenly It *attempts* to answer the question, so it shouldn't be deleted. If it's wrong or not useful, downvote it. To quote [this post by a moderator](https://meta.stackoverflow.com/q/287563/4284627): "An answer might not have description for why the code works, but it still shouldn't be deleted; just leave a comment asking the author for an explanation, and move on." – Donald Duck Jan 02 '21 at 17:15
  • @DonaldDuck It is a suggestion and not an answer. – Kenly Jan 02 '21 at 19:04
  • @Kelly Yes, it's a suggestion and not really an answer. But the "not an answer" flag only applies when the post doesn't even attempt to answer the question. It does, but "try this" is too tentative and vague (esp. when no one will ever try). We have enough of that kind of answers and I wish people wouldn't post them. – Gert Arnold Jan 02 '21 at 19:47
  • @GertArnold I updated my suggestion, I think it's now a real answer. – obnews Aug 03 '22 at 00:59