tirsdag den 16. marts 2010

Having a caller form close a called form

Today I was asked an interesting question by a colleague.

If a form is opened from an other form, can we make Axapta (3.0) close the called form automatically when closing the caller form.

I remembered some old code for making a form truly modal in Axapta, and thought that maybe we could use some of that.

Now the solution presented here, is only good for one called form. but it should be quite easy to implement, that all opened forms open from a parent form, are closed when the parent form is closed, by changing the hwndchild reference to a list or a set of HWND-references, looping through that set or list, checking if each HWND-reference refers to an active window, and then and destroying each window.

A small example code project can be downloaded
here

The project contains two small forms and a display menu item.

The parent form test, has a buttongroup where the menuitem (referring to the child form testchild) is included.


When you open form test, and click the button, testchild opens.

Now when you give focus to the test (parent) form, and close it, the child form will automatically close.


As always - USE AT OWN RISK.