how to refer to the form that caused: form2.show  
Author Message
R.Tutus





PostPosted: Visual C# General, how to refer to the form that caused: form2.show Top

Hi if I have a form1 where i call : form2.show
In the code of form2, how do I refer to form1
I tried me.parent and me.owner in form2 but
they didn t work
Thanks for your help.



Visual C#20  
 
 
Naga Satish Rupenaguntla





PostPosted: Visual C# General, how to refer to the form that caused: form2.show Top

in form2, you can try using form1.show

 
 
R.Tutus





PostPosted: Visual C# General, how to refer to the form that caused: form2.show Top

How do I get a reference to form1.

Neither using parent nor owner in form2 has worked for referencing the form that instantiated the current form with:  form2.show

THnaks. 



 
 
Keith Rome





PostPosted: Visual C# General, how to refer to the form that caused: form2.show Top

There is no built-in knowledge of what form caused another form to show... after all, pretty much any code could have shown the Form2, not just something within Form1.

If you really need this association, then the easiest thing would be to add a public property to Form2 (call it "ShownByForm" for example). Then set this property just before showing the form...

public partial class Form1 : Form

{

/*...*/

public void ShowForm2()

{

Form2 FormToShow = new Form2();

FormToShow.ShownByForm = this;

FormToShow.Show();

}

}

public partial class Form2 : Form

{

/*...*/

private Form _shownByForm = null;

public Form ShownByForm

{

get { return _shownByForm; }

set { _shownByForm = value; }

}

}

HTH



 
 
R.Tutus





PostPosted: Visual C# General, how to refer to the form that caused: form2.show Top

what if we use showdialog. is there a built in way

Thanks for the solution for show method.



 
 
Keith Rome





PostPosted: Visual C# General, how to refer to the form that caused: form2.show Top

Same situation for ShowDialog(). ShowDialog is not much more than a Show() that temporarily takes over the main thread of the application until the modal dialog is closed.