Is there an example somewhere of how to customise/subclass a control type for Windows Forms?
For VO forms I have a custom TextBox class which I set the background colour on focus/lose focus.
I want to do the same thing in Windows Forms without having an event for every control but can't work it out.
Thanks
Leigh
subclass / customise WinForms controls
-
- Posts: 60
- Joined: Tue Oct 11, 2016 8:56 pm
- Location: UK
subclass / customise WinForms controls
Hi Leigh,
You just need to declare a custom class that inherits from TextBox and add all your custom code there. Better add this class in a base library and then, after you build it, that "new" control should appear in the Toolbox.
In order to convert previous TextBox controls to the new one, you need to go to the designer generated code and change the class names, from the old one to the new one. That needs to be done in 2 places, in the class declaration of the form where the control iVars are declared, and inside the constructor, in the beginning of it, where they are being instantiated.
You just need to declare a custom class that inherits from TextBox and add all your custom code there. Better add this class in a base library and then, after you build it, that "new" control should appear in the Toolbox.
In order to convert previous TextBox controls to the new one, you need to go to the designer generated code and change the class names, from the old one to the new one. That needs to be done in 2 places, in the class declaration of the form where the control iVars are declared, and inside the constructor, in the beginning of it, where they are being instantiated.
Chris Pyrgas
XSharp Development Team
chris(at)xsharp.eu
XSharp Development Team
chris(at)xsharp.eu
-
- Posts: 60
- Joined: Tue Oct 11, 2016 8:56 pm
- Location: UK
subclass / customise WinForms controls
OK so I've created new library with this class:
I've added the DLL as a reference in my project and changed the class name in the 'form.designer' code and it all builds ok.
Unfortunately the modified control doesn't change colour as hoped!
Anything obvious that I'm missing?
Thanks
Leigh
Code: Select all
PUBLIC CLASS LeighTextBox INHERIT System.Windows.Forms.TextBox
CONSTRUCTOR() STRICT
RETURN
PRIVATE METHOD Enter(sender AS OBJECT, e AS System.EventArgs) AS VOID STRICT
SELF:BackColor := System.Drawing.Color.Yellow
RETURN
PRIVATE METHOD Leave(sender AS OBJECT, e AS System.EventArgs) AS VOID STRICT
SELF:BackColor := System.Drawing.Color.White
RETURN
END CLASS
Unfortunately the modified control doesn't change colour as hoped!
Anything obvious that I'm missing?
Thanks
Leigh
subclass / customise WinForms controls
Hi Leigh,
If you do not want to use events (like Enter += myhandler), you need to override the On* methods. In this case:
PROTECTED OVERRIDE METHOD OnEnter(e AS EventArgs) AS VOID
SUPER:OnEnter(e)
// .. add your code here
RETURN
(note that the "OVERRIDE" keyword is not strictly necessary, if you have the /vo (all methods VIRTUAL) compiler option enabled.
You can view the correct parameters for the On() methods in the .net docs (they are the same as their respective event handler methods, but without the first OBJECT param):
https://docs.microsoft.com/en-us/dotnet ... work-4.7.1
If you do not want to use events (like Enter += myhandler), you need to override the On* methods. In this case:
PROTECTED OVERRIDE METHOD OnEnter(e AS EventArgs) AS VOID
SUPER:OnEnter(e)
// .. add your code here
RETURN
(note that the "OVERRIDE" keyword is not strictly necessary, if you have the /vo (all methods VIRTUAL) compiler option enabled.
You can view the correct parameters for the On() methods in the .net docs (they are the same as their respective event handler methods, but without the first OBJECT param):
https://docs.microsoft.com/en-us/dotnet ... work-4.7.1
Chris Pyrgas
XSharp Development Team
chris(at)xsharp.eu
XSharp Development Team
chris(at)xsharp.eu
-
- Posts: 60
- Joined: Tue Oct 11, 2016 8:56 pm
- Location: UK
subclass / customise WinForms controls
Thanks Chris - thats exactly what I was looking for - all working great now.
Leigh
Leigh
subclass / customise WinForms controls
pmfji, followed and learned, thx.Chris wrote:If you do not want to use events (like Enter += myhandler), you need to override the On* methods. In this case:
...
Is it correct, that the
Code: Select all
...
SELF:oTextBox1:Enter += SELF:TextBox1Enter
...
METHOD TextBox1Enter(sender AS System.Object , e AS System.EventArgs) AS VOID
SELF:oTextBox1:BackColor :=System.Drawing.Color.Yellow
RETURN
,
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
subclass / customise WinForms controls
You're welcome Leigh!
Karl, yes, typically you would always use the event because it is an easy way to do this without subclassing, but when you use a subclass, you can either use the On? methods, or can still use an event, handled at the subclass level.
Btw, the same behavior can be achieved also without subclassing, you could write some code that gets executed after the form is generated and manually bind to the events. Something like this:
Karl, yes, typically you would always use the event because it is an easy way to do this without subclassing, but when you use a subclass, you can either use the On? methods, or can still use an event, handled at the subclass level.
Btw, the same behavior can be achieved also without subclassing, you could write some code that gets executed after the form is generated and manually bind to the events. Something like this:
Code: Select all
METHOD BindEvents(oForm AS Form) AS VOID
FOREACH oControl AS Control IN oForm:Controls
IF oControl IS TextBox
oControl:Enter += myEnterHandler
END IF
NEXT
Chris Pyrgas
XSharp Development Team
chris(at)xsharp.eu
XSharp Development Team
chris(at)xsharp.eu
subclass / customise WinForms controls
Argh, that's too much for my rusty brain...Chris wrote:Karl, yes, typically you would always use the event because it is an easy way to do this without subclassing, but when you use a subclass, you can either use the On? methods, or can still use an event, handled at the subclass level.
Btw, the same behavior can be achieved also without subclassing, you could write some code that gets executed after the form is generated and manually bind to the events. Something like this:
Code: Select all
METHOD BindEvents(oForm AS Form) AS VOID FOREACH oControl AS Control IN oForm:Controls IF oControl IS TextBox oControl:Enter += myEnterHandler END IF NEXT
Your BindEvents belongs to which class? The shell? myEnterHandler is a method of ...?
BTW, don't unterstand why folks shy of subclassing. Only to think of the pain folks experienced, fiddling with generated code in "painted" forms, VS denying to open the form etc...
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
subclass / customise WinForms controls
Hi Karl,
Most suitable it is to define it in the same class with the form. Not using subclassing is just simpler...but if there's no other convenient way to do what you want, subclassing is also fine.
Most suitable it is to define it in the same class with the form. Not using subclassing is just simpler...but if there's no other convenient way to do what you want, subclassing is also fine.
Chris Pyrgas
XSharp Development Team
chris(at)xsharp.eu
XSharp Development Team
chris(at)xsharp.eu