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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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