Select Text in Formatted Text field

Hi all,

I want to be able to select the text of a formatted text field in a script.

I saw another post on this but did not see a solution. Highlighting text

I am trying to emulate a “default text” in the field i.e., <type here>.

I can use the text property to set the string without committing it. I want it to be selected so that the user can just start typing and it will be over written.

Any ideas?

Thanks, Steven

What are you trying to do with it? I haven’t tried to select a specific text box when a window opens. If it is the only thing on the screen your opening, would system.gui.inputBox() be a better fit or are there other things on the screen after they fill it in?

@bpreston,

I have default text in the text field that will be over written. If the text is not selected in the text field, the user will have to highlight the text or otherwise delete it before entering their text.

Thanks, Steven

I got tied up after my reply yesterday. Have you tried using requestFocusInWindow(). You should be able to use it to set the order your inputs will be focused on. I believe this will also select the text so the user can just start typing but I haven’t had a chance to verify it.

If you only needed one string passed, I’d use the system.gui.inputBox() function from a script to open a small dialog that the user can type into. That is only useful though if there is only one value that needs to be entered or if you need to request input from a user as part of a sequence.

@bpreston,

The Formatted text field does not come in focus with the text selected. However, the Text Field does and restrict the format of the input anyway so I may just switch to the Text Field and avoid the aggravation.

There was a reason I switched to the formatted text field but I cannot remember what it is so maybe it wont matter. LOL.

Thanks, Steven

Didn’t realize the Formatted Text Field acted different on focus. If the regular Text Field works then thats the easiest. If not, I’m not sure how to make the formatted one select the text.

You need to drill down into the text area control to get to the actual control.

#Grab the control on the window
ta = event.source.parent.getComponent('Text Area')
#Set a range for selected text
ta.getComponent(0).getComponent(0).select(5,15)
#or select all of the text in the control
#ta.getComponent(0).getComponent(0).selectAll()
#request focus on the control so the selected text is highlighted.
ta.getComponent(0).getComponent(0).requestFocus()

@MMaynard,

Thanks! I will try this.

Steven

I added this to my actionPerformed event on a button.

event.source.parent.getComponent('new topic Formatted Text Field').visible = 1

event.source.parent.getComponent('new topic Formatted Text Field').requestFocusInWindow()

tf = event.source.parent.getComponent('new topic Formatted Text Field')
tf.getComponent(0).getComponent(0).select(5,15)
##request focus on the control so the selected text is highlighted.
tf.getComponent(0).getComponent(0).requestFocus()

I get this error message in a popup.

Traceback (most recent call last):
File "event:actionPerformed", line 12, in
java.lang.ArrayIndexOutOfBoundsException: java.lang.ArrayIndexOutOfBoundsException: No such child: 0

caused by ArrayIndexOutOfBoundsException: No such child: 0

Ignition v8.0.16 (b2020082513)
Java: Azul Systems, Inc. 11.0.7

What am I missing?

Thanks, Steven

I should have also mentioned that I would like to put that code in the formatted text components onFocus event.

I assume the only thing that will change is the component definition.

Thanks, Steven

My example was for a Text AREA control.
For a formatted text field you don’t have to drill down into it.

tf = event.source.parent.getComponent('new topic Formatted Text Field')
tf.select(5,15)
##request focus on the control so the selected text is highlighted.
tf.requestFocus()
#Or to Select all the text just use the selectAll()
#tf.selectAll()
#tf.requestFocus()
1 Like

I setup a new formatted text field and it works. However, it does not work on mine and there are other questions.

I will start another thread to address this.

Thanks for the help!!

Steven