Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET size font
Greetings -- Have been looking around for an answer for a long time without luck.
 
VB.NET 2010 - I am looking to take a string from a database (No newlines, but may in the future) and display the text in a multiline textbox with wordwrapping and have the text's font size adjust to ensure all the text is displayed. Strings are between 50 to 300 characters in length, so the font size can vary wildly.
 
Doable?
 
Thanks!
Posted 4-Dec-10 16:00pm
VyReN444
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I've never done exactly what you are requesting, but I wanted to point out the ZoomFactor[^] property of the RichTextBox. Perhaps that can be used. And I vaguely remember something about being able to determine if scrollbars are needed in a textbox...I had trouble finding it again though...and it's not pretty[^]. Maybe you can google and find something better. Hope this helps.
  Permalink  
Comments
vyren at 7-Dec-10 2:55am
   
That did the trick! Took a little while to correctly detect the scrollbars, but it seems to be working! Posted my code as an answer in this question. THANKS!!
Dalek Dave at 7-Dec-10 3:41am
   
Excellent Answer!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Got it! Kschuler, you pointed me in the right direction!
Here is a quick sloppy sample I created, when the Button is clicked, the Rich Text Box is adjusted to fill the entire box.
 
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As IntPtr, ByVal nIndex As Integer) As Integer
    Public Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Integer) As Integer
    Public Const GWL_STYLE As Integer = (-16)
    Public Const WS_VSCROLL As Integer = &H200000
    Public Const WS_HSCROLL As Integer = &H100000
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Center the text in the RichBox
        RichTextBox1.SelectAll()
        RichTextBox1.SelectionAlignment = HorizontalAlignment.Center
        'Is scrollbar visible?
        Dim bVScrollBar As Boolean
        bVScrollBar = ((GetWindowLong(Me.RichTextBox1.Handle, GWL_STYLE) And WS_VSCROLL) = WS_VSCROLL)
        Select Case bVScrollBar
            Case True
                'Scrollbar is visible - Make it smaller
                Do
                    RichTextBox1.ZoomFactor = RichTextBox1.ZoomFactor - 0.01
                    bVScrollBar = ((GetWindowLong(Me.RichTextBox1.Handle, GWL_STYLE) And WS_VSCROLL) = WS_VSCROLL)
                    'If the scrollbar is no longer visible we are done!
                    If bVScrollBar = False Then Exit Do
                Loop
            Case False
                'Scrollbar is not visible - Make it bigger
                Do
                    RichTextBox1.ZoomFactor = RichTextBox1.ZoomFactor + 0.01
                    bVScrollBar = ((GetWindowLong(Me.RichTextBox1.Handle, GWL_STYLE) And WS_VSCROLL) = WS_VSCROLL)
                    If bVScrollBar = True Then
                        Do
                            'Found the scrollbar, make smaller until bar is not visible
                            RichTextBox1.ZoomFactor = RichTextBox1.ZoomFactor - 0.01
                            bVScrollBar = ((GetWindowLong(Me.RichTextBox1.Handle, GWL_STYLE) And WS_VSCROLL) = WS_VSCROLL)
                            'If the scrollbar is no longer visible we are done!
                            If bVScrollBar = False Then Exit Do
                        Loop
                        Exit Do
                    End If
                Loop
        End Select
    End Sub
  Permalink  
v2
Comments
Dalek Dave at 7-Dec-10 3:41am
   
Always nice when it is solved.
Kschuler at 7-Dec-10 9:18am
   
Nice. You could probably publish this as a tip/trick.
vyren at 7-Dec-10 16:27pm
   
Thanks for the help - I've seriously been trying to do this with VB.net for 3 years. I've been prancing around the house like a little schoolgirl all day. :) I do need to add this as a tip/trick, I know I've searched high and low for something like this and never found it. I never thought of using ZoomFactor in this way, its always good to get a fresh set of eyes on a problem!! THANK YOU!!! Plus, if I now save the text in the DB in RTF I can get colors, bold, etc to display right away too. AWESOME. Totally totally awesome!
vyren at 7-Dec-10 17:05pm
   
Added! http://www.codeproject.com/Tips/134418/VB-net-Change-font-size-to-fit-all-text-in-a-RichT.aspx
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There is no function to tell you what the font size should be to get everything to fit.
 
I had to do something similar, but without the requirements of editing the text in the box. What I did was make a custom control that displayed the text painted in a panel. But, in order to get the text to fit properly without constantly guessing at the right font, I painted the text oversized in an offscreen buffer scaled up from teh actual size of my rendering area. Then I just painted the picture scaled down to fit the width or height of the render area, depending on which was greater.
  Permalink  
Comments
Walt Fair, Jr. at 5-Dec-10 0:41am
   
I don't have a better solution, but that wouldn't be a multiline text box, nor would it be editable.
vyren at 7-Dec-10 2:56am
   
I never said I needed to edit it. :) But thanks!
Dalek Dave at 7-Dec-10 3:41am
   
Good Call.

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

  Print Answers RSS
0 OriginalGriff 278
1 Maciej Los 205
2 Nguyen.H.H.Dang 190
3 arvind mepani 187
4 Peter Leow 180


Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 7 Dec 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid