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();
        }

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 であるというエラーが発生します。

4

2 に答える 2

1

あなたの最初の問題は、タイプがUserControl何であるかがわからないことです。Form1フォームの名前空間を含めるには、ファイルの先頭に using ステートメントを配置する必要がありますWindowsFormApplication1。また、更新された例では、InitializeComponentメソッドを呼び出すことはないため、チャートを作成することはありません。

パラメーターなしのコンストラクターを使用する場合は、次のようなことを試すことができます: ( Default Constructor への InitializeComponent メソッドの追加と、2 つの追加メソッドの追加に注意してください。またSetupGraphSetFormコードをGraphChart_LoadイベントハンドラーからSetupGraphメソッドに移動しました。これは機能します。コンストラクターで Form1 を渡すSetForm場合と、呼び出す前に使用する限り、パラメーターなしのコンストラクターを使用する場合の両方SetupGraph)

ユーザーコントロール

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);

    }
}

フォーム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();
    }

}
于 2013-03-14T02:40:15.293 に答える
0

クラスに引数を 0 とるコンストラクタを与える必要があるため、クラス GraphChart に次の行を追加してみてください。

public GraphChart(){}
于 2013-03-14T02:30:17.630 に答える