2

I have a TextBox where I need to show some text. The length of the text is dynamic, so it needs to be wrapped to fit into multiple lines.

The maximum length of a line is 50 characters. If the text is more than that, I need to add a line break \n.

For example, if the text is 165 chars:

  1. Add a \n at 51st position
  2. Add a \n at 102nd position
  3. Add a \n at 153rd position

So finally the total length of text will be 168 chars.

I know how to do this using a loop. But my question is, can this be done without much code? Does the String class have a method to provide that function?

This is a Windows Forms app, but all controls including TextBox are created programmatically.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
don
  • 193
  • 1
  • 3
  • 15

1 Answers1

3

For Windows Forms application

You can use the WordWrap property, set it to true.

If you want to do it dynamically

You can do this in code, using this:

myTextBox.WordWrap = true;
myTextBox.Multiline = true;

If you want to do it in UI

Select the textbox and then press F4. Search for WordWrap, and set it to true.

Also don't forget to set your TextBox as Multiline

Working sample

@Don since you said that using WordWrap doesn't work for you, you can try using regex, like the code below:

using System.Linq;
using System.Text.RegularExpressions;

private void Form1_Load(object sender, EventArgs e)
{
    var textBox = new TextBox
    {
        Multiline = true,
        WordWrap = false,
        Width = 295,
        Height = 100,
        ReadOnly = true
    };

    var textFromDatabase = "1234567890 1234567890 1234567890 1234567890 111150dasdasds1234567890 1234567890 1234567890 1234567890 111150dasdasds1234567890 1234567890 1234567890 1234567890 1111";

    var strings = Regex.Matches(textFromDatabase, ".{0,50}");
    var lines = strings.Cast<Match>()
                       .Select(m => m.Value)
                       .Where(m => !string.IsNullOrWhiteSpace(m));
    var @join = string.Join(Environment.NewLine, lines);

    textBox.Text = @join;

    Controls.Add(textBox);
}

Note that I'm creating a TextBox with WordWrap false and Multiline = true.

Jibin Balachandran
  • 3,381
  • 1
  • 24
  • 38
Alberto Monteiro
  • 5,989
  • 2
  • 28
  • 40
  • how do you suggest without knowing the type of application? – sujith karivelil Dec 31 '15 at 00:58
  • @un-lucky I tried it, he said "TextBox" and also mentioned "\n", so I thought that is a Win Form app, it could be a WPF, but I will change my question, thanks for advise!! – Alberto Monteiro Dec 31 '15 at 01:01
  • User top tags: Winform – user1501127 Dec 31 '15 at 01:07
  • @user1501127 Well noted!! – Alberto Monteiro Dec 31 '15 at 01:11
  • It's a Win Forms app, but there's no designer used here. All controls including TextBox's are created dynamically in code. – don Dec 31 '15 at 01:18
  • @don so I said how to do it dynamically – Alberto Monteiro Dec 31 '15 at 01:23
  • Thanks @cFrozenDeath I removed!! – Alberto Monteiro Dec 31 '15 at 01:25
  • @AlbertoMonteiro I tried this approach earlier and it didn't work. i.e., setting Multiline & WordWrap to true. Please see this posting - http://stackoverflow.com/questions/34534301/textbox-wrapping-text-to-multiple-lines – don Dec 31 '15 at 01:27
  • Seeing the question posted above me, this answer is useless for the OP. – Camilo Terevinto Dec 31 '15 at 01:29
  • @don This is really weird, I tried here and it works properly, are you setting Width and Height? – Alberto Monteiro Dec 31 '15 at 01:35
  • @AlbertoMonteiro I'm setting Width, but not Height – don Dec 31 '15 at 01:35
  • @don try setting Height to 50 and write a lot words in texbox – Alberto Monteiro Dec 31 '15 at 01:36
  • @AlbertoMonteiro One more thing, this is a ReadOnly TextBox, so there's no user input, the text that's displayed comes from a database. – don Dec 31 '15 at 01:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99359/discussion-between-alberto-monteiro-and-don). – Alberto Monteiro Dec 31 '15 at 01:42
  • @AlbertoMonteiro Do you know of any logic in String class to insert line break after every few lines? – don Dec 31 '15 at 01:42
  • @AlbertoMonteiro I am accepting your answer for your effort and also it gave me another idea to get it working on my app. – don Dec 31 '15 at 02:04
  • @don Still wont work for you? I can do without using WordWrap. What the data that come from your database? is it a regular string? Can you paste it here? – Alberto Monteiro Dec 31 '15 at 02:05
  • @don I want to help, please talk to me <3 – Alberto Monteiro Dec 31 '15 at 02:08
  • @AlbertoMonteiro Yes regular text string with alphabets and numbers – don Dec 31 '15 at 02:12
  • @don I added another solution, can you try it? – Alberto Monteiro Dec 31 '15 at 02:15
  • @AlbertoMonteiro Which USING declarations i need to add for RegEx and Match? – don Dec 31 '15 at 02:25
  • @don System.Text.RegularExpressions – Alberto Monteiro Dec 31 '15 at 02:25
  • @AlbertoMonteiro I did that. But get an error for Match. System.Text.RegularExpressions.MatchCollection' does not contain a definition for 'Cast' and no extension method 'Cast' accepting a first argument of type 'System.Text.RegularExpressions.MatchCollection' could be found (are you missing a using directive or an assembly reference?) – don Dec 31 '15 at 02:27
  • @don Add using System.Linq; – Alberto Monteiro Dec 31 '15 at 02:28
  • @AlbertoMonteiro It actally works in wrapping text to multiple lines. So that part is fine. But the problem is the height of TextBox is not auto adjusting to show all the lines in same time. Is there a way to auto adjust TextBox based on number of lines in text? – don Dec 31 '15 at 02:32
  • @don Yeah, you can do it, look this question http://stackoverflow.com/questions/2893059/autoresize-textbox-control-vertically – Alberto Monteiro Dec 31 '15 at 02:34
  • @AlbertoMonteiro Using TextChanged event make it looks great! Now my last questions to you is, if the TextBox width = 100, is it possible to calculate how many characters it can hold in one line? I am sure it will depend based on Language too like Japanese or Arabic might take more space. Is there a function to calculate number of characters that can be displayed on one line of a TextBox with width set to 100? – don Dec 31 '15 at 02:42
  • @don The question that I sent to you to make Height autosize, the code that is used there, also is used to define how many characters can fit in width 100. You will still use the method `TextRenderer.MeasureText` but you will pass just one character in first parameter, then you will have the width of 1 char, and the calculate how many fit in 100 width – Alberto Monteiro Dec 31 '15 at 02:47