HOWTO: Scroll EditBoxes to the left programatically

104,670pages on
this wiki
Add New Page
Talk0 Share

OR: "How to move the editbox cursor!"

Problem Edit

  • 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 Edit

The easiest way is to do the following when the EditBox (or its parent) is loaded:


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 Edit

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:Insert(" "..strsub(this:GetText(),1,1));
  this:SetScript("OnUpdate", nil);
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.

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki