clickable label

  • 12 years ago

    Hi guys,

    I am working on VB .NET CF application. In it I would like to show some text in labels, and when  clicked on them the alignment, text and backcolor would change.

    But because the label in compact framework does not expose on click event...I tried to create my own control from the code I found on opennetcf forum... but it doesn't work completely.

    the click event works fine...but when i try to change the text after i click the label nothing happens??

    If anyone has any idea what is wrong I would really appreciate help.

    Thanks 

    Grega 

     

    Here is the code of the control 

     Imports System
    Imports System.Drawing
    Imports System.Windows.Forms


    Public Class Label
        Inherits Control
        'Implements Label as I expect it to be (more or less...)
        Private m_borderStyle As BorderStyle = BorderStyle.None
        Private m_textAlignment As ContentAlignment = ContentAlignment.TopLeft
        ' Accessing the font object of the Base Class throws a nasty exception!
        ' So, I am using my own font.
        Private m_fontName As String = "Tahoma"
        Private m_fontSize As Integer = 10
        Private m_fontStyle As FontStyle = FontStyle.Regular

        Public Event labelClick As EventHandler
        Public Event labelPaint As PaintEventHandler

        Public Sub New()
        End Sub

        Public Property BorderStyle() As BorderStyle
            Get
                Return Me.m_borderStyle
            End Get
            Set(ByVal value As BorderStyle)
                Me.m_borderStyle = value
            End Set
        End Property

        Public Property FontName() As String
            Get
                Return Me.m_fontName
            End Get
            Set(ByVal value As String)
                Me.m_fontName = value
            End Set
        End Property

        Public Property FontSize() As Integer
            Get
                Return Me.m_fontSize
            End Get
            Set(ByVal value As Integer)
                Me.m_fontSize = value
            End Set
        End Property

        Public Property FontStyle() As FontStyle
            Get
                Return Me.m_fontStyle
            End Get
            Set(ByVal value As FontStyle)
                Me.m_fontStyle = value
            End Set
        End Property

        Public Property TextAlignment() As ContentAlignment
            Get
                Return Me.m_textAlignment
            End Get
            Set(ByVal value As ContentAlignment)
                Me.m_textAlignment = value
            End Set
        End Property

        Protected Overloads Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
            Dim graphics As Graphics = pe.Graphics
            Dim pen As New Pen(Color.Black)
            Dim brush As New SolidBrush(ForeColor)
            Dim font As New Font(Me.m_fontName, Me.m_fontSize, Me.m_fontStyle)
            Dim s As SizeF

            If Me.BorderStyle = BorderStyle.FixedSingle Then
                graphics.DrawRectangle(pen, 0, 0, Me.Width - 1, Me.Height - 1)
            ElseIf Me.BorderStyle = BorderStyle.Fixed3D Then
                graphics.DrawRectangle(pen, Me.ClientRectangle)
            End If

            s = graphics.MeasureString(Me.Text, font)
            Select Case Me.m_textAlignment
                Case ContentAlignment.TopCenter
                    graphics.DrawString(Me.Text, font, brush, (Me.Width - s.Width) / 2, (Me.Height - s.Height) / 2)
                    Exit Select
                Case ContentAlignment.TopLeft
                    graphics.DrawString(Me.Text, font, brush, 2, (Me.Height - s.Height) / 2)
                    Exit Select
                Case ContentAlignment.TopRight
                    graphics.DrawString(Me.Text, font, brush, Me.Width - s.Width - 2, (Me.Height - s.Height) / 2)
                    Exit Select
            End Select

            pen.Dispose()
            brush.Dispose()
            font.Dispose()
            RaiseEvent labelPaint(Me, pe)


            MyBase.OnPaint(pe)
        End Sub

        Protected Overloads Overrides Sub OnClick(ByVal e As System.EventArgs)
            RaiseEvent labelClick(Me, e)

            MyBase.OnClick(e)
        End Sub
    End Class

     



  • 12 years ago

    Thanks for reply, but I already figured out why pasted code didn't work. I simply forgot to refresh the control, after I changed the text. So it works perfectly for what I need now. BR Grega

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++: an octopus made by nailing extra legs onto a dog.” - Steve Taylor