[英]How can i pass form1 variables to user control class?
I have this user control code the User Control is GraphChart: 我有此用户控件代码,用户控件为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();
}
After adding form1 and f1 variables im getting error: 在添加form1和f1变量后,我得到了错误:
Error 2 GatherLinks.GraphChart' does not contain a constructor that takes 0 arguments 错误2 GatherLinks.GraphChart'不包含带有0个参数的构造函数
When I make double click on the error it's moving to Form1 designer cs to the line: 当我双击错误时,它正在移到Form1 Designer cs的行:
this.graphChart1 = new GatherLinks.GraphChart();
I tried to put Form1 in between the () but it's not working getting an error. 我试图将Form1放在()之间,但出现错误无法正常工作。 How can I solve it ?
我该如何解决?
EDIT: 编辑:
I just did in the User Control code: 我只是在用户控制代码中做了:
public partial class GraphChart : UserControl
{
Form1 form1;
public GraphChart() { }
public GraphChart(Form1 f1)
{
InitializeComponent();
form1 = f1;
}
But now in Form1 constructor I have this lines: 但是现在在Form1构造函数中,我有以下几行:
this.graphChart1.chart1.MouseMove += chart1_MouseMove;
this.graphChart1.chart1.MouseLeave += chart1_MouseLeave;
They worked before fine but as soon as I added the line: public GraphChart() { } Im getting an error when running the application that chart1 is null . 它们可以正常工作,但是只要我添加以下行:public GraphChart(){}我在运行chart1为null的应用程序时遇到错误。
Your first problem is that your UserControl
has no idea what the Form1
type is. 您的第一个问题是您的
UserControl
不知道Form1
类型是什么。 You need to put a using statement at the top of your file to include your Forms Namespace, In my case I tested with WindowsFormApplication1
though it will be what ever namespace that you used. 您需要在文件顶部放置一个using语句,以包括您的Forms命名空间。就我而言,我使用
WindowsFormApplication1
进行了测试,尽管它将是您使用过的命名空间。 And in your updated example you are never calling your InitializeComponent
Method so you never create your Chart. 在更新的示例中,您永远不会调用
InitializeComponent
方法,因此也永远不会创建图表。
You can try something like this if you want to use a parameterless constructor: ( note the addition of the InitializeComponent method to the Default Constructor and the addition of two additional methods SetupGraph
and SetForm
I also moved the code out of the GraphChart_Load
eventhandler to the SetupGraph
Method. This works both with passing Form1 in the Constructor and using the Parameterless Constructor as long as you use SetForm
before you call SetupGraph
) 你可以尝试这样的事情,如果你想使用一个无参数的构造函数:( 注意添加InitializeComponent方法以默认的构造函数和另外的两个额外的方法
SetupGraph
和SetForm
我也感动的代码出的GraphChart_Load
事件处理程序的SetupGraph
方法。只要在调用SetupGraph
之前使用SetForm
就可以在构造函数中传递Form1并使用无参数构造函数。
UserControl 用户控件
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 表格1
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(){}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.