Solved

Center Text Verically and Horizontal in a rich text box

Posted on 2008-10-07
4
840 Views
Last Modified: 2009-01-09
hello all,
i am making a little flash card app to help me with some studying, im wondering how do i center the text vertically and horizontally in the text box? is there a way or do i need to use another control? i wanting the text to appear as if it was on a note card like a regular flash card, with the text in the middle.

Thanks for the help
0
Comment
Question by:designwitt
  • 2
4 Comments
 
LVL 11

Expert Comment

by:ladarling
ID: 22662376
Horizontal is easy...

With RichTextBox1
.Text = "Hello"
.SelectAll()
.SelectionAlignment = HorizontalAlignment.Center
End With
vertical is a pain in the %$#^&&*.
You could just 'draw' the text centered on any control you want, provided that you dont need to select / copy / paste from it.
Here is a method that will draw centered to a panel control named Panel1. You could modify it to draw to any control that has a .CreateGraphics() method...
 

    Public Sub DrawText(ByVal dText As String)
        Dim g As Graphics
        g = Panel1.CreateGraphics
        Dim centerPoint As New Point(Panel1.Width / 2, Panel1.Height / 2)
        Dim drawPoint As New Point
        Dim textSize As SizeF
 
        Dim myFont As New Font("Tahoma", 20, FontStyle.Bold, GraphicsUnit.Pixel)
        textSize = g.MeasureString(dText, myFont, Panel1.Size, StringFormat.GenericDefault)
        drawPoint.X = centerPoint.X - (textSize.Width / 2)
        drawPoint.Y = centerPoint.Y - (textSize.Height / 2)
 
        g.DrawString(dText, myFont, Brushes.Blue, drawPoint)
    End Sub

Open in new window

0
 

Author Comment

by:designwitt
ID: 22666266
cool, yeah that worked great, is there way to make it stay in the panel? like wordwrap or something? because when i put a long definition in there it just runs off the side.

Thanks
0
 
LVL 11

Accepted Solution

by:
ladarling earned 500 total points
ID: 22668920
The best way to handle that would probably be to pre-format the text prior to passing it to the draw function. This will take a little design forethought, though, since font size will really determine the breakpoints of the text. I slapped this together really quickly, and I call it in the first line of the DrawText function. The basic premise is to insert a carriage return near a space where your text would 'overrun' the panel. The draw method can handle the multiple lines, it just a matter of breaking them where needed.

    Private Function WrapText(ByVal theText As String) As String
        'The test value (20) would change with font size, here its an arbitrary example.
        'It assumes that only around 20 characters will fit on a 'line', 
        'and we should break the text somewhere near that.
        'If we were needing to break multiple times, you could rewrite this as
        'loop, replacing a space around 20, 40, 60, and so forth.
 
        If theText.Length > 20 Then
            'Lets find a space to break at
            Dim spcLoc As Integer = InStr(15, theText, " ") 'Start looking for a space at character 15
            If spcLoc > 0 Then
                theText = theText.Insert(spcLoc, vbCrLf)
            End If
        End If
        Return theText
 
    End Function

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to use odbc in vb to connect to ms sql 14 62
ASP/VB email question 4 46
VB.Net - MemoryMappedFiles - Confirm receipt 2 24
vb.net 1 month apart 11 35
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question