Drawing Text in OnPaint problem

V

vijay

Hi

I am trying to create a class which acts a Bordered label control.
i have derived TextLabel class from forms.Control and i implemented
OnPaint function. i am enclosing the code.

I declared two variables in my form .

Dim txtLabel As New TextLabel
Dim txtLabel1 As New TextLabel

and form_load

txtLabel.Location = New System.Drawing.Point(10, 10)
txtLabel.Bounds = New Rectangle(10, 10, 100, 70)
txtLabel.Text = "Welcome to new text Label control" & vbCrLf &
"abc"
txtLabel.SetBorder = True
Controls.Add(txtLabel)

txtLabel1.Location = New System.Drawing.Point(10, 120)
txtLabel1.Bounds = New Rectangle(10, 120, 100, 70)
txtLabel1.Text = "rectangle test"
txtLabel1.SetBorder = True
Controls.Add(txtLabel1)


My Problem is
=========

txtLabel control is properly displayed with the text. but in the
txtLabel1 control the text is not displaying but the border is
displaying. i tried to add two more controls to the form. the problem
is repeated with the new controls also. only the first Control is
displaying text and border and all other controls are displaying only
Border not text.

where i am going wrong?.

thank you.

source code of TextLabel class.
=============
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms

Public Class TextLabel
Inherits System.Windows.Forms.Control

Private bDrawBorder As Boolean = False

Public Sub New()
InitializeComponent()
End Sub

'/ <summary>
'/ Clean up any resources being used.
'/ </summary>
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
MyBase.Dispose(disposing)
End Sub

'/ <summary>
'/ Required method for Designer support - do not modify
'/ the contents of this method with the code\editor.
'/ </summary>
Private Sub InitializeComponent()

End Sub


Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
'myLabelT = Me.Text
If Me.Text.Length > 0 Then
Dim size As SizeF = e.Graphics.MeasureString(Me.Text,
Me.Font)
Dim layoutRectangle As New RectangleF
layoutRectangle = Bounds()
Dim strFmt As New StringFormat
strFmt.Alignment = StringAlignment.Center
strFmt.FormatFlags = StringFormatFlags.NoClip
e.Graphics.DrawString(Me.Text, Me.Font, New
SolidBrush(Me.ForeColor), layoutRectangle, strFmt)
End If
If (bDrawBorder) Then
e.Graphics.DrawRectangle(New Pen(Color.Black), 0, 0,
Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)
End If

MyBase.OnPaint(e)
End Sub
Public Property SetBorder() As Boolean
Get
Return Me.bDrawBorder
End Get
Set(ByVal value As Boolean)
Me.bDrawBorder = value
End Set
End Property

End Class
 
A

Alex Feinman

The problem is in this line:
layoutRectangle = Bounds()

Bounds are in parent coordinates. I.e. for the second control Bounds would
be 10, 120, 100, 70. But the painting is done in control's client
coordinates. Change to
layoutRectangle = new RectangleF(0, 0, Width, Height)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top