Thursday, March 28, 2019

Fixing RichTextBox's ContentsResized boundaries with NBSPs

Previous I noticed that RichTextBox does not count trailing spaces as part of the contents when determining the needed size for the ContentsResized event. It does, however, count non-breaking spaces (NBSPs), which look identical to normal spaces. When the text in the text box is changed, I check whether the text contains a space, and if so, replace them all with NBSPs (character U+00A0). The selection/position needs to be saved before altering the text and restored afterward, otherwise the cursor will jump to the beginning. When using the text for other purposes, the NBSPs are first replaced with normal spaces.

No comments:

Post a Comment