开发者

How to determine whether a form has raised its Shown event

I have an MDI application that can either run in TABBED or WINDOWED mode. When a user selects something from the menu a form is shown. All menuItem clicks pretty much look the same:

Dim frm As New <SomeForm>
frm.MdiParent = MDIParentForm
frm.Show()

There is a timer that wakes up every xx seconds and checks if the application is running in TABBED or WINDOWED mode. If the application is running in TABBED mode it goes through all the MDIChildren and adds them to tabs (keeping track of them in a list). I am finding that there is a bug when it adds a form to a page in a tabcontrol - it doesn't 开发者_如何学Pythonclear the MDIParent properly and there is a reference to that form in the PropertyStore (used by .NET MDI System) and hence a big memory leak. In order to fix this issue I tried to set the MDIParent of the form to Nothing/Null before adding to a tabpage but that throws a StackOverflowException by the .NET framework. I suspect the StackOverflowException is caused because I am trying to change the MDIParent while I am still loading a form. I am finding out (through tests on 1 form) that IF I am able to catch the "Shown" event I can change the MDIParent without any problems. The problem is that there are a zillion of these menuItemClick event handlers and changing each one is not feasible so I can't subscribe to the "Shown" event early enough for each form.

Suppose I have an infinite number of these forms that are being shown from this MDI Application and changing each one of those forms is not practical so I need to do something from the MDI Application. What would be a way for the Sub() that sets MDIParent = Nothing to determine if the form has already raised its "Shown" event? (I tried checking frm.Visible = True but that throws the same exception)


Instead of removing the MDIParent parent property, make a copy of the form and delete the original:

  1. Create a new instance of the child form (without an MDI parent)
  2. Add it to the tabcontrol
  3. Duplicate everything from the MDI Child in the new child form in the tab.
  4. Delete the MDI child form.


Check out the Activated event.


re: There is a timer that wakes up every xx seconds and checks if the application is running in TABBED or WINDOWED mode.

Why don't you have the code run after they edit the mode value instead of using a timer?

re: What would be a way for the Sub() that sets MDIParent = Nothing to determine if the form has already raised its "Shown" event?

You could create a form-level property whose value is false by default and set true within the form_show event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜