Well, first of all, please see my comment to the question.
Replace the function
which "prints" to a string:
You can even avoid replacing the function name where it is currently used. Instead, wrap a call to
into a function and name it
; use this new function in your legacy code.
When all printing to a string is done, use this string to populate the text box value.