Label in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes the Label control


Label

The Label control displays a simple piece of text that the user can view but not select or modify, and because you cannot select the text, you cannot copy it to the clipboard as well. If the text contains a value that you think the user might want to copy and paste into another application e.g. serial or email, you should use a TextBox control with ReadOnly set to True.

Unlike some controls that are not very useful if you can't respont to their events, a Label just displays a message and you don't need to catch its events.

Usually you use the Label to tell the user that something has changed or happened. For that you change its Text property:

    Label1.Text = "Please save changes, before exit."


Note: To make the Label control resizes itself to fits its text, set the Label contros AutoSize property to True (the default), but you should not use AutoSize with multiline text (especially if the text contains carriage returns)

    Dim dynamiclabel As New Label
    dynamiclabel.AutoSize = True
    dynamiclabel.Name = "DynamicLabel"
    dynamiclabel.Text = "I am dynamically added label control"
    dynamiclabel.Font = New Font(Label1.Font, FontStyle.Bold)
    dynamiclabel.Location = New Point(Label1.Location.X, Label1.Location.Y + 30)
    Me.Controls.Add(dynamiclabel)


dynamic label

The Label control can also display an image. The following example demonstrates how do you set the Image, ImageAlign and TextAlign properties to assing and determine where the image and text are positioned inside the control.

    Dim dynamiclabel As New Label
    dynamiclabel.AutoSize = True
    dynamiclabel.Name = "DynamicLabel"
    dynamiclabel.Text = "I am dynamically added label control"
    dynamiclabel.Font = New Font(Label1.Font, FontStyle.Bold)
    dynamiclabel.Location = New Point(Label1.Location.X, Label1.Location.Y + 30)
    Me.Controls.Add(dynamiclabel)

    Dim labelwithimage As New Label
    labelwithimage.AutoSize = False
    labelwithimage.Image = My.Resources.text
    labelwithimage.ImageAlign = ContentAlignment.MiddleLeft
    labelwithimage.TextAlign = ContentAlignment.MiddleRight
    labelwithimage.Name = "LabelWithImage"
    labelwithimage.Text = "I am a label control with image"
    labelwithimage.Font = New Font(Label1.Font, FontStyle.Bold)
    labelwithimage.Location = New Point(Label1.Location.X, Label1.Location.Y + 60)
    labelwithimage.Height = 64
    labelwithimage.Width = 240
    Me.Controls.Add(labelwithimage)


image example

Previous Lesson | Next Lesson


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.