Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 896
Joe Chin Avatar asked Oct 31 '08 10:10

Joe Chin


People also ask

How pass data from parent form to child form in C#?

To send data from Parent to Child form you need to create a parameterized constructor. To send data from Child to Parent form you need to create a public method in Child class and use the method in Parent class to get the return value.

Which property on Windows Forms Gets or sets the size and location of the form on the windows desktop?

If you want to set the size and location of a form, you can use the DesktopBounds property to size and locate the form based on desktop coordinates or use the Bounds property of the Control class to set the size and location of the form based on screen coordinates.


2 Answers

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.

like image 183
François Avatar answered Oct 18 '22 02:10

François


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

2) Call MyForm.ShowDialog(this);

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

like image 38
Timothy Khouri Avatar answered Oct 18 '22 03:10

Timothy Khouri