0

Goal

I want to display a vertical text on the left of a user control to let a user know which product they are creating/editting. Like so:

enter image description here


How am I building it?

This user control is made up of three controls.

  1. Label with text "Product Information". Dock=Top
  2. User Control with a vertical draw string text of "Product #1". Dock=Left
  3. Table Layout panel which contains X amount of user controls inside it. Dock=Fill

Here's the design view:

enter image description here

Here is the code for my product name user control that draws "Product #1"

Public Class uProductName
    Public drawString As String = "Product #1"

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        ' Call the OnPaint method of the base class.
        MyBase.OnPaint(e)
        ' Call methods of the System.Drawing.Graphics object.
        DrawVerticalString(e)
    End Sub

    Public Sub DrawVerticalString(ByVal e As PaintEventArgs)
        Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
        Dim drawFont As New System.Drawing.Font("Arial", 20)
        Dim drawBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Black)
        Dim stringSize As New SizeF
        stringSize = e.Graphics.MeasureString(drawString, drawFont)
        Dim x As Single = (Me.Width / 2) - (stringSize.Height / 2)
        Dim y As Single = (Me.Height / 2) - (stringSize.Width / 2)
        Dim drawFormat As New System.Drawing.StringFormat
        drawFormat.FormatFlags = StringFormatFlags.DirectionVertical
        formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat)

        drawFormat.Dispose()
        drawFont.Dispose()
        drawBrush.Dispose()
        formGraphics.Dispose()
    End Sub
End Class

Current Problem

When I start selecting buttons, the table layout panel expands to display more selections and the "Product #1" text starts to glitch. See below:

enter image description here

I tried to set the "Double Buffer" property to true and didn't the result. Any advice?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Alex
  • 4,821
  • 16
  • 65
  • 106

1 Answers1

2

You need to set ControlStyles.ResizeRedraw style for your control to indicate whether the control redraws itself when resized.

Also Instead of using CreateGraphics(), use the graphics object of OnPaint method and never dispose it, because it doesn't belong to you.

Public Sub New()
    ' If the base class is Control, comment the next line
    InitializeComponent()

    Me.SetStyle(ControlStyles.ResizeRedraw, True)
End Sub

Public Sub DrawVerticalString(ByVal e As PaintEventArgs)
    Dim formGraphics As System.Drawing.Graphics = e.Graphics
    '...
End Sub
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Alright I modified the necessary changes. I changed Me.CreateGraphics() to e.Graphics and removed the dispose calls. Although I still get the same issue. – Alex Jul 14 '16 at 17:33
  • What's the base class for `uProductName`. – Reza Aghaei Jul 14 '16 at 17:34
  • uProductName is a user control with nothing inside of it. I just use it to draw the string and then I insert uProductName into my other user control called uProductInformation – Alex Jul 14 '16 at 17:35
  • The base class is `Control` or `UserControl`? – Reza Aghaei Jul 14 '16 at 17:36
  • The constructor fix did it! Thanks! – Alex Jul 14 '16 at 17:47
  • You're welcome. Also don't forget *Instead of using CreateGraphics(), use the graphics object of OnPaint method and never dispose it, because it doesn't belong to you*. It's really important. It's the most obvious problem which one can see in a painting code in `OnPaint` method :) – Reza Aghaei Jul 14 '16 at 17:48