Changing TextField.Text does not clear text selection. Resulting in `ArgumentOutOfRange` exception in some situations.
See original GitHub issueDescribe 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:
- Created 9 months ago
- Reactions:1
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Perfect!