How do I change the Color of the border of a Label created at run-time?

The code below is for the dynamic creation of a Label named Title, it sets properties as Font, ForeColor etc. related to the Label's Text.

 Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim Title As New Label
    Title.Text = "BROKERS"
    Title.Font = New Drawing.Font("Times New Roman", 30, FontStyle.Bold)
    Me.Controls.Add(Title)
    Title.ForeColor = Color.Red
    Title.Location = New Point((Me.Width / 2), (Me.Height / 2))
    Title.BackColor = Color.Black
    Title.Size = New System.Drawing.Size(220, 50)
    Title.BringToFront()
    Title.BorderStyle = BorderStyle.Fixed3D
    Me.Size = New System.Drawing.Size(900, 700)
End Sub

This section is supposed colour the border of the Label but it doesn't do so.

Private Sub Title_paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    Dim g As Graphics = e.Graphics
    Dim pen As New Pen(Color.Red, 4.0)
    For Each ctr As Control In Me.Controls
        If TypeOf ctr Is Label Then
            g.DrawRectangle(pen, New  _
                            Rectangle(ctr.Location, ctr.Size))
        End If
    Next
    pen.Dispose()
End Sub

1 answer

  • answered 2021-07-27 20:22 Jimi

    You should subscribe to the Paint event of your Label (or Labels, if you add more than one; use the same event handler for all).

    When the Paint event is raised, the sender object is the Control that raised the event, so you can cast sender to Control or Label (we just need the ClientRectangle property value, which belongs to the Control class, so you could use the same event handler for other Controls that are not Label; e.g., a Panel).

    Now, if you want your Label to auto-size to its Text content, you need to set AutoSize = True: the Label will auto-size when its Layout is performed (after you add it to a Container - the Form here).
    So, to show it in the middle of the Form, set its Location property after you have added it the Controls collection and you have changed the Form size.

    If you want to set a specific Size instead, don't set AutoSize = False, set the Size Property explicitly and the Location Property after you have changed the Form size (it appears you want to resize the Form at run-time).

    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MyBase.OnLoad(e)
        Dim lblTitle As New Label() With {
            .AutoSize = True,
            .BackColor = Color.Black,
            .BorderStyle = BorderStyle.None,
            .Font = New Font("Times New Roman", 30, FontStyle.Bold),
            .ForeColor = Color.Red,
            .Text = "BROKERS"
        }
    
        AddHandler lblTitle.Paint, AddressOf TitleLabel_Paint
    
        ClientSize = New Size(900, 700)
        Controls.Add(lblTitle)
        lblTitle.Location = New Point(ClientSize.Width - lblTitle.Width) \ 2, 
                                     (ClientSize.Height - lblTitle.Height) \ 2)
    End Sub
    
    Private Sub TitleLabel_Paint(sender As Object, e As PaintEventArgs)
        If sender Is Nothing Then Return
        Dim ctrl = DirectCast(sender, Control)
        Using pen As New Pen(Color.Red, 4)
            e.Graphics.DrawRectangle(pen, ctrl.ClientRectangle)
        End Using
    End Sub
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum