Skip to content

Avoiding the TextChanged Event in Desktop Projects

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.

Step 1

Create a new Xojo Desktop project.

Step 2

Drag a TextField from the Library to the Navigator.

Step 3

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.

Step 4

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.

Step 5

Add an Event Definition named TextChanged

Step 6

Add an Event Handler for TextChanged

Add this code:

If userEntered Then
  RaiseEvent TextChanged
End If

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.