tags:

views:

1092

answers:

6

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

A: 

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.

Kaius
A: 

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,

Leon Tayson
it is good but I think it is not the best as if I want to access alot of MDI child form data I have to copy it into mydialog form
Ahmed Said
+1  A: 

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
Preets
this is my simple code (exist in the MDI child form)groupsettingsform mydialog= new groupsettingsform();//create dialogmydialog.Owner= this; //set owner MDI child formmydialog.ShowDialog(this);if I comment the line of Owner=this,the owner of mydialog will be the MDI parent form
Ahmed Said
thank you but what actually happens, why when I removed the passing parameter the owner will be MDI child, and if I passed it (without setting the owner explicitly) the owner will be MDI parent??
Ahmed Said
Yea. Kinda strange. I am trying to solve the mystery ;-)
Preets
A: 

Setting the owner should not throw an exception... Try this in you MyDialog form

((YourMDIChildForm)Owner).YourMDIChildFormMethod();
Leon Tayson
A: 

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);

Ahmed Said
A: 

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.

NathanE