Passing data between forms
I have the following scenario I have a main form as an MDI parent parent and an MDI child form, the child form shows a new form (I called it mydialog). I want to access a specific function in an MDI child form from mydialog, so I tried to set the Owner property on mydialog object, but an exception occurred (circular reference exception) and the parent and parent form properties of mydialog form are null, I am a quick and dirty solution. using the Tag property mydialog. I am looking for a better solution and why an exception is thrown when I set the Owner property of mydialog
source to share
so i tried to set the Owner property on mydialog object but an exception was thrown (circular reference exception)
There should be no exceptions when setting the Owner property. Can you insert an exception? Also can you paste your code that sets this value? Have you set other properties like MDIParent / etc?
EDIT: code update
Try this, it should work
groupsettingsform mydialog= new groupsettingsform(); //create dialog
mydialog.Owner= this; //set owner MDI child form
mydialog.ShowDialog(); // <== DO NOT PASS THE OWNER
source to share
you can create a parameterized constructor in MyDialog as
public MyDialog(object param1)
pass your data as
MyDialog frm = new MyDialog("data");
frm.ShowDialog();
or create a public property in MyDialog as
public object Data {get; set;}
and set this when creating your dialog
MyDialog frm = new MyDialog (); frm.Data = "your data is here"; frm.ShowDialog ();
NTN,
source to share
I prefer to pass any custom data or parameters to the form with my constructor. This is the least "WinForms" special way, and really is no different from the fact that it actually creates any other object.
Other times I like to set properties on the form. This is similar to the WinForms convention.
This is better than using tags or pulling data from the Owner. As both of these require explicit casting and are therefore not particularly elegant.
source to share