Solved

center label in a form

Posted on 2006-10-23
4
371 Views
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
0
Comment
Question by:chadmanvb
  • 2
4 Comments
 
LVL 10

Assisted Solution

by:gangwisch
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)
0
 

Author Comment

by:chadmanvb
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
0
 
LVL 10

Accepted Solution

by:
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
        CentreLabel()
    End Sub

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

    '//-- Change location when text changes
    Private Sub Label1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.TextChanged
        CentreLabel()
    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
0
 

Author Comment

by:chadmanvb
ID: 17800283
Thanks worked great!  Chad
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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