我在 form1 上有一個圖表,我正在嘗試從 form2 獲取該圖表。例如獲取系列的名稱。它給了我這個“物件參考未設定到物件的實體。” Chart 的修飾符是 Public(我也嘗試內部)。我不明白為什么。有任何想法嗎
TrackerV2 tv2 = ActiveForm as TrackerV2; //reaching main form with this
MessageBox.Show(tv2.chartMonthlyReport.Series[0].Name.ToString());
uj5u.com熱心網友回復:
下面是如何使用事件向 發回信號的最小示例form1
。
void Main()
{
var form1 = new Form1();
form1.Show();
}
public class Form1 : Form
{
private Button Button1;
private Chart chartMonthlyReport;
public Form1()
{
this.Button1 = new Button() { Text = "Open Form 2", Width = 256, };
this.Button1.Click = (s, e) =>
{
var form2 = new Form2();
form2.DisplaySeriesName = (s2, e2) =>
MessageBox.Show(chartMonthlyReport.Series[0].Name);
form2.ShowDialog();
};
this.chartMonthlyReport = new Chart();
this.chartMonthlyReport.Series.Add(new Series() { Name = "Monthly" });
this.Controls.Add(this.Button1);
}
}
public class Form2 : Form
{
private Button Button1;
public event EventHandler DisplaySeriesName;
public Form2()
{
this.Button1 = new Button() { Text = "Display Series Name", Width = 256, };
this.Button1.Click = (s, e) =>
this.DisplaySeriesName?.Invoke(this, new EventArgs());
this.Controls.Add(this.Button1);
}
}
當我運行它時,我得到這個:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/537943.html
標籤:C#窗体
上一篇:從前面帶有分隔符的文本中洗掉單詞(使用正則運算式或串列)
下一篇:DateTime到一天結束C#