Dynamic Label Control Property in 2008

R

Rick

I am dynamically creating a label control of width=100 and adding text
(sTextVar) to it dynamically whose string length is unknown:

Dim myLabel As Label = New Label()
myLabel.Width = 100
myLabel.Text = sTextVar
Me.Controls.Add(myLabel)

1. How do I set myLabel so the font size of sTextVar is shrunk to smaller
size?
2. How do I set myLabel so sTextVar is displayed in multiple lines?
 
C

Cor Ligthert[MVP]

Hi Rick,

Try to avoid the prefixes with data types, it looks so awful from the
previous millennium when we did not have intelligence

\\\
Dim myLabel As New Label With {.Size = New Size(70, 100), .Location = New
Point(10, 10), _
.Font = New Font("Arial Narrow", 8.25,
FontStyle.Regular), _
.Text = MyText}
Me.Controls.Add(myLabel)
///

Success

Cor
 
R

Rick

Cor,
This is not what I am looking for. I need to be able to adjust the font
size dynamically based on the text size of the label. With your example; if I
set the size to 100 and the label contains a text of 500 (for example) then I
will be able to see only what is going to fir with the textbox of sized 100.

BTW,
I like your suggestion for not using the prefixes with data type. thanks
 
P

Phill W.

I am dynamically creating a label control of width=100 and adding text
(sTextVar) to it dynamically whose string length is unknown:

Dim myLabel As Label = New Label()
myLabel.Width = 100
myLabel.Text = sTextVar
Me.Controls.Add(myLabel)

1. How do I set myLabel so the font size of sTextVar is shrunk to smaller
size?
2. How do I set myLabel so sTextVar is displayed in multiple lines?

Extend the Label class (i.e. create one that inherits from Label) and
use that in place of the standard one.

Override the Text property and, in the set routine, you can do whatever
adjustments you like to the Font, etc.

Class CustomLabel
Inherits Label

Public Sub New()
Me.AutoSize = False
Me.Size = New Size( 100, 24 )
End Sub

Public Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal value As String)
MyBase.Text = value
' Adjust other properties as required
End Set
End Property
End Class

Labels implicitly display on multiple lines - you just have to worry
about making your control tall enough for the text to fit.

There are plenty of ways to kludge the sizing, to do the job "properly",
read up on Graphics.MeasureString().

HTH,
Phill W.
 

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