Link to home
Start Free TrialLog in
Avatar of designwitt
designwitt

asked on

Center Text Verically and Horizontal in a rich text box

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
Avatar of ladarling
ladarling
Flag of United States of America image

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

Avatar of designwitt
designwitt

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of ladarling
ladarling
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial