Search code examples
c#winformsword-wrap

Custom word wrapping with TextBox


I have a multi-line textbox in my WinForms application, I set the WordWrap property to true.

But as you can see, when I set the Text property to the following string, the line breaks at '-' character of ORD-082619/1023.

myTextBox.Text = "ORD-082619/1020, ORD-082619/1021, ORD-082619/1022, ORD-082619/1023";

enter image description here

Is there any way to wrap the text as following image? enter image description here


Solution

  • You could replace the regular hyphens with non-breaking hyphens:

    myTextBox.Text = myTextBox.Text.Replace("-", "\u2011");
    

    enter image description here

    This will of course only work if you don't have any of those before and need to know which was which, (in which case you can keep the original version somewhere, maybe in the Tag ;-)