How to access the previous form after doing showdialog?

后端 未结 3 603
Happy的楠姐
Happy的楠姐 2021-01-13 19:47

Having trouble figuring this one out..

I currently have a frmMain and a frmLoading..

inside frmMain Shown event, I\'m doing

frmLoading load = new          


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-13 20:06

    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.

提交回复
热议问题