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

0


source to share


6 answers


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

      

+1


source


You can show us the code you used to display the dialog. One of the ShowDialog methods takes a parent object as a parameter, which can do what you are looking for.



0


source


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,

0


source


Owner setting shouldn't throw an exception ... Try this in MyDialog form

((YourMDIChildForm)Owner).YourMDIChildFormMethod();

      

0


source


My simple answer so far is to use the Tag property, but I think there is a better way

groupsettingsform mydialog = new groupsettingsform (); mydialog.Tag = this; mydialog.ShowDialog (this);

0


source


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.

0


source







All Articles