1
votes

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:

  1. He can press Return, to manually make new line character, which always should be in this place.
  2. He can press space to start writing new word.
  3. 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?

1
you can't inherit from string... have you looked for similar cases in google?No Idea For Name
My mistake. I thought about boxing string into my own class. Of course I have searched in Google. Asking question on Stack Overflow is always the last thing I do, when I'm solving problem.Marcin Bigoraj
@MarcinBigoraj have you looked into just setting the PageWidth of the FlowDocument? msdn.microsoft.com/en-us/library/… For the row maximum I would just keep track of how many newline characters there are.ToastyMallows
I haven't looked into FlowDocument, but I have already solved my problem. Counting newline characters is something, that is natural, but unfortunately, it is wrong. In RichBoxText appear no newline characters, if user haven't write them by himself. So if I'm writing one sentence, which is written in 3 rows, there are no newline characters.Marcin Bigoraj

1 Answers

0
votes

I figured it out. My other idea was to use StringReader and read whole text character by character, but it was also very buggy and painful to implement.

The best way is to not change the default behaviour of RichTextBox. I'm not splitting the text, I'm not checking if there is a space for word in line. My RichTextBox has fixed size and I'm okay with that. There is no problem with 18 characters in row, but the problem is with counting lines, because it's not that simple like counting new line characters.

The reason I wanted to change the behaviour is because later I must send this text line by line. Manually counting how many words fit in one line and dynamically create lines seemed to be easy, but it was to buggy.

RichTextBox has very useful method GetLineFromCharIndex. I'm able to determine how many actual lines I have, by passing Text.Count() to it. Now I have two time shorter code than before and it seems there are no bigger bugs. Sending the text line by line it's not so easy, because I needed to manually count in which line I am, when reading characters, but overall, it was more acceptable then previous solution.