2013-03-14 80 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