How to create a Windows forms custom control

This forum is meant for questions and discussions about the X# language and tools
User avatar
Kees Bouw
Posts: 172
Joined: Wed Nov 06, 2019 11:35 am
Location: Netherlands

How to create a Windows forms custom control

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

Image
User avatar
Fabrice
Posts: 526
Joined: Thu Oct 08, 2015 7:47 am
Location: France

Re: How to create a Windows forms custom control

Post 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
XSharp Development Team
fabrice(at)xsharp.eu
Jamal
Posts: 360
Joined: Mon Jul 03, 2017 7:02 pm

Re: How to create a Windows forms custom control

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

XSharpTempWinformsApp.zip
(2.86 MiB) Downloaded 1325 times
CustomClassLibrary.zip
(1.21 MiB) Downloaded 1320 times
Jamal
Last edited by Jamal on Thu Apr 03, 2025 3:26 pm, edited 3 times in total.
User avatar
Kees Bouw
Posts: 172
Joined: Wed Nov 06, 2019 11:35 am
Location: Netherlands

Re: How to create a Windows forms custom control

Post 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
User avatar
Kees Bouw
Posts: 172
Joined: Wed Nov 06, 2019 11:35 am
Location: Netherlands

Re: How to create a Windows forms custom control

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

Image
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
Jamal
Posts: 360
Joined: Mon Jul 03, 2017 7:02 pm

Re: How to create a Windows forms custom control

Post 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
User avatar
Kees Bouw
Posts: 172
Joined: Wed Nov 06, 2019 11:35 am
Location: Netherlands

Re: How to create a Windows forms custom control

Post 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
User avatar
Kees Bouw
Posts: 172
Joined: Wed Nov 06, 2019 11:35 am
Location: Netherlands

Re: How to create a Windows forms custom control

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

Image

Image

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
User avatar
Fabrice
Posts: 526
Joined: Thu Oct 08, 2015 7:47 am
Location: France

Re: How to create a Windows forms custom control

Post 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,
XSharp Development Team
fabrice(at)xsharp.eu
User avatar
Kees Bouw
Posts: 172
Joined: Wed Nov 06, 2019 11:35 am
Location: Netherlands

Re: How to create a Windows forms custom control

Post 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,
Post Reply