Label control questions; avoiding cut-off text

C

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
 
C

Chris

C said:
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

Am I missing something or isn't it as simple as after you resize the
form to full screen, get the forms width and height then multiply by
your 40% and 70%?

chris
 
H

Herfried K. Wagner [MVP]

C R said:
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.

\\\
With Screen.PrimaryScreen.WorkingArea
Me.Size = New Size(CInt(.Width * 0.7), CInt(.Height * 0.7))
End With
///
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?


Check out the label control's 'AutoSize' property and controls' 'Dock' and
'Anchor' properties.
 
C

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
 
C

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
 
S

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
 
C

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.
 
S

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
 
S

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
 

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