473,563 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.CreateGraphi cs().MeasureStr ing(textbox1.Te xt,
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_Ke yPress(ByVal sender As Object, ByVal e As
System.Windows. Forms.KeyPressE ventArgs) Handles RichTextBox1.Ke yPress
' ADJUSTS HEIGHT OF LABEL BASED ON AMOUNT OF TEXT
RichTextBox1.He ight =
Me.CreateGraphi cs().MeasureStr ing(RichTextBox 1.Text,
RichTextBox1.Fo nt, RichTextBox1.Wi dth).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 14318
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_Co ntentsResized(B yVal sender As Object, ByVal
e As System.Windows. Forms.ContentsR esizedEventArgs ) Handles
RichTextBox1.Co ntentsResized
' THE "10" IS FOR THE SIZE DIFFERENCE BETWEEN THE HEIGHT OF YOUR FONT
AND THE PADDING YOU'D LIKE TO GIVE IT
If RichTextBox1.He ight <e.NewRectangle .Height + 10 Then
RichTextBox1.He ight = 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
11728
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 MyString As String Am I getting VLS declaration or a FLS declaration? Can I control which I get somehow? I have done some homework, but I don't...
1
2196
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. The value is optional (i.e., leaving the textbox blank is perfectly acceptable). The problem is that I pass the UI values down to the DAL for...
4
23800
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 specifiers" error. Thank you, Scott
2
5897
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 record. If I leave a field totally blank, and then click the Update button (which uses the DataAdapter.Update() command, I get an error saying: ...
10
4566
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 zero-length string (always returns a zero-length string when used in a query expression)" **** How many records are there in FirstTable in which...
2
6081
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 FindValuesButton_Click(System::Object * sender, System::EventArgs * e) { string fileLoc ; fileLoc= CD_location->Text->ToString();
1
9407
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 form data in a fixed length structure that is written direct to disk. I need to load the existing files, into a fixed length structure to...
4
3693
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 how many times the user has added the control. For example, the first textbox is on the form at design time; its name is tbItem1. If the user...
5
3653
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 solve this ..?
0
7665
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.