簡體   English   中英

用於自定義控件的Visual C#停止代碼生成

[英]Visual C# Stop Code Generation For Custom Control

希望我可以對我的問題有所幫助,可能是一個簡單的解決方法。 我正在嘗試創建一個自定義圖表控件(從Chart類繼承)。 在我的構造函數中,我添加了一個ChartArea和Legend。 當我將控件添加到表單時,一切都很好,但是在運行它時,我在表單設計器中遇到一個錯誤,說已經存在一個圖表區域,其名稱與我在構造函數中添加的名稱相同。 所以我的問題是,圖表區域正試圖在表單設計器生成的代碼中第二次添加。 我可以從生成的代碼中刪除它,但是我想查看是否可以在自定義圖表類中控制它的簡便方法。 圖表的構造函數代碼為:

    public MultiFunctionalGraph(DataForGraph dataA, DataForGraph dataB, DataForGraph dataC, DataForGraph dataD)
    {
        this.dataA = dataA;
        this.dataB = dataB;
        this.dataC = dataC;
        this.dataD = dataD;

        ChartArea chartArea = new ChartArea();
        Legend legend = new Legend();
        Axis xAxis = new Axis(chartArea, AxisName.X);
        Axis yAxis = new Axis(chartArea, AxisName.Y);

        chartArea.Name = "ChartArea";
        chartArea.Visible = true;
        this.ChartAreas.Add(chartArea);
        legend.Name = "Legend";
        this.Legends.Add(legend);

    }

我在設計器的代碼生成中得到了這一點:

        this.Graph1 = Graph();
        ((System.ComponentModel.ISupportInitialize)(this.Graph1)).BeginInit();

        // Graph1
        // 
        this.Graph1.BackColor = System.Drawing.Color.Transparent;
        chartArea1.AxisX.Interval = 5D;
        chartArea1.AxisX.MajorGrid.Interval = 10D;
        chartArea1.AxisX.MajorTickMark.Interval = 5D;
        chartArea1.AxisY.LabelStyle.Interval = 500D;
        chartArea1.AxisY.MajorGrid.Interval = 500D;
        chartArea1.AxisY.MajorTickMark.Interval = 500D;
        chartArea1.AxisY.MinorGrid.Interval = 500D;
        chartArea1.AxisY.MinorTickMark.Interval = 500D;
        chartArea1.CursorX.LineColor = System.Drawing.Color.LimeGreen;
        chartArea1.CursorX.LineWidth = 2;
        chartArea1.CursorY.LineColor = System.Drawing.Color.LimeGreen;
        chartArea1.CursorY.LineWidth = 2;
        chartArea1.Name = "ChartArea";
        this.Graph1.ChartAreas.Add(chartArea1);
        legend1.AutoFitMinFontSize = 5;
        legend1.Name = "Legend";
        legend1.TextWrapThreshold = 20;
        this.Graph1.Legends.Add(legend1);
        this.Graph1.Location = new System.Drawing.Point(46, 302);
        this.Graph1.Name = "Graph1";
        this.Graph1.Size = new System.Drawing.Size(569, 300);
        this.Graph1.TabIndex = 7;
        this.Graph1.Text = "Graph1";

因此,基本上我必須停止生成代碼以在設計器中添加圖表區域。 我想我可以在某處使用DesignerSerialize屬性來獲取它,但我確實可以使用一些幫助。 謝謝!

在圖形設計器中,打開Chart上的ChartAreas集合屬性,然后刪除當前存在的任何內容。

ETA:您也可以在調用InitializeComponent方法的構造函數中以編程方式執行此操作:

public MyChartClass()
{
    InitializeComponent();
    ChartAreas.Clear();
    ChartAreas.Add("ChartArea");
}

好吧,我大概解決了它。 我沒有擴展Chart控件,而是擴展了UserControl類,並在其中添加了一個圖表對象。 現在,我可以以更少的麻煩輕松控制它。 謝謝大家的幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM