2013-03-14 81 views
0

我有这样的用户控制代码的用户控制是图形图表:如何将form1变量传递给用户控件类?

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的应用程序时,出现错误。

+0

可否请您展现不工作语法? – Fendy 2013-03-14 02:24:59

+0

Fendy一旦我在用户控件代码中添加了form1和f1,错误就出现了。没有没有工作的语法。这只是需要我将Form1传递给GraphChart,但我无法做到。 – user2065612 2013-03-14 02:30:11

+0

什么是您的Form1使用的命名空间 – 2013-03-14 02:43:08

回答

1

你的第一个问题是,你的UserControl不知道什么Form1类型是。你需要在你的文件的顶部放置一个using语句来包含你的Forms Namespace,在我的情况下,我使用WindowsFormApplication1进行了测试,不过这将是你使用的任何命名空间。在你更新的例子中,你永远不会打电话给你的InitializeComponent方法,所以你永远不会创建你的图表。

你可以尝试这样的事情,如果你想使用一个无参数的构造函数:(注:将InitializeComponent方法以默认的构造函数,并增加了两个额外的方法SetupGraphSetForm的我也感动的代码出来的GraphChart_Load事件处理程序的SetupGraph方法。这适用与在构造函数传递Form1中只要您使用SetForm你打电话SetupGraph

用户控件

0123之前,使用参数的构造函数
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); 

    } 
} 

Form1中

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(); 
    } 

} 
0

你需要给一个构造函数0参数类,所以尽量把下面几行添加到类图形图表:

public GraphChart(){} 
+0

特里我做了它看我的问题即时更新它现在这个错误已经消失,但我有另一个问题。 – user2065612 2013-03-14 02:36:03