Hi
I need help to center a label with a form.
I have tried with the following code but was unsuccessful.

Me.Width = 135
        Me.Height = 38
        Dim w As Integer = Me.Width / 2
        Dim h As Integer = Me.Height / 2
        Dim lblw As Integer = Me.lblnumber.Width / 2
        Dim lblh As Integer = lblnumber.Height / 2
        Dim ttw As Integer = w - lblw
        Dim tth As Integer = h - lblh
         lblnumber.Location = New Point(ttw, tth)

Recommended Answers

All 8 Replies

Hi, To make a label in centre of form you just write..

1. Make sure the anchor property of label is Top only.
2.write this code on load of form.

label1.Left = (me.ClientSize.Width / 2) - (label1.Width / 2)

Note: Label will adjust its alignment on resizing of form also.

Happy Coding :-)

First set Anchor, of the label from properties window, to None.

Then replace your code with this NEW code.

lblnumber.Top = Me.Height/2
lblnumber.Left = Me.Width/2

Your welcome

Try it,

Label1.SetBounds((Me.ClientSize.Width - Label1.Width) / 2, (Me.ClientSize.Height - Label1.Height) / 2, 0, 0, BoundsSpecified.Location)

First set Anchor, of the label from properties window, to None.

Then replace your code with this NEW code.

lblnumber.Top = Me.Height/2
lblnumber.Left = Me.Width/2

Your welcome

Since I can't edit my own code.

lblnumber.Top = (Me.Height / 2) - (Button3.Height / 2)
lblnumber.Left =(Me.Width / 2) - (Button3.Width / 2)

Anchor properties no longer matter

Thank you all for your replies.
I have tried all but still not successful.
I have tried all the codes with Anchor set to None.
Here is what I have in the Sub, And in front it shows the where the label is being placed. Hope some one can help me figure this out with the following code.

Private Sub Localidade()
        lblnumber.TextAlign = ContentAlignment.MiddleCenter
        Dim x As Integer = (DsgnForm.Width) + 300
        Dim y As Integer = (DsgnForm.Height) + 300
        Me.Location = New Point(x, y)

        lblnumber.SetBounds((Me.ClientSize.Width - lblnumber.Width) / 2, _
                            (Me.ClientSize.Height - lblnumber.Height) / 2, 0, 0, BoundsSpecified.Location) ' (CENTER RIGHT)

        'lblnumber.Top = Me.Height / 2 
        'lblnumber.Left = Me.Width / 2 (BOTTOM RIGHT)

        ' lblnumber.Left = (Me.ClientSize.Width / 2) - (lblnumber.Width / 2) (BOTTOM RIGHT)

        ' lblnumber.Top = (Me.Height / 2) - (Me.Height / 2) 
        ' lblnumber.Left = (Me.Width / 2) - (Me.Width / 2) (TOP LEFT)
    End Sub

Replace Button with lblnumber

lblnumber.Top = (Me.Height / 2) - (lblnumber.Height / 2)
lblnumber.Left =(Me.Width / 2) - (lblnumber.Width / 2)

Thanks Yorro your code puts the label in the Center Left.

This is what I'm trying to do I have a Dialog showing the an Order Number,
I'm also using the Opacity to show and Fade the dialog as Outlook displays the warnings.

Tank you

Thank you all for all of your effort.
your code worked with a text box but not with a Label.
So I changed my label to text box .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.