C# 如何将form1变量传递给用户控件类?
我有这个用户控制代码,用户控制是GraphChart:C# 如何将form1变量传递给用户控件类?,c#,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
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();
}
public 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为空的应用程序时,我收到一个错误。您需要为类提供一个接受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();
}
public GraphChart(){}
第一个问题是您的
UserControl
不知道Form1
类型是什么。在我的例子中,我使用WindowsFormApplication1
对表单名称空间进行了测试,尽管它将是您使用的名称空间。在更新的示例中,您从未调用InitializeComponent
方法,因此您从未创建图表
如果要使用无参数构造函数,可以尝试以下操作:(注意将InitializeComponent方法添加到默认构造函数中,并添加了两个附加方法SetupGraph
和SetForm
,我还将代码从GraphChart\u Load
eventhandler中移到SetupGraph
方法中。这对构造函数中的传递Form1和usin都有效。)g无参数构造函数,只要在调用SetupGraph
之前使用SetForm
)
用户控制
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);
}
}
你能显示不起作用的语法吗?Fendy当我在用户控制代码中添加form1和f1时,出现了错误。没有不起作用的语法。只是需要我将form1传递到GraphChart,但我不能这样做。你的应用程序使用的名称空间是什么Form1@user2065612那么非工作语法最初必须是GraphChart graphChart1=new GraphChart();但没关系。现在开始处理第二个问题,您能给我们“调用者”吗语法?@user2065612我所说的调用方语法不仅仅是这个。graphChart1.chart1.MouseMove+=chart1_MouseMove;等等。相反,请提供这个的初始化。graphChart1;Terry我做了。我看了我的问题,我正在更新它。现在这个错误消失了,但我有另一个问题。