0

我有这个用户控件代码,用户控件是 GraphChart:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Windows.Forms.DataVisualization.Charting;

namespace GatherLinks
{

    public partial class GraphChart : UserControl
    {
        Form1 form1;

        public GraphChart(Form1 f1)
        {
            InitializeComponent();

            form1 = f1;

        }

        private void GraphChart_Load(object sender, EventArgs e)
        {
            chart1.Series.Clear();
            var series1 = new System.Windows.Forms.DataVisualization.Charting.Series
            {
                Name = "Series1",
                Color = System.Drawing.Color.Green,
                IsVisibleInLegend = false,
                IsXValueIndexed = true,
                ChartType = SeriesChartType.Line
            };

            this.chart1.Series.Add(series1);

            //for (int i = 0; i < 100; i++)
            //{
                series1.Points.AddXY(form1.axisX, form1.axisY);
            //}
            chart1.Invalidate();
        }

添加 form1 和 f1 变量后,我得到错误:

错误 2 GatherLinks.GraphChart' 不包含采用 0 个参数的构造函数

当我双击错误时,它移动到 Form1 设计器 cs 到该行:

this.graphChart1 = new GatherLinks.GraphChart();

我试图将 Form1 放在 () 之间,但它没有工作,出现错误。我该如何解决?

编辑:

我刚刚在用户控制代码中做了:

public partial class GraphChart : UserControl
    {
        Form1 form1;

        public GraphChart() { }
        public GraphChart(Form1 f1)
        {
            InitializeComponent();

            form1 = f1;

        }

但现在在 Form1 构造函数中,我有以下几行:

this.graphChart1.chart1.MouseMove += chart1_MouseMove;
this.graphChart1.chart1.MouseLeave += chart1_MouseLeave;

他们以前工作得很好,但是一旦我添加了以下行: public GraphChart() { } 我在运行 chart1 为 null 的应用程序时遇到错误。

4

2 回答 2

1

您的第一个问题是您UserControl不知道Form1类型是什么。您需要在文件顶部放置一条 using 语句以包含您的 Forms 命名空间,在我的情况下,我测试了它,WindowsFormApplication1尽管它将是您使用的任何命名空间。在您更新的示例中,您永远不会调用您的InitializeComponent方法,因此您永远不会创建图表。

如果您想使用无参数构造函数,可以尝试这样的事情:(注意将 InitializeComponent 方法添加到默认构造函数并添加两个附加方法SetupGraphSetForm我还将代码从事件处理程序GraphChart_Load移到SetupGraph方法中。这行得通在构造函数中传递 Form1 和使用无参数构造函数,只要你SetForm在调用之前SetupGraph使用)

用户控制

public partial class GraphChart : UserControl
{
    private Chart chart1;
    Form1 form1;
    public GraphChart()
    {
        InitializeComponent();
    }
    public GraphChart(Form1 f1)
    {
        InitializeComponent();
        form1 = f1;
        this.Load+=new EventHandler(GraphChart_Load);
    }
    public void SetForm( Form1 f1)
    {
        form1 = f1;
    }
    public void SetupGraph()
    {
        chart1.Series.Clear();
        var series1 = new System.Windows.Forms.DataVisualization.Charting.Series
        {
            Name = "Series1",
            Color = System.Drawing.Color.Green,
            IsVisibleInLegend = false,
            IsXValueIndexed = true,
            ChartType = SeriesChartType.Line
        };

        this.chart1.Series.Add(series1);

        //for (int i = 0; i < 100; i++)
        //{
        series1.Points.AddXY(form1.axisX, form1.axisY);
        //}
        chart1.Invalidate();
    }
    private void GraphChart_Load(object sender, EventArgs e)
    {
        SetupGraph();
    }

    private void InitializeComponent()
    {
        System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
        System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
        System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
        this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
        ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
        this.SuspendLayout();
        // 
        // chart1
        // 
        chartArea1.Name = "ChartArea1";
        this.chart1.ChartAreas.Add(chartArea1);
        legend1.Name = "Legend1";
        this.chart1.Legends.Add(legend1);
        this.chart1.Location = new System.Drawing.Point(0, 0);
        this.chart1.Name = "chart1";
        series1.ChartArea = "ChartArea1";
        series1.Legend = "Legend1";
        series1.Name = "Series1";
        this.chart1.Series.Add(series1);
        this.chart1.Size = new System.Drawing.Size(519, 473);
        this.chart1.TabIndex = 0;
        this.chart1.Text = "chart1";
        // 
        // GraphChart
        // 
        this.Controls.Add(this.chart1);
        this.Name = "GraphChart";
        ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
        this.ResumeLayout(false);

    }
}

表格1

public partial class Form1 : Form
{
    public int axisX = 100;
    public int axisY = 100;
    GatherLinks.GraphChart graphChart1;

    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        graphChart1 = new GatherLinks.GraphChart();
        this.Controls.Add(graphChart1);
        graphChart1.SetForm(this);
        graphChart1.SetupGraph();
    }

}
于 2013-03-14T02:40:15.293 回答
0

您需要为该类提供一个接受 0 个参数的构造函数,因此尝试将以下行添加到类 GraphChart:

public GraphChart(){}
于 2013-03-14T02:30:17.630 回答