Windows窗体,从父窗体获取属性 [英] Windows Forms, getting a property from parent form

查看:163
本文介绍了Windows窗体,从父窗体获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题。我有一个父表单中的数据表。我打开一个获取datatable属性的对话框窗体并创建一个复选框列表。这将用于导出这些列。但是当我运行该应用程序时,parentform属性为null。我已经尝试在父窗体和对话框窗体中设置它(我认为如果调用ShowDialog(),这会自动完成)。



有人可以看一看看看我要去哪里错了?
从对话框:

  frmParent MyParentForm =(frmParent)this.ParentForm; 
for(int i = 0; i< MyParentForm.DataGridTable.Count; i ++)
{
chkListExportItems.Add(MyParentForm.DataGrid.Columns [i] .Name,true);



$ b $ p
$ b

从父表单:

  frmExports MyForm = new frmExports(); 
MyForm.MdiParent = this;
if(MyForm.ShowDialog == DialogResult.OK)
{
MyForm.SelectedItems //做某事
}


解决方案

将DataGridTable引用给对话框窗体。你可以在构造函数中传递它。
您应该避免使用Parent / ParentForm并避免投射。


I'm having a bit of a problem. I have a datatable in the parent form. I open a dialogbox form that gets the datatable property and creates a checkboxlist. This will be used to export those columns. But when I run the application the parentform property is null. I've tried setting it in the parent and dialogbox form (I assumed this would have been done automagically if ShowDialog() was called).

Can someone take a look and see where I'm going wrong? From the dialogbox:

frmParent MyParentForm = (frmParent)this.ParentForm;
for (int i=0; i<MyParentForm.DataGridTable.Count; i++)
{
   chkListExportItems.Add(MyParentForm.DataGrid.Columns[i].Name,true);
}

From the parent form:

frmExports MyForm = new frmExports();
MyForm.MdiParent = this;
if (MyForm.ShowDialog == DialogResult.OK)
{
   MyForm.SelectedItems // Do something
}

解决方案

Give a reference to the DataGridTable to your dialogbox form. You may pass it in the constructor. You should avoid using Parent/ParentForm and avoid casting.

这篇关于Windows窗体,从父窗体获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆