我有這樣的用戶控制代碼的用戶控制是圖形圖表:如何將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的應用程序時,出現錯誤。
可否請您展現不工作語法? – Fendy 2013-03-14 02:24:59
Fendy一旦我在用戶控件代碼中添加了form1和f1,錯誤就出現了。沒有沒有工作的語法。這只是需要我將Form1傳遞給GraphChart,但我無法做到。 – user2065612 2013-03-14 02:30:11
什麼是您的Form1使用的命名空間 – 2013-03-14 02:43:08