label: overriding a baseclass paint event


VB.NET Forum Moderator
Staff member
Jun 3, 2004
Lansing, MI; USA
Programming Experience
continuation from the topic titled "label forecolour"

i've started to make the usercontrol but i'm quickly realizing that i dont know what i'm doing.

what i'm trying to do is use the text property of the label (my control inherits the Label class) and the first part of whatever is in the text property is in one color and the rest is in another color (i got the two colors part working) but what i need is for it to not call the MyBase.Paint and only call mine

at one point i had both displaying right now only the baseclass gets display
this is what i have thusfar:
Option Explicit On 
Option Strict On

Public Class NewLabel
	Inherits System.Windows.Forms.Label

#Region " Windows Form Designer generated code "

	Public Sub New()

		'This call is required by the Windows Form Designer.

		'Add any initialization after the InitializeComponent() call

	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
			End If
		End If
	End Sub

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

	'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()
		components = New System.ComponentModel.Container
	End Sub

#End Region

	Private clrForeColor2 As Color = Color.Blue
	Private intForeColor1Length As Integer = 1
	Private Shadows Event Paint As PaintEventHandler

	Public Property ForeColor2() As Color
			Return clrForeColor2
		End Get
		Set(ByVal Value As Color)
			clrForeColor2 = Value
			'RaiseEvent me.Paint()
		End Set
	End Property

	Public Property ForeColor1Length() As Integer
			Return intForeColor1Length
		End Get
		Set(ByVal Value As Integer)
			intForeColor1Length = Value
		End Set
	End Property

	Private Sub MyPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
		' Create font and brush.
		Dim Font As Font = Me.Font '("Arial", 10)
		Dim ForeColor1 As New SolidBrush(Me.ForeColor)
		Dim ForeColor2 As New SolidBrush(Me.ForeColor2)
		Dim strForeColor1 As String
		Dim strForeColor2 As String
		Dim intCounter As Integer
		For intCounter = 1 To Me.ForeColor1Length
			strForeColor1 &= Mid(Me.Text, intCounter, 1)
		Next intCounter
		For intCounter = (Me.ForeColor1Length + 1) To Len(Me.Text)
			strForeColor2 &= Mid(Me.Text, intCounter, 1)
		' Create rectangle for drawing.
		Dim x As Single = 1.0F
		Dim y As Single = 2.0F
		Dim MyWidth As Single = e.Graphics.MeasureString(strForeColor1, Font).Width
		'Dim height As Single = Me.Height
		Dim drawRect As New RectangleF(x, y, Width, Height)
		'Dim blackPen As New Pen(Color.Black)
		e.Graphics.DrawString(strForeColor1, Font, ForeColor1, drawRect)
		e.Graphics.DrawString(strForeColor2, Font, ForeColor2, MyWidth - 4, y)
	End Sub
End Class
What you need to do is OverRide the OnPaint method so that only your custom method will be executed and not the baseClass'.
To do that, define the OnPaint method like this:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    'your code
End Sub
You can remove the 'Private Shadows Event Paint As PaintEventHandler' line.

To have the IDE generate an OverRidden method or property signature for a class while in code view, choose (OverRides) in the leftHand ComboBox, then the Method or Property in the RightHand comboBox.