Click here to Skip to main content
15,885,824 members
Articles / Mobile Apps
Article

Vertical Label Control in VB.NET

Rate me:
Please Sign up or sign in to vote.
2.95/5 (18 votes)
3 Sep 2005CPOL 124K   35   14
Create a custom vertical label user control in VB.NET.

Vertical Label

Introduction

This article describes how to create a custom vertical label user control in VB.NET.

Working

Create a new Windows user control. Change the inherited class from System.Windows.Forms.UserControl to System.Windows.Forms.Control. Add an icon file in the project to associate with the control and set its property to "EmbeddedResource" and add a class attribute <ToolboxBitmap(GetType(Name of Class), "Name of Icon file"). Delete:

VB
components = New System.ComponentModel.Container()

line in the Windows designer generated code and add the default size of the label. For example:

VB
Me.Size = New System.Drawing.Size(24, 100)

Override the OnPaint method and the Text property of the control. Build the control and add it to your toolbox.

Source Code

VB
Imports System.ComponentModel

<ToolboxBitmap(GetType(VerticalLabel), "Varticallabel.ico")> _
Public Class VerticalLabel

    'Since we are not using the additional resources/capabilities of 
    'UserControl we will inherit from Control instead to save overhead
    'Inherits System.Windows.Forms.UserControl
    Inherits System.Windows.Forms.Control

    Private labelText As String
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        'This call is required by the Windows Form Designer.
        InitializeComponent()
    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        '
        'VerticalLabel
        '
        Me.Size = New System.Drawing.Size(24, 100)
    End Sub
#End Region

    Protected Overrides Sub OnPaint(ByVal e As _
              System.Windows.Forms.PaintEventArgs)
        Dim sngControlWidth As Single
        Dim sngControlHeight As Single
        Dim sngTransformX As Single
        Dim sngTransformY As Single
        Dim labelColor As Color
        Dim labelBorderPen As New Pen(labelColor, 0)
        Dim labelBackColorBrush As New SolidBrush(labelColor)
        Dim labelForeColorBrush As New SolidBrush(MyBase.ForeColor)
        MyBase.OnPaint(e)
        sngControlWidth = Me.Size.Width
        sngControlHeight = Me.Size.Height
        e.Graphics.DrawRectangle(labelBorderPen, 0, 0, _
                   sngControlWidth, sngControlHeight)
        e.Graphics.FillRectangle(labelBackColorBrush, 0, _
                   0, sngControlWidth, sngControlHeight)
        ' set the translation point for the 
        ' graphics object - the new (0,0) location
        sngTransformX = 0
        sngTransformY = sngControlHeight
        ' translate the origin used for rotation and drawing 
        e.Graphics.TranslateTransform(sngTransformX, _
                            sngTransformY) ' (0, textwidth)
        'set the rotation angle for vertical text
        e.Graphics.RotateTransform(270)
        ' draw the text on the control
        e.Graphics.DrawString(labelText, Font, _
                   labelForeColorBrush, 0, 0)
    End Sub

    Private Sub VTextBox_Resize(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles MyBase.Resize
        Invalidate()
    End Sub

    <Category("Verticallabel"), _
    Description("Text is displayed vertiaclly in container")> _
    Public Overrides Property Text() As String
        Get
            Return labelText
        End Get
        Set(ByVal Value As String)
            labelText = Value
            Invalidate()
        End Set
    End Property
End Class

License

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


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalinherits from a label [modified] Pin
scalpa9828-Oct-08 3:22
scalpa9828-Oct-08 3:22 
GeneralNeed properties for this code Pin
zephiruspt15-Sep-08 1:10
zephiruspt15-Sep-08 1:10 
GeneralRe: Need properties for this code Pin
Raman Tayal15-Sep-08 6:44
Raman Tayal15-Sep-08 6:44 
GeneralRe: Need properties for this code Pin
zephiruspt15-Sep-08 7:29
zephiruspt15-Sep-08 7:29 
GeneralRe: Need properties for this code Pin
pablleaf21-Apr-09 12:09
pablleaf21-Apr-09 12:09 
GeneralThanks for the control Pin
Shane Story19-Aug-08 5:10
Shane Story19-Aug-08 5:10 
GeneralRe: Thanks for the control Pin
Jalapeno Bob27-May-11 7:10
professionalJalapeno Bob27-May-11 7:10 
QuestionTransparent Background Pin
SourSW2-Jun-06 11:49
SourSW2-Jun-06 11:49 
AnswerRe: Transparent Background Pin
pablleaf24-Apr-09 8:15
pablleaf24-Apr-09 8:15 
SourSW,

I had the same problem with this control and the answer is actually simple!

Just put this in the Sub New method
<br />
Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)<br />

GeneralThank you Pin
nonanon14-Oct-05 12:27
nonanon14-Oct-05 12:27 
General&quot;Article&quot; is not the same as &quot;sourcecode&quot; Pin
mav.northwind4-Sep-05 0:43
mav.northwind4-Sep-05 0:43 
GeneralRe: &quot;Article&quot; is not the same as &quot;sourcecode&quot; Pin
Raman Tayal4-Sep-05 2:11
Raman Tayal4-Sep-05 2:11 
GeneralRe: &quot;Article&quot; is not the same as &quot;sourcecode&quot; Pin
Luca Crisi, MCP12-Mar-07 8:33
Luca Crisi, MCP12-Mar-07 8:33 
GeneralRe: &quot;Article&quot; is not the same as &quot;sourcecode&quot; Pin
mav.northwind12-Mar-07 9:36
mav.northwind12-Mar-07 9:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.