Windows窗体,从父窗体获取属性 [英] Windows Forms, getting a property from parent form
问题描述
有人可以看一看看看我要去哪里错了?
从对话框:
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屋!