I have the following scenario I have a main form as MDI parent and MDI child form, the child form shows a new form (I called it mydialog). I want to access specific function in the MDI child form from mydialog so I tried to set the Owner property in the mydialog object but exception occured (circular reference exception) also the Parent and ParentForm properities of mydialog form are null, I made a quick and dirty soluion using the Tag property of mydialog. I am looking for a better solution and why there is an exception when I set the Owner property of mydialog
Can you show us the code you were using to show the dialog, One of the ShowDialog methods takes a parent object as a parameter which may do what you are looking for.
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 instantiating your dialog
MyDialog frm = new MyDialog(); frm.Data = "your data here"; frm.ShowDialog();
HTH,
so I tried to set the Owner property in the mydialog object but exception occured (circular reference exception)
There should be no exception when setting the Owner property. Can you paste the exception ? Also could you paste your code that sets this value ? Have you set other properties like MDIParent / etc ?
EDIT: Code Update
Try the following, it should work
groupsettingsform mydialog= new groupsettingsform(); //create dialog
mydialog.Owner= this; //set owner MDI child form
mydialog.ShowDialog(); // <== DO NOT PASS THE OWNER
Setting the owner should not throw an exception... Try this in you MyDialog form
((YourMDIChildForm)Owner).YourMDIChildFormMethod();
My simple answer till now is to use the Tag property , but I think there is better way
groupsettingsform mydialog = new groupsettingsform(); mydialog.Tag= this; mydialog.ShowDialog(this);
I prefer to pass in any custom data or parameters to the Form by its constructor. It is the least "WinForms"-specific way and in fact is no different to constructing mostly any other object really.
In other cases I like to set properties on the Form. This seems to be a WinForms convention as well.
It is better than using Tag or "pulling" data from the Owner. As both of these require explicit casting and therefore aren't particularly elegant.