xsharp.eu • ChildWinForm and QueryClose
Page 1 of 1

ChildWinForm and QueryClose

Posted: Wed Jul 08, 2020 4:40 pm
by leighproman
I've been using ChildWinForm to host WinForms in my VO-converted application.
Is it possible to add a QueryClose event for ChildWinForm and get it to access a property of the WinForm?
(ie If the user clicks the X of the ChildWinForm header can I stop the form closing?)
In VO I coud just subclass and add a QueryClose method but can't work out how to do that in XSharp.
Thanks
Leigh

ChildWinForm and QueryClose

Posted: Wed Jul 08, 2020 6:52 pm
by robert
Leigh,
The ChildWinForm class is a normal subclass of ChildAppWindow
See: https://www.xsharp.eu/runtimehelp/html/ ... inForm.htm
So you can create your own subclass of ChildWinForm and add a QueryClose() method.
In that method you can access the Windows Forms object (The WinForm property gives you access to the form).
The source to the ChildWinForm class can be seen here:
https://github.com/X-Sharp/XSharpPublic ... inForm.prg

Robert

ChildWinForm and QueryClose

Posted: Wed Jul 08, 2020 10:13 pm
by leighproman
Hi Robert

OK thanks - I think when I tried earlier I ended up trying to subclass the Windows Form rather than ChildWinForm!

I have my QueryClose now - something like this:

Code: Select all

CLASS MyCWF INHERIT XSharp.ChildWinForm

METHOD QueryClose(oEvent) AS USUAL CLIPPER

	LOCAL lAllowClose AS LOGIC
	LOCAL oWF AS Form1

	lAllowClose := SUPER:QueryClose(oEvent)
	//Put your changes here 

	oWF:=(Form1)SELF:WinForm
	
	lAllowClose := oWF:AllowClose()

	RETURN lAllowClose

END CLASS
Only remaining question is can I make it more generic?
i.e. if all the forms I'm testing have the AllowClose() method can I call it without casting SELF:WinForm to the specific form?

Leigh

ChildWinForm and QueryClose

Posted: Thu Jul 09, 2020 12:21 am
by Chris
Hi Leigh,

If all your forms have this method, then you can create a base form class, inheriting from Form (and having all the other forms inheriting from this base form) and declare the AllowClose() method also in this class, so you can cast the object to that base form.

Or, if you want to do it in a more "neat" way, you can use an interface instead of a base class: Declare one as:

INTERFACE IAllowsClose
METHOD AllowClose() AS LOGIC STRICT
END INTERFACE

and then simply have all your forms implement this interface with using an "IMPLEMENTS IAllowsClose" clause in your class declarations. This way, you can simply cast the object to this IAllowsClose interface.

ChildWinForm and QueryClose

Posted: Thu Jul 09, 2020 2:49 pm
by leighproman
Hi Chris
I went for the "neat" option and I have it all working now.
Thanks again for your help.
Leigh

ChildWinForm and QueryClose

Posted: Thu Jul 09, 2020 3:04 pm
by Chris
Hi Leigh,

Thanks, that would be my favorite way as well, as it also makes sure that you do not forget to implement the method in one of your new form classes, otherwise the compiler will complain. One of those nice side effects on moving to a more modern language like X#