-
Oct 13th, 2009, 07:46 PM
#1
Thread Starter
Member
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...
-
Oct 13th, 2009, 08:09 PM
#2
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.
-
Oct 13th, 2009, 08:29 PM
#3
Thread Starter
Member
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;
}
}
-
Oct 14th, 2009, 09:46 AM
#4
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.
-
Oct 14th, 2009, 08:35 PM
#5
Thread Starter
Member
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...
-
Oct 15th, 2009, 08:00 AM
#6
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
-
Oct 15th, 2009, 11:27 AM
#7
Thread Starter
Member
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?
-
Oct 15th, 2009, 11:44 AM
#8
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.
-
Oct 17th, 2009, 05:48 PM
#9
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|