Results 1 to 9 of 9

Thread: Textbox autosize

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2009
    Posts
    46

    Textbox autosize

    Does anyone know how I would go about making a textbox grow when the user's text extends past the original size and then shrinks when the text is deleted, up to it's original size? I've looked around the internet for a while and haven't seen anyone attempting this...

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Re: Textbox autosize

    (sender as TextBox).Width = TextRenderer.MeasureText((sender as TextBox).Text, (sender as TextBox).Font).Width;
    Add the above code to the boxes TextChanged event

    Just add an if statement to make sure it doesn't get smaller than your minimum size
    Last edited by Cander; Oct 13th, 2009 at 08:17 PM.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2009
    Posts
    46

    Re: Textbox autosize

    I knew it'd be something simple. Thanks!

    Now, I've been trying to figure out how to make it extend the height with one carriage return once the textbox hit the max width... then once it hit the max height it'd scroll the earlier text upwards. Know what I mean?

    i tried it like this, just guessing, which didn't work
    Code:
    if (textbox1.Width <= 293)
    {
         (sender as TextBox).Width = TextRenderer.MeasureText((sender as TextBox).Text, (sender as TextBox).Font).Width;
    }
    else
    {
         if (textbox1.Height <= 62)
         {
              (sender as TextBox).Height = TextRenderer.MeasureText((sender as TextBox).Text, (sender as TextBox).Font). Height;
         }
    }

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Re: Textbox autosize

    It would be something like this (need to use a RichTextBox).

    Code:
                textBox1.TextChanged += new EventHandler((obj, args) =>
                {
                    if ((obj as RichTextBox).Width <= 200)
                        (obj as RichTextBox).Width = TextRenderer.MeasureText((obj as RichTextBox).Text, (obj as RichTextBox).Font).Width;
    
                    if (TextRenderer.MeasureText((obj as RichTextBox).Lines[(obj as RichTextBox).Lines.Length - 1], (obj as RichTextBox).Font).Width > 200)
                        (obj as RichTextBox).AppendText("\r\n");
    
                    (obj as RichTextBox).Height = TextRenderer.MeasureText((obj as RichTextBox).Text, (obj as RichTextBox).Font).Height + 10;
                });
    For some reason the first line moves up beyond the bounds of the control but every other line after that works. If I figure out why I will add the fix.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2009
    Posts
    46

    Re: Textbox autosize

    Just a couple of things, I wish I knew more about this particular method call so I'd know a little more of how to customize your suggestions...

    First, if I delete all the text in the rtb, the program stops responding with an error saying "Index was outside the bounds of the array."

    Also, I changed it to look like this:
    Code:
    if ((obj as RichTextBox).Width <= 293)
                        (obj as RichTextBox).Width = TextRenderer.MeasureText
                            ((obj as RichTextBox).Text, (obj as RichTextBox).Font).Width;
    
                    if (TextRenderer.MeasureText((obj as RichTextBox).Lines
                        [(obj as RichTextBox).Lines.Length - 1], (obj as RichTextBox)
                        .Font).Width > 293)
    
                    (obj as RichTextBox).Height = TextRenderer.MeasureText
                        ((obj as RichTextBox).Text, (obj as RichTextBox).Font).Height + 20;
                });
    so that it would show the last line written rather than start with a blank box.
    I'm not sure if that caused any problems, lol, I'm no expert. The delete fix baffled me though...

  6. #6
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Re: Textbox autosize

    Just a couple of things, I wish I knew more about this particular method call so I'd know a little more of how to customize your suggestions...
    Sorry about that. That is a functional style methodolgy. I have been studying functional programming a little more lately so it is becoming a habit. It is called an anonymous method. It is just another way to define a delegate. The only thing that was important was what was in between. But for your education I will explain it a bit. The (obj,args) part is the 2 parameters for the control's Click eventhandler. The => is a Lamba expression that tells the compiler that the stuff on the left are the paramters and pass them into the function to the right. Then you have the actual inline function code. They call it anonymous methods because the compiler really doesnt care what the name of a function is so no need to define it and the compiler is smart enough to know the obj is of type Object and args is of type EventArgs.

    But anyway:

    Code:
    if ((obj as RichTextBox).Width <= 200)
                        (obj as RichTextBox).Width = TextRenderer.MeasureText((obj as RichTextBox).Text, (obj as RichTextBox).Font).Width;
    
                    if (TextRenderer.MeasureText((obj as RichTextBox).Lines[(obj as RichTextBox).Lines.Length - 1], (obj as RichTextBox).Font).Width > 200)
                        (obj as RichTextBox).AppendText("\r\n");
    
                    (obj as RichTextBox).Height = TextRenderer.MeasureText((obj as RichTextBox).Text, (obj as RichTextBox).Font).Height + 10;
    I am away from my development machine right now but I figure the delete problem is because of the ((obj as RichTextBox).Lines[(obj as RichTextBox).Lines.Length - 1]. Since once you delete everything there are no more lines. Best way to handle that would be to wrap that 2nd if with another if to not run that line of code if the line count is 0
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2009
    Posts
    46

    Re: Textbox autosize

    Ahh, okay... got it. The only thing that I'm having a little bit of an issue with is that the method seems really laggy. Is that just my machine or does that method maybe just create some lag?

  8. #8
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913

    Re: Textbox autosize

    I didn't have any lag with it but then again I code on a pretty beefy machine. -_-

    Only thought on that is to go ahead and cast the sender object to RichTextBox at the start into a new variable then replace all the (obj as RichTextBox) to that variable. Maybe that constant casting conversion is causing some slow down. Just a guess though.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2009
    Posts
    46

    Re: Textbox autosize

    Yah, I tried that, it made it a little bit faster, but not much. I only use 1 pc, which I wouldn't have if it wasn't for my school requiring the use of a tablet. I use a Toshiba M400 tablet with the worst graphics card you can buy because that's what our school decided to get. Sounds great huh? :P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width