Xojo Mobile and Web project controls don’t raise the TextChanged Event like Desktop controls do. But I want my code to be as similar as possible across all project types so I’ve created some custom controls. In this blog post I’ll walk you through the process of creating a Custom Desktop TextField control.
Create a new Xojo Desktop project.
Drag a TextField from the Library to the Navigator.
Add a private property userEntered As Boolean = True. This will be used to determine whether the user has entered the text, or if the text has been set via the application.
Add a method Value as below:
Add the following code:
userEntered = False
Text = value
userEntered = True
As you can see the userEntered property is set to false. The text is changed which will raise the TextChanged event and the userEntered property is set back to true.
Add an Event Definition named TextChanged
Add an Event Handler for TextChanged
Add this code:
If userEntered Then
This method handles the default TextChanged event from the Super Class and if the userEntered property is true, raises the defined TextChanged event.
To use this control, you would drag it from the navigator or library onto your window or container as you would a normal TextField control. However, you can now set the text in the control using Value = “Some Text” and the TextChanged event won’t be raised. You can still set the Text property directly which will continue to raise the event.
For the sake of consistency I also have custom Web and Mobile TextFields that have the Value method. This allows the same code to be used on all project types.
You can also use this technique on other controls that automatically raise a “changed” event.
Wayne Golding has been a Xojo developer since 2005 and is a Xojo MVP. He operates the IT Company Axis Direct Ltd which primarily develops applications using Xojo that integrate with Xero www.xero.com. Wayne’s hobby is robotics where he uses Xojo to build applications for his Raspberry Pi, often implementing IoT for remote control.