Page 1 of 2
How to create a Windows forms custom control
Posted: Wed Apr 02, 2025 12:26 pm
by Kees Bouw
Hi,
On
https://learn.microsoft.com/ro-ro/dotne ... esktop-4.8 there are instructions on how to add a Windows forms custom control. In short, you have to choose "Add New Item" and then you can select "Custom Control". My goal is to subclass the TextBox control and add a few methods. But the option to add a Custom Control appears to be missing. I can add a "User Control" but that is not what I want. What should I do to be able to create a new Custom Control?
Kees.

Re: How to create a Windows forms custom control
Posted: Wed Apr 02, 2025 2:49 pm
by Fabrice
Hi Kees,
Sorry, but I think that you are mixing words :
A WinForm UserControl is the same (by functionnality) as VO CustomControl. This is what the UserControl template is offering.
You can imagine the UserControl as a panel, where you can drop severals Controls to create a more complex control. Fo eg, a IPAddressText that is composed by 4 TextBox.
If I read you correctly, you want to inherit from the TextBox control, so simply create a .prg file with the needed code for inheritance.
CLASS MyControl INHERIT System.Windows.TextBox
// Make your changes here
END CLASS
Build your app; go to the designer : MyControl should appear in the ToolBox.
HTH,
Fab
Re: How to create a Windows forms custom control
Posted: Wed Apr 02, 2025 6:03 pm
by Jamal
Hi Kees,
In addition to what Fabrice mentioned, please see the download both attached projects, demo app and a custom control lib to see how it is done.
Jamal
Re: How to create a Windows forms custom control
Posted: Wed Apr 02, 2025 6:55 pm
by Kees Bouw
Hi Fabrice,
Thank you for your reply. From what I find on the internet, a "User Control" is very different from a "Custom Control". A user control is a combination of existing controls on a window that you can then attach to a placeholder object. A custom control is a control derived from an existing control or derived from the generic Control class which makes it possible to create a new type of control. It seems that in C# both user and custom controls can be added as new items and I wonder why in X# you can only add a user control.
Kees.
Fabrice wrote: Wed Apr 02, 2025 2:49 pm
Hi Kees,
Sorry, but I think that you are mixing words :
A WinForm UserControl is the same (by functionnality) as VO CustomControl. This is what the UserControl template is offering.
You can image the UserControl as a panel, where you can drop severals Controls to create a more complex control. Fo eg, a IPAddressText that is composed by 4 TextBox.
If I read you correctly, you want to inherit from the TextBox control, so simply create a .prg file with the needed code for inheritance.
CLASS MyControl INHERIT System.Windows.TextBox
// Make you changes here
END CLASS
Build your app; go to the designer : MyControl should appear in the ToolBox.
HTH,
Fab
Re: How to create a Windows forms custom control
Posted: Wed Apr 02, 2025 6:58 pm
by Kees Bouw
Hi Jamal,
Thank you for your reply. It seems that the solution you provided is missing something which makes it impossible to see how it is done. I downloaded the zip, unpacked it and double clicked on the solution. Not sure what I could have done wrong.
Kees.
Jamal wrote: Wed Apr 02, 2025 6:03 pm
Hi Kees,
In addition to what Fabrice mentioned, please see the attached project to see how it is done.
XSharpTempWinformsApp.zip
Jamal
Re: How to create a Windows forms custom control
Posted: Wed Apr 02, 2025 7:29 pm
by Jamal
Hi Kees,
I updated the post which has the CustomClassLibrary.zip which contains the custom control.
Please extract the project and add it to the solution.
Jamal
Re: How to create a Windows forms custom control
Posted: Thu Apr 03, 2025 7:10 am
by Kees Bouw
Hi Jamal,
Thanks for the update. I did get it working and it is a nice demonstration of a custom TextBox control.
Thank you very much!
Kees.
Jamal wrote: Wed Apr 02, 2025 7:29 pm
Hi Kees,
I updated the post which has the CustomClassLibrary.zip which contains the custom control.
Please extract the project and add it to the solution.
Jamal
Re: How to create a Windows forms custom control
Posted: Fri Apr 04, 2025 9:18 am
by Kees Bouw
Hi,
Strange things have been happening with my custom controls. I have created a class that inherits from (Winforms) TextBox that adds two access/assigns. I have put this control on a Windows Forms User Control window. Then when I edit this window later on and save it, I can't open it again because of an error "Invalid cast from 'System.Boolean' to 'XSharp._Usual'. Then what I find is that two resources have been created in the .resx file of the form, one for each access/assign. See the pictures below. I have tried to reproduce the problem in a new Winforms application, but was unsuccessful. Apparently there is something in my code that causes this behaviour but I have no idea what it is. I think the TextBox subclass code is correct, it is also below. What I would like to know is: under what circumstances are resources created from access/assigns? I know this is not supposed to happen in this case because they are not created in my test application. And when the resources are there, I can't open the form and vice versa. So I am very curious to know what triggers the creation of resources from access/assigns.
Kees.
Code: Select all
CLASS WF_SingleLineEdit INHERIT System.Windows.Forms.TextBox
PROTECT FIsRequired AS LOGIC
PROTECT FConfirmIfBlank AS LOGIC
PROTECT FBrush AS Color
EXPORT ShowBalloon AS LOGIC
CONSTRUCTOR() STRICT
SUPER()
SELF:FBrush := SELF:BackColor
SELF:ShowBalloon := TRUE
SELF:FIsRequired := FALSE
SELF:FConfirmIfBlank := FALSE
RETURN
ACCESS ConfirmIfBlank AS LOGIC
RETURN SELF:FConfirmIfBlank
ASSIGN ConfirmIfBlank(lValue AS LOGIC)
IF SELF:FConfirmIfBlank <> lValue
SELF:FConfirmIfBlank := lValue
IF SELF:FConfirmIfBlank
SELF:BackColor := Color.FromKnownColor(KnownColor.Info)
ELSE
SELF:BackColor := SELF:FBrush
ENDIF
ENDIF
RETURN
ACCESS IsRequired AS LOGIC
RETURN SELF:FIsRequired
ASSIGN IsRequired(lValue AS LOGIC)
IF SELF:FIsRequired <> lValue
SELF:FIsRequired := lValue
IF SELF:FIsRequired
SELF:BackColor := Color.FromKnownColor(KnownColor.Info)
ELSE
SELF:BackColor := SELF:FBrush
ENDIF
ENDIF
RETURN
END CLASS
Re: How to create a Windows forms custom control
Posted: Fri Apr 04, 2025 10:22 am
by Fabrice
Hi Kees,
Just a (quick) guess : Have you typed your ACCESS/ASSIGN return values ?
The Designer is living in his own world, so he might know nothing about USUALs.
Can you share your code ?
TIA,
Re: How to create a Windows forms custom control
Posted: Fri Apr 04, 2025 12:23 pm
by Kees Bouw
Hi Fabrice,
At first the return values were not typed. Then I typed them as can be seen in the code I included, but it makes no difference, I still have the same problems.
Kees.
Fabrice wrote: Fri Apr 04, 2025 10:22 am
Hi Kees,
Just a (quick) guess : Have you typed your ACCESS/ASSIGN return values ?
The Designer is living in his own world, so he might know nothing about USUALs.
Can you share your code ?
TIA,