如何将form1变量传递给用户控件类?

时间:2013-03-14 02:23:20

标签: c#

我有这个用户控制代码,用户控件是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 designer 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为空。

2 个答案:

答案 0 :(得分:1)

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

如果你想使用无参数构造函数,你可以尝试这样的事情:( 注意将DefaultizeComponent方法添加到Default Constructor并添加另外两个方法SetupGraph和{{1}我还将代码从SetForm事件处理程序移到了GraphChart_Load方法。这既可以在构造函数中传递Form1,也可以在您使用SetupGraph之前使用无参数构造函数。致电SetForm

<强>用户控件

SetupGraph

<强> Form1中

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 :(得分:0)

你需要给一个带0个参数的构造函数,所以尝试将以下行添加到GraphChart类中:

public GraphChart(){}