I need to create RichTextBox
in Windows Forms, which will have maximum 18 characters in a row and maximum 6 rows. The problem with RichTextBox
is when you write very long word, it is displayed in two rows, but it is treated like one row. For example:
This is
supercalifragilist
icexpialidocious
This is a text with two lines.
My first approach was to create own class, which was inheriting from RichTextBox
and override OnTextChanged
. It was nicely done, but I totally forgot about word-wrap. So I implemented word-wrapping in my approach.
Algorithm is pretty simple: when text changes, split it by 'space' and 'new line' characters. Then I'm counting how many characters were entered in a row, and if there is space for word, I place it in this row. When there is no space for word, I'm creating new line.
And annoying part: now it have some bugs, which I don't know how to solve. Let's assume user has written 18-characters long word. He has a cursor after last character. And now:
- He can press Return, to manually make new line character, which always should be in this place.
- He can press space to start writing new word.
- He can continue to write other characters for this word.
In each scenario he will end up with 18-characters long word and 'new line' character. I don't know how to detect, what he wanted to do. And please keep in mind that user can place cursor everywhere and edit the text he has entered previously.
I know I can create custom string class (inheriting from string) or keep list of entered words, but implementing this will be painful and code will start to be unreadable.
Any ideas how to do this better?