【发布时间】:2015-03-27 14:47:08
【问题描述】:
我来问是因为我是使用 Windows 窗体的新手,我需要一些帮助。
我有一个包含 2 个表单的 Windows 窗体项目。 MainForm 和 InnerForm。
我正在尝试从 InnerForm 访问 MainForm 中的 TableLayoutPanel,以在此 tablelayoutpanel 中添加新行,并在 InnerForm 中发生一些操作。
我有下一个代码:
主窗体:
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
TableLayoutPanel panel = tableLayoutPanel1;
panel.ColumnCount = 4;
panel.RowCount = 1;
panel.RowStyles.Add(new RowStyle(SizeType.AutoSize, 0));
panel.Controls.Add(new Label() { Text = "Tag/ID" }, 0, 0);
panel.Controls.Add(new Label() { Text = "Tipo" }, 1, 0);
panel.Controls.Add(new Label() { Text = "Acción" }, 2, 0);
panel.Controls.Add(new Label() { Text = "Ejecutar" }, 3, 0);
}
private void AddInnerForm(string url)
{
var inner = new InnerForm(url)
// more code
}
public void agregarRow(ConsoleMessageEvents args){
// some action with tableLayoutPanel1
}
}
内窗体:
public partial class InnerForm : UserControl
{
MainForm theMain;
public InnerForm(MainForm main)
{
theMain = main;
}
public InnerForm(string url)
{
InitializeComponent();
// more code
}
private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
{
theMain.agregaRow(args);
}
}
但是当我调试程序时,我得到了这个错误:
An unhandled exception of type 'System.NullReferenceException' occurred in Project.exe
Additional information: Object reference not set to an instance of an object.
InnerForm 的这一行:
private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
{
**theMain.agregaRow(args)**;
}
这里有什么问题?
【问题讨论】: