Home All Groups Group Topic Archive Search About

Dynamic Label Control Property in 2008

Author
31 Mar 2010 11:07 PM
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?

Author
1 Apr 2010 7:50 AM
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

Show quoteHide quote
"Rick" <R***@discussions.microsoft.com> wrote in message
news:AB013A8B-CAB1-4020-9C14-1F19573220E7@microsoft.com...
> 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?
Author
2 Apr 2010 8:07 PM
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


Show quoteHide quote
"Cor Ligthert[MVP]" wrote:

> 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
>
> "Rick" <R***@discussions.microsoft.com> wrote in message
> news:AB013A8B-CAB1-4020-9C14-1F19573220E7@microsoft.com...
> > 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?
>
> .
>
Author
6 Apr 2010 12:07 PM
Phill W.
On 01/04/2010 00:07, Rick wrote:
> 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.