question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Changing TextField.Text does not clear text selection. Resulting in `ArgumentOutOfRange` exception in some situations.

See original GitHub issue

Describe the bug

This occurs when you select a range in a TextField then change the Text to a new shorter string value and attempt to update the CursorPosition:

To Reproduce

[InlineData("a")] // Fails
[InlineData ("aaaaaaaaaaa")] // Passes
[Theory]
public void TestSetTextAndMoveCursorToEnd_WhenExistingSelection(string newText)
{
	var tf = new TextField ();
	tf.Text = "fish";
	tf.CursorPosition = tf.Text.Length;

	tf.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers()));

	tf.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
	tf.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));

	Assert.Equal (1, tf.CursorPosition);
	Assert.Equal (2, tf.SelectedLength);

	tf.Text = newText;
	tf.CursorPosition = tf.Text.Length;
}

Test fails with:

   Source: TextFieldTests.cs line 1303
   Duration: 25 ms

  Message: 
System.ArgumentOutOfRangeException : Index and length must refer to a location within the string. (Parameter 'length')

  Stack Trace: 
String.ThrowSubstringArgumentOutOfRange(Int32 startIndex, Int32 length)
String.Substring(Int32 startIndex, Int32 length)
TextField.PrepareSelection(Int32 x, Int32 direction) line 1139
TextField.set_CursorPosition(Int32 value) line 342
TextFieldTests.TestSetTextAndMoveCursorToEnd_WhenExistingSelection(String newText) line 1318
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

Workaround is to call ClearAllSelection (); before updating CursorPosition.

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
BDispcommented, Jan 5, 2023

I think is better change the issue title to something like the or whatever you think better:

TextField.Text setter doesn’t clearing text selection if not empty.

1reaction
tznindcommented, Jan 5, 2023

Perfect!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Text Selection Not Working in Basic TextField when using ...
in my case it works: (2,2) place pointer after second character and (0, name.length) highlights whole text and it gets deleted by backspace....
Read more >
InputField ArgumentOutOfRangeException
It's occurs when set Text inside Vertically Overflow. Sometimes the error shown up but even if error not show, the text inside doesn't...
Read more >
Bug with Ellipsis overflow when using a font fallback ...
The text has wrapping disabled and overflow set to Ellipsis (although you can't ... ArgumentOutOfRangeException: Selection is out of range.
Read more >
C# Exception Guide: ArgumentOutOfRangeException
The ArgumentOutOfRangeException exception is thrown when the argument passed to a method is not null and contains a value that is not within ......
Read more >
Clear text if selection is changed - Power Platform Community
Solved: Hello, I'm building a roster app and I have a Skills dropdown that based on the selections, a box in which people...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found