Home All Groups Group Topic Archive Search About

Label control questions; avoiding cut-off text

Author
28 Dec 2005 6:11 PM
C R
I have a label inside of a form. The form is maximized to fill the
entire screen. I would like the label to take up 40% of the width of the
screen, and 70% of the height, regardless of monitor size or resolution.
How can I do this?

Also, if I use a certain font & font size, how can I avoid getting any
half-lines of text, where you just see the cut-off tops or bottoms of
the letters?

Thanks for any help!

CR



*** Sent via Developersdex http://www.developersdex.com ***

Author
28 Dec 2005 7:46 PM
Colin Neller
In order to have full control of the text being displayed, you're going
to have to do custom drawing on a panel, usercontrol, or form.  Below
is an example of doing custom drawing on a form meeting your
requirements.

Regards,

Colin Neller
www.colinneller.com/blog

----

Public Class Form1

    Private s As String

    Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To 1000
            sb.Append("This text repeats a lot.  ")
        Next
        s = sb.ToString

        With Me
            .SetStyle(ControlStyles.AllPaintingInWmPaint, True)
            .SetStyle(ControlStyles.ResizeRedraw, True)
            .SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        End With
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim w As Single = CSng(Me.Width * 0.4)
        Dim h As Single = CSng(Me.Height * 0.7)

        Using format As New StringFormat, _
            brush As New SolidBrush(Me.ForeColor)

            With format
                .Alignment = StringAlignment.Far
                .FormatFlags = StringFormatFlags.LineLimit
                .LineAlignment = StringAlignment.Center
                .Trimming = StringTrimming.EllipsisWord
            End With

            Dim r As New RectangleF(0, 0, w, h)

            With e.Graphics
                .DrawString(s, Me.Font, brush, r, format)
            End With
        End Using
    End Sub

End Class
Author
4 Jan 2006 4:29 PM
C R
Colin,

Thanks so much for your help!

DrawString was just what I needed to get the level of control I want.

C R




*** Sent via Developersdex http://www.developersdex.com ***
Author
4 Jan 2006 10:16 PM
Shishu Das
Colin,

When I do the DrawStrings in the form's Load event, they work, but then
the form gets blanked out. Subsequent Drawstrings based on mouse events
work fine. Any idea why, and how to get around this?

shishudas




*** Sent via Developersdex http://www.developersdex.com ***
Author
4 Jan 2006 10:32 PM
Colin Neller
Sure!  As in my example, put any code that is used to paint in the "Paint"
event of the control that you are painting onto.  No painting should occur
in the "Load" event.  The paint event is called every time windows notifies
your form that it needs to repaint its self - just what you need.

Show quoteHide quote
"Shishu Das" <shishud***@gmail.com> wrote in message
news:%23fnD1xXEGHA.2436@TK2MSFTNGP15.phx.gbl...
>
> Colin,
>
> When I do the DrawStrings in the form's Load event, they work, but then
> the form gets blanked out. Subsequent Drawstrings based on mouse events
> work fine. Any idea why, and how to get around this?
>
> shishudas
>
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
5 Jan 2006 6:38 PM
Shishu Das
Well, when I put the Drawstrings in Paint, they work, but then the form
gets blanked out (same as happens in Load). I can't figure out why.
Perhaps it does my Drawstrings, but then "re-paints" the blank form. Is
there an After-Paint event or something like that? This is a bit
confusing...

shishudas



*** Sent via Developersdex http://www.developersdex.com ***
Author
5 Jan 2006 7:19 PM
Colin Neller
Post a simple example of what you are trying to do and I'll have a look.

Show quoteHide quote
"Shishu Das" <shishud***@gmail.com> wrote in message
news:uWeCCdiEGHA.3856@TK2MSFTNGP12.phx.gbl...
>
> Well, when I put the Drawstrings in Paint, they work, but then the form
> gets blanked out (same as happens in Load). I can't figure out why.
> Perhaps it does my Drawstrings, but then "re-paints" the blank form. Is
> there an After-Paint event or something like that? This is a bit
> confusing...
>
> shishudas
Author
5 Jan 2006 8:59 PM
Shishu Das
Ah, I solved it in the process of creating a simple example. The culprit
was:

Me.SetStyle(ControlStyles.DoubleBuffer, True)

Maybe you can explain why...

shishudas




*** Sent via Developersdex http://www.developersdex.com ***