473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Auto-adjust height of richtextbox (or textbox) based on length of string?

Hey all...

I've been using the following to calculate the height the textbox
needs to be in order to grow it vertically to its necessary size.

textbox1.Height = Me.CreateGraphics().MeasureString(textbox1.Text,
textbox1.Font, textbox1.Width).Height

However it's not appearing to work that well, especially when I'm
using a RichTextbox control, and adjusting it (or checking it) on each
KeyDown event.

Private Sub richtextbox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress
' ADJUSTS HEIGHT OF LABEL BASED ON AMOUNT OF TEXT
RichTextBox1.Height =
Me.CreateGraphics().MeasureString(RichTextBox1.Tex t,
RichTextBox1.Font, RichTextBox1.Width).Height
End Sub

Any other methods of determining this? In the end, I'd like a
richtextbox control that grows vertically (only) as the text wraps to
a second line (and only grows at that point, no sooner)

Thanks...

Jan 31 '07 #1
1 14302
Wow... found the answer, was easier than I thought. It takes into
account at least bold, underline text. Not sure about tables, etc.
though.

Private Sub RichTextBox1_ContentsResized(ByVal sender As Object, ByVal
e As System.Windows.Forms.ContentsResizedEventArgs) Handles
RichTextBox1.ContentsResized
' THE "10" IS FOR THE SIZE DIFFERENCE BETWEEN THE HEIGHT OF YOUR FONT
AND THE PADDING YOU'D LIKE TO GIVE IT
If RichTextBox1.Height <e.NewRectangle.Height + 10 Then
RichTextBox1.Height = e.NewRectangle.Height + 10
End If
End Sub

Jan 31 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: MLH | last post by:
I'm working with lots of long strings now, it seems. I have to import them & parse them constantly. The A97 memo field type supports only 32768 chars. What happens when this is processed... Dim...
1
by: Guadala Harry | last post by:
What do to about conversion to decimal type when blank is allowed? The situation is this. In the UI there is a textbox into which users can enter a value which is supposed to be a dollar amount....
4
by: Scott Lemen | last post by:
Hi, Some Win APIs expect a structure with a fixed length string. How is it defined in VB .Net 2003? When I try to use the FixedLengthString class I get an "Array bounds cannot appear in type...
2
by: johnb41 | last post by:
I have a simple form that has a bunch of textboxes that show data from a dataset table. When I "Add" a record with BindingManagerBase.AddNew(), all the textboxes go blank, so I can add a new...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
2
by: rachit.goyal | last post by:
Hi All, I am using .net first time.In my windows form VC7 application, on a click of button, I am assigning text of TextBox to STL string varibale. Using this code- private: System::Void...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all...
4
by: Randy | last post by:
My situation is that I have a form on which a number of textboxes and comboboxes are added dynamically based on interaction with the user. As these controls are added, they are given names based on...
5
mafaisal
by: mafaisal | last post by:
Hello Experts I am Using Vb.net2005 windows app My Doubt is How we can change the height of TextBox without multiline ie, in multi line textbox we can change but other Fixed height !! Hw can...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.