center label in a form

Posted on 2006-10-23
Last Modified: 2010-04-23
I know how to center a label on my form for a fixed Length, but I have a variable that will change in length.  I would like it to display in the center regardless of the length.  How would I do that?  Thanks, Chad
Question by:chadmanvb
  • 2
LVL 10

Assisted Solution

gangwisch earned 25 total points
ID: 17792634
what you need to do to the label is put autosize=true then
dim y as integer=form1.height/2
dim x as integer=form1.width/2 - label1.width/2
label1.location=new point(x,y)

Author Comment

ID: 17792748
I just tried this, but it does not work
dim x as integer=form1.width/2 - label1.width/2
label1.location=new point(x,y)

 If I do it like:

dim x as integer=485/2 - label1.width/2
label1.location=new point(x,205)

This works, but would get messed up if I change the form size.

It does not like the "form1.width"?  It recomended I try using me instead, but could not get that to work either.  Am I missing something?  Not a big deal because this will also work.

Thanks, Chad
LVL 10

Accepted Solution

Kinger247 earned 25 total points
ID: 17794433
chadmanvb: Try this ...

Public Class Form1

    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        Label1.Text &= "X"
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    End Sub

    '//-- Change location when text changes
    Private Sub Label1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.TextChanged
    End Sub

    '//-- Centre the text
    Private Sub CentreLabel()
        Label1.Location = New Point(Me.Width / 2 - (Label1.Width / 2), Me.Height / 2 - (Label1.Height / 2))
    End Sub
End Class

Author Comment

ID: 17800283
Thanks worked great!  Chad

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Allow user to hide and show datagridview columns 4 25
C# Single Form 8 42
Modal Popup Extender control 1 20 1 month apart 11 29
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question