Click here to Skip to main content
14,330,218 members
Rate this:
Please Sign up or sign in to vote.
See more:
so im making an application with textboxes. I dont want the textbox to be scrollable and that it gets bigger when new text is added. everything works fine with how i coded it. there is only one problem. when you keep typing without making a new line. the textbox does not become bigger and a scrollbar appears.
example:
Imgur: The magic of the Internet[^]
how do i prevent this from happening?

What I have tried:

i tried to detect when the text is at the end of the textbox and add a new line then. but i could not get it to work properly.
Posted
Updated 28-Jul-19 7:07am
Rate this:
Please Sign up or sign in to vote.

Solution 3

I agree with OG's remarks, that it is better to use ScrollBars; however, I can't claim I understand your goal here. If you really want a change-size-while-typing TextBox, here's a clue:

With a WinForm TextBox with 'WordWrap and 'Multiline set to 'true, 'textBox1'
private int fontht, baseheight;

private void Form1_Load(object sender, EventArgs e)
{
    fontht = textBox1.Font.Height;
    baseheight = textBox1.Height;
}

private void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
    int numLines = textBox1.GetLineFromCharIndex(textBox1.TextLength);

    if (numLines == 0)
    {
        if (textBox1.Height != baseheight)
        {
            textBox1.Height = baseheight;
        }                    

        return;
    }

    int newht = baseheight + (fontht * numLines);

    if (newht == textBox1.Height) return;

    textBox1.Height = newht;
}
You can improve this by taking into account TextBox properties 'Padding, and the difference between textBox1.Height and textBox1.ClientSize.Height which will give you the border width in effect. An alternative technique would use 'MeasureString.
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

Simple solution: don't do it.
Text boxes aren't "meant" to grow to fit the content: by which I mean that users aren't expecting that, and anything unexpected confuses them. And what do you expect to happen when the box exceeds the bounds of the form? What is the user supposed to see then? How can he see what he typed, or find how to go back to the beginning, or even see the buttons to submit the data or cancel?

Make your text boxes adjust in size to fit within a form or other container, yes - but anything else is a lot of effort for no good result in terms of usability: that is what scroll bars are there for. And if you make your app harder to use, they won't use it!
   
Comments
dolfijn3000 28-Jul-19 6:19am
   
the thing is that currently the scrolbar is making it hard to use becous the textbox does not grown. becous it does not grown the view of the text is verry small.
OriginalGriff 28-Jul-19 6:32am
   
You're going to have to run that past us again: the size of a textbox has nothing to do with the size of text inside, unless you are altering the font size based on the amount of content. Which would be really silly as it would make text unreadable very quickly...

You make the box big enough to read the average amount of text it will hold: a book would get a whole page at a time, an address four or five lines, a name 20 to 50 characters. You don't make a box start out with 20 characters, and expand to a page (or vice versa) - that just not good UI design.
dolfijn3000 28-Jul-19 6:34am
   
I dont know how big the text wil be. the user can write anything they want inside of it. they can write a single line in it or a whole story.
OriginalGriff 28-Jul-19 6:43am
   
Then you make it big enough to hold "some text" and "fit in" with the rest of the UI. Making the text box expand to fit the text a user enters doesn't make any sense when you try to use it because it either fills the whole screen (and the desktop doesn't scroll) or it interferes with other controls that they may need.
Think about it: if you have even Word open in a window, it doesn't get bigger when you reach the end of a line or a page, does it? :laugh:
dolfijn3000 28-Jul-19 6:48am
   
ok i will try that. and also that it would fill up the screen is not a problem. the panel where the textbox, buttons etc... are in is scrollable.
https://imgur.com/a/gDorPXl

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100