绘制图形时不执行Form1_Load()
本文关键字:Load Form1 执行 图形 绘制 | 更新日期: 2023-09-27 18:11:01
我需要在VS2013中通过ZedGraph在c#中绘制图形。我创建了一个windows应用程序项目,并将form .cs添加为win forms文件。但是,当我运行代码时,只创建了一个空表单,而没有图形。在调试模式下,我发现Form1_Load()没有执行。为什么?
这是c#代码。
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
...
}
如有任何帮助,不胜感激。
我已经尝试了解决方案,但我只有一个空的形式,没有图表显示在形式。
是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)
{
}
}
}
更多更新我在VS2013的工具箱中,通过在对象关系设计下添加zedgraph.dll,添加了zedgraphControl。
但是,我的工具箱中的所有组件在VS2013中都是灰色的。
当我运行代码时,我仍然得到一个空表单。
我在http://www.codeproject.com/Articles/5431/A-flexible-charting-library-for-NET试过这个项目
我可以得到图表,即使我的工具箱中的所有组件在VS2013中也都是灰色的。
从Form1_Load中删除以下行,您就可以开始了:
Console.WriteLine("Form1_Load is called");
ZedGraph.ZedGraphControl zg1 = new ZedGraphControl();
我猜你没有在Form1的属性窗口(在设计器窗口的右下角)中添加事件处理程序到Form1_Load事件,你可以这样做或将CreateChart(zg1)
移动到Form1的构造函数中,像这样:
public Form1()
{
InitializeComponent();
CreateChart(zg1);
}