DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Mon, 30 Jul 2007 19:46:23 -0700,    posted on: microsoft.public.dotnet.framework.compactframework        back       

Thread Index
  1    vijay
          2    vijay
          3    Alex Feinman
          4    vijay


Drawing Text in OnPaint problem   
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
Date:Mon, 30 Jul 2007 19:46:23 -0700   Author:  

Re: Drawing Text in OnPaint problem   
I am using VS2005 and .NET CF 2.0
Date:Mon, 30 Jul 2007 19:51:46 -0700   Author:  

Re: Drawing Text in OnPaint problem   
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)

"vijay"  wrote in message 
news:1185849983.286042.53080@g4g2000hsf.googlegroups.com...

> 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
> 
Date:Tue, 31 Jul 2007 22:04:38 +0800   Author:  

Re: Drawing Text in OnPaint problem   
it solved the problem.

Thank you Alex.
Date:Tue, 31 Jul 2007 18:52:11 -0700   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us