Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

center label in a form

Posted on 2006-10-23
4
Medium Priority
?
396 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
[X]
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
4 Comments
 
LVL 10

Assisted Solution

by:gangwisch
gangwisch earned 100 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 100 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

597 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