ChildWinForm and QueryClose

This forum is meant for questions and discussions about the X# language and tools
Post Reply
leighproman
Posts: 60
Joined: Tue Oct 11, 2016 8:56 pm
Location: UK

ChildWinForm and QueryClose

Post 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
User avatar
robert
Posts: 4567
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

ChildWinForm and QueryClose

Post 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
XSharp Development Team
The Netherlands
robert@xsharp.eu
leighproman
Posts: 60
Joined: Tue Oct 11, 2016 8:56 pm
Location: UK

ChildWinForm and QueryClose

Post 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
User avatar
Chris
Posts: 4987
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

ChildWinForm and QueryClose

Post 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.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
leighproman
Posts: 60
Joined: Tue Oct 11, 2016 8:56 pm
Location: UK

ChildWinForm and QueryClose

Post by leighproman »

Hi Chris
I went for the "neat" option and I have it all working now.
Thanks again for your help.
Leigh
User avatar
Chris
Posts: 4987
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

ChildWinForm and QueryClose

Post 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#
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Post Reply