Joe Chin
Joe Chin

Reputation: 452

Windows Forms, getting a property from parent form

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
}

Upvotes: 7

Views: 5014

Answers (2)

Timothy Khouri
Timothy Khouri

Reputation: 31885

1) "MdiParent" is the wrong property to use.

2) Call MyForm.ShowDialog(this);

3) Use "this.Owner" in the modal form.

Upvotes: 4

Fran&#231;ois
Fran&#231;ois

Reputation: 945

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.

Upvotes: 4

Related Questions