我如何将form1变量传递给用户控制类
本文关键字:给用户 控制 变量 form1 | 更新日期: 2023-09-27 18:03:43
我有这个用户控件代码,用户控件是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;
他们工作之前很好,但一旦我添加行:当运行应用程序时,我得到一个错误,chart1为null。
您的第一个问题是您的UserControl
不知道Form1
类型是什么。你需要把一个using语句在你的文件的顶部,包括你的表单名称空间,在我的情况下,我用WindowsFormApplication1
测试,虽然它将是你使用的任何名称空间。在你更新的例子中,你从来没有调用你的InitializeComponent
方法,所以你从来没有创建你的图表。
如果您想使用无参数构造函数,您可以尝试这样做:(注意,向默认构造函数添加了InitializeComponent方法,并添加了两个额外的方法SetupGraph
和SetForm
。我还将代码从GraphChart_Load
事件处理程序移到了SetupGraph
方法。只要在调用SetupGraph
)
SetForm
,就可以在构造函数中传递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);
}
}
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个参数的构造函数,因此尝试将以下行添加到类GraphChart:
public GraphChart(){}