# Select Text in Formatted Text field

**URL:** <https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776>\
**Category:** Ignition\
**Created:** [October 17, 2020, 9:58pm UTC](https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776 "2020-10-17T21:58:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![steven.cox](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steven.cox/32/18589_2.png) [@steven.cox](https://forum.inductiveautomation.com/u/steven.cox)\
**Post date:** [October 17, 2020, 9:58pm UTC](https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776/1 "2020-10-17T21:58:27Z")

</div>

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](http://forum.inductiveautomation.com/t/highlighting-text/2165)

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

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [October 19, 2020, 2:58pm UTC](https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776/2 "2020-10-19T14:58:13Z")

</div>

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()](https://docs.inductiveautomation.com/display/DOC80/system.gui.inputBox) be a better fit or are there other things on the screen after they fill it in?

---

<div class="post-metadata">

**Author:** ![steven.cox](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steven.cox/32/18589_2.png) [@steven.cox](https://forum.inductiveautomation.com/u/steven.cox)\
**Post date:** [October 19, 2020, 3:32pm UTC](https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776/3 "2020-10-19T15:32:01Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [October 20, 2020, 3:04pm UTC](https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776/4 "2020-10-20T15:04:03Z")

</div>

I got tied up after my reply yesterday. Have you tried using [requestFocusInWindow()](https://docs.inductiveautomation.com/display/DOC80/Focus+Manipulation). 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.

---

<div class="post-metadata">

**Author:** ![steven.cox](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steven.cox/32/18589_2.png) [@steven.cox](https://forum.inductiveautomation.com/u/steven.cox)\
**Post date:** [October 20, 2020, 10:11pm UTC](https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776/5 "2020-10-20T22:11:25Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [October 21, 2020, 1:19pm UTC](https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776/6 "2020-10-21T13:19:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [October 21, 2020, 5:25pm UTC](https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776/7 "2020-10-21T17:25:18Z")

</div>

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

```auto
#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()

```

---

<div class="post-metadata">

**Author:** ![steven.cox](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steven.cox/32/18589_2.png) [@steven.cox](https://forum.inductiveautomation.com/u/steven.cox)\
**Post date:** [October 21, 2020, 6:53pm UTC](https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776/8 "2020-10-21T18:53:59Z")

</div>

@MMaynard,

Thanks! I will try this.

Steven

---

<div class="post-metadata">

**Author:** ![steven.cox](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steven.cox/32/18589_2.png) [@steven.cox](https://forum.inductiveautomation.com/u/steven.cox)\
**Post date:** [October 22, 2020, 5:39pm UTC](https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776/9 "2020-10-22T17:39:12Z")

</div>

> [@MMaynard](#):
>
> You need to drill down into the text area control to get to the actual control.
> 
> ```auto
> #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()
> 
> ```

I added this to my actionPerformed event on a button.

```auto
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

```auto

```

What am I missing?

Thanks, Steven

---

<div class="post-metadata">

**Author:** ![steven.cox](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steven.cox/32/18589_2.png) [@steven.cox](https://forum.inductiveautomation.com/u/steven.cox)\
**Post date:** [October 22, 2020, 5:44pm UTC](https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776/10 "2020-10-22T17:44:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [October 22, 2020, 5:55pm UTC](https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776/11 "2020-10-22T17:55:45Z")

</div>

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

```auto
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()

```

---

<div class="post-metadata">

**Author:** ![steven.cox](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/steven.cox/32/18589_2.png) [@steven.cox](https://forum.inductiveautomation.com/u/steven.cox)\
**Post date:** [October 22, 2020, 8:16pm UTC](https://forum.inductiveautomation.com/t/select-text-in-formatted-text-field/39776/12 "2020-10-22T20:16:34Z")

</div>

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
