Centering text in user control

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
I'm trying to make a user control that I call a DigitalDisplay. I have most of it working, but I'm trying to make it center the text, depending on the length of the text that is entered. I'm having trouble with this, and it's either in the Text3Rect or Text3Brush, or maybe both. I'm not sure where the change should be made, and what I need to do to fix it. My control looks like this:

1087079149_9f9c9d5ab1.jpg


I'm going to attach my project and also post the code here. Thanks for any help with this.
VB.NET:
Public Class UserControl1
    Inherits UserControl

    Dim mytext As String = "TEST"
    Dim bcolor As Color = Color.Red
    Dim g As Graphics = Graphics.FromHwnd(Me.Handle)

    Private stroke3 As System.Drawing.Pen = New System.Drawing.Pen(System.Drawing.Color.FromArgb(0, 255, 255, 255), 1.0!)

    Private Ellipse3Brush As System.Drawing.SolidBrush = New System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(255, 255, 255, 255))

    Private Ellipse3Pen As System.Drawing.Pen = New System.Drawing.Pen(System.Drawing.Color.DimGray, 2.0!)

    Protected Ellipse3 As System.Drawing.Rectangle = New System.Drawing.Rectangle(0, 1, Me.Width * 2 - 6, Me.Height)


    Private Ellipse1Brush As System.Drawing.Drawing2D.LinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(New System.Drawing.Rectangle(0, 0, Me.Width, Me.Height), System.Drawing.Color.FromArgb(215, 255, 0, 0), System.Drawing.Color.FromArgb(255, 0, 0, 0), System.Drawing.Drawing2D.LinearGradientMode.Vertical)

    Protected Ellipse1 As System.Drawing.Rectangle = New System.Drawing.Rectangle(3, 4, Me.Width * 2 - 10, 146)

    Protected Text3 As String = "TESTING"

    ' Private Text3Rect As System.Drawing.RectangleF = New System.Drawing.RectangleF(Me.Width / 2 - 42, Me.Height / 2 - 24, Me.Width + 100, 50)


    Private Text3Format As System.Drawing.StringFormat = New System.Drawing.StringFormat(System.Drawing.StringFormat.GenericTypographic)

    Private Text3Font As System.Drawing.Font = New System.Drawing.Font("DS-Digital", 48.0!, CType(1, System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, 1)

    Private Text3Brush As System.Drawing.Drawing2D.LinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(New System.Drawing.Rectangle(Text3Rect.X, Text3Rect.Y, g.MeasureString(Text3, Text3Font, 200).Width, Text3Rect.Height + 10), bcolor, System.Drawing.Color.FromArgb(110, 255, 255, 255), System.Drawing.Drawing2D.LinearGradientMode.Vertical)

    Private Text3Rect As System.Drawing.RectangleF = New System.Drawing.RectangleF(Me.Width / 2 - 42, Me.Height / 2 - 24, Me.Width + 100, 50)


    Private Ellipse2Brush As System.Drawing.Drawing2D.LinearGradientBrush = New System.Drawing.Drawing2D.LinearGradientBrush(New System.Drawing.Rectangle(42, 3, 212, 83), System.Drawing.Color.FromArgb(255, 255, 255, 255), System.Drawing.Color.FromArgb(0, 255, 255, 255), System.Drawing.Drawing2D.LinearGradientMode.Vertical)

    Protected Ellipse2 As System.Drawing.Rectangle = New System.Drawing.Rectangle(42, 3, Me.Width * 2 - 85, Me.Height * 2 - 214 - 6)

    Private Ellipse5Brush As System.Drawing.SolidBrush = New System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(0, 255, 255, 255))

    Private Ellipse5Pen As System.Drawing.Pen = New System.Drawing.Pen(System.Drawing.Color.Black, 2.0!)

    Protected Ellipse5 As System.Drawing.Rectangle = New System.Drawing.Rectangle(1, 0, Me.Width * 2 - 6, Me.Height + 2)


    Private Sub InitializeGraphics()
        Me.Text3Format.Alignment = System.Drawing.StringAlignment.Near
        Me.BackColor = Color.Transparent

    End Sub

    Public Overridable Sub RenderGraphics(ByVal g As System.Drawing.Graphics)
        Dim sz As SizeF
        sz.Width = Ellipse3.Width
        sz.Height = Ellipse3.Height
        ' Text3Rect = New System.Drawing.RectangleF(Text3Rect.Width - g.MeasureString(Text3, Text3Font, 5).Width, Me.Height / 2 - 24, Me.Width + 100, 50)
        Text3Brush = New System.Drawing.Drawing2D.LinearGradientBrush(New System.Drawing.Rectangle(Text3Rect.X - g.MeasureString(Text3, Text3Font, 200).Width, Text3Rect.Y, g.MeasureString(Text3, Text3Font, 200).Width, Text3Rect.Height + 10), bcolor, System.Drawing.Color.FromArgb(110, 255, 255, 255), System.Drawing.Drawing2D.LinearGradientMode.Vertical)
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias
        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
        g.FillEllipse(Me.Ellipse3Brush, Me.Ellipse3)
        g.DrawEllipse(Me.Ellipse3Pen, Me.Ellipse3)
        g.FillEllipse(Me.Ellipse1Brush, Me.Ellipse1)
        g.DrawEllipse(Me.stroke3, Me.Ellipse1)
        g.DrawString(Me.Text3, Me.Text3Font, Me.Text3Brush, Me.Text3Rect, Me.Text3Format)
        g.FillEllipse(Me.Ellipse2Brush, Me.Ellipse2)
        g.DrawEllipse(Me.stroke3, Me.Ellipse2)
        g.FillEllipse(Me.Ellipse5Brush, Me.Ellipse5)
        g.DrawEllipse(Me.Ellipse5Pen, Me.Ellipse5)
    End Sub

    'Required to dispose of created resources
    

    

    Private Sub UserControl1_ForeColorChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ForeColorChanged
        bcolor = Me.ForeColor
        Ellipse1Brush = New System.Drawing.Drawing2D.LinearGradientBrush(New System.Drawing.Rectangle(0, 0, Me.Width, Me.Height), bcolor, System.Drawing.Color.FromArgb(255, 0, 0, 0), System.Drawing.Drawing2D.LinearGradientMode.Vertical)
        Text3Brush = New System.Drawing.Drawing2D.LinearGradientBrush(New System.Drawing.Rectangle(Text3Rect.X, Text3Rect.Y, Text3Rect.Width, Text3Rect.Height + 10), bcolor, System.Drawing.Color.FromArgb(110, 255, 255, 255), System.Drawing.Drawing2D.LinearGradientMode.Vertical)
        RenderGraphics(g)
    End Sub

    Private Sub UserControl1_Layout(ByVal sender As Object, ByVal e As System.Windows.Forms.LayoutEventArgs) Handles Me.Layout
        RenderGraphics(g)
        Me.Refresh()
    End Sub

    Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        RenderGraphics(e.Graphics)
    End Sub

    Private Sub UserControl1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        RenderGraphics(g)
        Me.Refresh()
    End Sub
    
    <System.ComponentModel.Browsable(True)> _
    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            Text3 = value
            Me.Refresh()
            ' MyBase.Text = value
        End Set
    End Property

    Private Sub UserControl1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        RenderGraphics(g)
        Me.Refresh()
    End Sub
End Class
 

Attachments

  • DigitalDisplay.zip
    14.6 KB · Views: 31
Some help:
  • Remove the private 'g' variable and all calls to RenderGraphics(g), use Me.Refresh instead.
  • Don't create a new Text3Brush in RenderGraphics method. Also remove sz variable that is not used here.
  • Remove the 'bcolor' variable and use Me.ForeColor instead.
  • Remove the 'mytext' variable, it's not used.
  • Remove the 'Text3' variable, use instead MyBase.Text in the Property and Me.Text elsewhere.
  • Use Me.ClientRectangle as first parameter to the LinearGradientBrush constructor.
  • Select the Sub New constructor of the UserControl (top comboboxes in code view) and call your InitializeGraphics method where appropriate.
  • In your InitializeGraphics method set Text3Format.Alignment to Center.

Doing this will fix most of the problems and code errors of your control and also center the text.
 
Back
Top