Agasi Mkhitaryan
Agasi Mkhitaryan

Reputation: 180

Make textbox add new line automatically when it's end of a line

This seems an easy task, however, I can't find any working method to do it. I want a textbox add extra line to itself automatically when we type at the end of a full line (so, we are redirected to a new line). Maybe I show it better as follows:
Textbox current value: asdfghj(this is full length of a textbox)
We type new string after j: asd. And I see:

asd

Only one line, to see the first line I need to scroll up ^
And I want to see:

asdfghj
asd

Two lines.

I tried this code:

  private void textBox1_TextChanged(object sender, EventArgs e)
    {
        Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
        textBox1.Width = size.Width;
        textBox1.Height = size.Height;
    }

But the extra line is being created when I press enter or shift-enter only. And I want it to be automatically added. I also have Multiline=true and Wordwrap=true.

Upvotes: 0

Views: 3128

Answers (2)

Pikoh
Pikoh

Reputation: 7703

Kind of a hack, but try this and see if it fits your needs:

    int previouslines = 1;
    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        int size=textBox2.Font.Height;
        int lineas = textBox2.Lines.Length;
        int newlines = 0;
        if (textBox2.Text.Contains(Environment.NewLine))
        {
            newlines = textBox2.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).Length - 1;
            lineas += newlines - (textBox2.Lines.Length - 1);
        }

        for(int line_num= 0;line_num<textBox2.Lines.Length;line_num++)
        {
            if (textBox2.Lines[line_num].Length > 1)
            {
                int pos1=textBox2.GetFirstCharIndexFromLine(line_num);
                int pos2= pos1 + textBox2.Lines[line_num].Length-1;
                int y1 = textBox2.GetPositionFromCharIndex(pos1).Y;
                int y2 = textBox2.GetPositionFromCharIndex(pos2).Y;
                if (y1 != y2)
                {
                    int aux = y2+size;
                    lineas = (aux / size);
                    if (y1 != 1)
                    {
                        lineas++;
                    }

                    lineas += newlines - (textBox2.Lines.Length - 1);
                }
            }
        }

        if (lineas > previouslines)
        {
            previouslines++;
            textBox2.Height = textBox2.Height + size;
        }
        else if (lineas<previouslines)
        {
            previouslines--;
            textBox2.Height = textBox2.Height - size;
        }
    }

Upvotes: 2

Dieter Meemken
Dieter Meemken

Reputation: 1967

If MultiLine is set, you could do it like this:

1) Estimate the lenght of the last line of text in the TextBox by splitting it into an array (may be not the fasted possible)

2) If that line has more the MAX_CHARS chars, then

3) Take all of the text, except the last char, and add new line and then that char

4) Correct selection and position

    const int MAX_CHARS = 10;
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        string[] sTextArray = textBox1.Text.Split( new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries );
        int nLines = sTextArray.Length;
        string sLastLine = sTextArray[nLines -1];
        if (sLastLine.Length > MAX_CHARS)
        {
            int nTextLen = textBox1.Text.Length;
            string sText = textBox1.Text.Substring(0, nTextLen - 1) + Environment.NewLine + textBox1.Text[nTextLen - 1];
            textBox1.Text = sText;
            textBox1.SelectedText = "";
            textBox1.Select(nTextLen +2, 0);
        }
    }

Upvotes: 0

Related Questions