WoW:HOWTO: Scroll EditBoxes to the left programatically
OR: "How to move the editbox cursor!"
Problem
- If an editbox has been set to contain a long string, it will scroll to the right to display the end of the string.
- If later you set the editbox to contain a short string, it will still be partially scrolled to the right, meaning the short string won't show fully even though it's really short enough.
Solution
The easiest way is to do the following when the EditBox (or its parent) is loaded:
EditBox:SetCursorPosition(0); EditBox:ClearFocus();
This will (imperceptibly) set the cursor at the leftmost point of the editbox, forcing it to scroll, and then clear focus, leaving the EditBox scrolled.
A Messier Solution
The EditBox:Insert() function scrolls the editbox to show the place where the insert took place. But ONLY after an OnUpdate event has occured following the latest :SetText() call.
local function UglyScrollLeft() this:HighlightText(0,1); this:Insert(" "..strsub(this:GetText(),1,1)); this:HighlightText(0,1); this:Insert(""); this:SetScript("OnUpdate", nil); end SomeEditBox:SetText("short string"); SomeEditBox:SetScript("OnUpdate", UglyScrollLeft);
The above code will cause UglyScrollLeft() to trigger exactly once after setting the new text, which in turn will select the first letter in the EditBox, and replace it with itself. This causes the EditBox to scroll to the far left. The additional highlight/insert is just to force the cursor to go flush left rather than sit at position 2, which looks annoying. :-)
It has to be done after an OnUpdate has passed. Attempting to do the Insert() immediately after SetText() will have no effect.