Reputation: 2303
Having trouble figuring this one out..
I currently have a frmMain and a frmLoading..
inside frmMain Shown event, I'm doing
frmLoading load = new frmLoading(); load.ShowDialog();
inside frmLoading.. I want to access frmMain.. I know I could pass the previous form inside constructor but I'd like to avoid that if possible..
Was thinking it would be this.Owner, or this.Parent.. nope, searched through all the properties in "this", "Form".. could not find it..
Any idea?
Upvotes: 4
Views: 3518
Reputation: 873
frmLoading load = new frmLoading();
load.ShowDialog(this);
after
Button yourbutton= ((frmLoading)this.Owner).yourbutton;
yourbutton.Text= "Okay";
Upvotes: 0
Reputation: 941218
Avoid making Winforms guess who should be the owner, make it explicit:
frmLoading load = new frmLoading();
load.ShowDialog(this);
Now you can reliably use the Owner property to find the owner as soon as the Load event runs. If you need it in the constructor then you are going to have to pass it as a constructor argument, not an issue of course and the preferred solution since it doesn't rely on the Show overload you use. Consider using events to avoid the coupling.
Upvotes: 8
Reputation: 29673
frmLoading doesn't know about frmMain. But you can use events if you want to change sth on form main from second form. You need to pass frmMain handle to frmLoading.
Look here: http://forum.codecall.net/c-programming/515-c-calling-parent-functions-child-form.html
Upvotes: 0