0

I have a textbox in windows forms with mono spaced font (Courier New, 10pt) so I assume that each line of the textbox will contain the same number of characters. However, sometimes the first line contains one more character than all the others.

This is how the textbox is formattet for the string textbox.Text = "?06?00?00?00?01?00?00?00?00?00?01?00?00?04?00?00?00?00?00?00?01?00?00?00?00?00?00?00?02?00?00?00?":

?06?00?00?00?01?00?00?00?
00?00?01?00?00?04?00?00?
00?00?00?00?01?00?00?00?
00?00?00?00?02?00?00?00?

The same text box looks like this for another string textbox.Text = "06 00 00 00 01 00 00 00 00 00 01 00 00 04 00 00 00 00 00 00 01 00 00 00 00 00 00 00 02 00 00 00 f4 01 00 00 ":

06 00 00 00 01 00 00 00 00 
00 01 00 00 04 00 00 00 00 
00 00 01 00 00 00 00 00 00 
00 02 00 00 00 f4 01 00 00 

Note that for the first string the textbox contains 25 characters in the first line and 24 in the others. For the second string the textbox contains 26 characters in each line (theres a space at the end of each line)

Is "Courier New" really a monospaced font? (I read in several places that it is.)

If "Courier New" is a monospaced font then why is this happening?

How can this be fixed? (I just want to have the same amount of characters in each line).

  • 1
    I'm assuming that this is a text box with the Multiline property set to true, and that you are wondering why you are getting this line-breaking behavior. A multiline textbox is simply a Win32 textbox control (which is pretty much the same as the client text window in Notepad). Just like Notepad, you get line-break behavior. And, just like in Notepad, lines break on spaces or some punctuation. In the first case, you are getting breaks at the question marks. Since your first line has an extra one at the beginning, it's a bit longer. In the second case, you are getting breaks on the spaces. – Flydog57 Nov 30 '20 at 15:17
  • It's not clear how you'd want the first block to be presented, given that you have a different number of chars per line. Remove the leading question mark and it's like the second block. – Jimi Nov 30 '20 at 15:24
  • To answer the other questions... Yes Courier New is monospaced. To me, Consolas usually looks better (as a monospaced font). I think, but I'm not sure, that the only way you are going to get the line breaking behavior you want is to do it yourself. Use `Graphics.MeasureString` to figure out how long the string is using the Graphics associated with the text box, then chop the string up yourself to get it to fix. You'll need to handle the size changed event to redo the chopping up if the textbox changes width – Flydog57 Nov 30 '20 at 15:24
  • @Flydog57 : Yes it is a textbox with multiline property set to true. I don't see a way to change line-break behavior in the properties of the textbox. Is there any way to change it in a way that it breaks on any character? Edit: Just saw that you already answered this question :) – Felix Knopp Nov 30 '20 at 15:26
  • @FelixKnopp, based on my test, I reproduced your problem. I find that it will not be monospaced when we use the character such as '?','!' etc. Therefore, I recommend that you can insert a line break every n characters. Please refer to the link [C# line break every n characters](https://stackoverflow.com/questions/7768373/c-sharp-line-break-every-n-characters). – Jack J Jun Dec 01 '20 at 02:59

0 Answers0