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
Enter your message below
Sign in or Join us (it's free).