none
Center label text in a panel RRS feed

  • Question

  • Hi all

    I have a Label in a Panel

    The label.text change runtime ( may be longer or shorter)

    I would like to have the  label.text always orizontally centered in the panel

    How to do that ?

    Monday, January 15, 2018 11:54 AM

Answers

  • You could probably just use a TableLayoutPanel.  Anchor the panel in the center of the form, set it to 1 row and 1 column, put your label in the cell and set the anchor on the label to None.  With that done the label will "float" in the center of the panel.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Claudio111 Monday, January 15, 2018 6:12 PM
    Monday, January 15, 2018 2:41 PM
    Moderator
  • Label1.width takes the original Label1. width not the one after setting the new label1.text

    Hi

    Not here it doesn't! See screenshots below - one of Designer view, second of running app.

    Please show the code you are using that DOESN'T do similar.

    using this code

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	With Label1
    	  .Text = "Something to say"
    	  .Location = New Point(Panel1.Width \ 2 - Label1.Width \ 2, Label1.Location.Y)
    	  .BackColor = Color.PaleGoldenrod
    	End With
    	With Label2
    	  .Text = "Something to say here have you,"
    	  .Location = New Point(Panel1.Width \ 2 - Label2.Width \ 2, Label2.Location.Y)
    	  .BackColor = Color.PaleGreen
    	End With
    	With Label3
    	  .Text = "   Something to say here have you, well then  ....   "
    	  .Location = New Point(Panel1.Width \ 2 - Label3.Width \ 2, Label3.Location.Y)
    	  .BackColor = Color.PaleTurquoise
    	End With
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Claudio111 Monday, January 15, 2018 6:11 PM
    Monday, January 15, 2018 3:08 PM
  •  Being that the TextChanged event seems to get raised before the width of the label changes and that wont work,  you can create your own small sub to set the label text and call that when you want to change the text and have it centered after it is set.

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Label1.TextAlign = ContentAlignment.MiddleCenter
            SetLabelText("Default Text...")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            SetLabelText("Some longer text in the label")
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            SetLabelText("Some short text")
        End Sub
    
        Private Sub SetLabelText(txt As String)
            Label1.Text = txt
            Label1.Left = (Panel1.ClientSize.Width \ 2) - (Label1.Width \ 2)
            Label1.Top = (Panel1.ClientSize.Height \ 2) - (Label1.Height \ 2)
        End Sub
    End Class
     

     

     

     However,  if the size of the Panel changes at run-time too,  then you would need to call the sub in the Panel Resize event also...

        Private Sub Panel1_Resize(sender As Object, e As EventArgs) Handles Panel1.Resize
            SetLabelText(Label1.Text)
        End Sub
      

     

     

     It might be better to do what Reed said though,  i did not try that.


    If you say it can`t be done then i`ll try it


    • Edited by IronRazerz Monday, January 15, 2018 3:30 PM
    • Marked as answer by Claudio111 Monday, January 15, 2018 6:11 PM
    Monday, January 15, 2018 3:24 PM

All replies

  • Hi

    After setting the Text property of the Label to whatever text you want, then set the Label Location to New Point(Panel1.Width\2 - Label1.width\2,Label1.Location.Y)

    For example:

    With Label1
       .TextAlign = ContentAlignment.MiddleCenter
       .Text = "Something to say here have you, well then  ...."
       .Location = New Point(Panel1.Width \ 2 - Label1.Width \ 2, Label1.Location.Y)
    End With
    alternatively, you can set the Label to occupy the entire width of the Panel in the Designer and set the .TextAlign as needed.


    Regards Les, Livingston, Scotland



    • Proposed as answer by Cor Ligthert Monday, January 15, 2018 12:59 PM
    • Edited by leshay Monday, January 15, 2018 1:03 PM
    Monday, January 15, 2018 12:57 PM
  •  Please don't forget to mark leshay's post as the answer if it solves your problem.  It would be nice if you went back and marked the correct post as the answer in Your Question About The ToolStrip Focus Problem too.  Thanks.


    If you say it can`t be done then i`ll try it

    Monday, January 15, 2018 2:02 PM
  • Label1.width takes the original Label1. width not the one after setting the new label1.text
    Monday, January 15, 2018 2:14 PM
  • You could probably just use a TableLayoutPanel.  Anchor the panel in the center of the form, set it to 1 row and 1 column, put your label in the cell and set the anchor on the label to None.  With that done the label will "float" in the center of the panel.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Claudio111 Monday, January 15, 2018 6:12 PM
    Monday, January 15, 2018 2:41 PM
    Moderator
  • Label1.width takes the original Label1. width not the one after setting the new label1.text

    Hi

    Not here it doesn't! See screenshots below - one of Designer view, second of running app.

    Please show the code you are using that DOESN'T do similar.

    using this code

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	With Label1
    	  .Text = "Something to say"
    	  .Location = New Point(Panel1.Width \ 2 - Label1.Width \ 2, Label1.Location.Y)
    	  .BackColor = Color.PaleGoldenrod
    	End With
    	With Label2
    	  .Text = "Something to say here have you,"
    	  .Location = New Point(Panel1.Width \ 2 - Label2.Width \ 2, Label2.Location.Y)
    	  .BackColor = Color.PaleGreen
    	End With
    	With Label3
    	  .Text = "   Something to say here have you, well then  ....   "
    	  .Location = New Point(Panel1.Width \ 2 - Label3.Width \ 2, Label3.Location.Y)
    	  .BackColor = Color.PaleTurquoise
    	End With
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Claudio111 Monday, January 15, 2018 6:11 PM
    Monday, January 15, 2018 3:08 PM
  •  Being that the TextChanged event seems to get raised before the width of the label changes and that wont work,  you can create your own small sub to set the label text and call that when you want to change the text and have it centered after it is set.

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Label1.TextAlign = ContentAlignment.MiddleCenter
            SetLabelText("Default Text...")
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            SetLabelText("Some longer text in the label")
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            SetLabelText("Some short text")
        End Sub
    
        Private Sub SetLabelText(txt As String)
            Label1.Text = txt
            Label1.Left = (Panel1.ClientSize.Width \ 2) - (Label1.Width \ 2)
            Label1.Top = (Panel1.ClientSize.Height \ 2) - (Label1.Height \ 2)
        End Sub
    End Class
     

     

     

     However,  if the size of the Panel changes at run-time too,  then you would need to call the sub in the Panel Resize event also...

        Private Sub Panel1_Resize(sender As Object, e As EventArgs) Handles Panel1.Resize
            SetLabelText(Label1.Text)
        End Sub
      

     

     

     It might be better to do what Reed said though,  i did not try that.


    If you say it can`t be done then i`ll try it


    • Edited by IronRazerz Monday, January 15, 2018 3:30 PM
    • Marked as answer by Claudio111 Monday, January 15, 2018 6:11 PM
    Monday, January 15, 2018 3:24 PM
  • .... 

     It might be better to do what Reed said though,  i did not try that.


    If you say it can`t be done then i`ll try it


    Truly, all UI elements should be placed in a TableLayoutPanel or FlowLayoutPanel, as most appropriate for the UI element in question, and all positioning of controls should be done through anchoring and docking.

    The reason for this?

    It is the best/easiest (and only supported) way to handle the DPI scaling issue.  If you don't use nested layout panels then you run a high risk of your UI being rendered incorrectly in high-res scenarios, often leading to controls being placed partially outside of the client bounds of the form, or shrunken to where you can't read their text (e.g. poorly auto-sized buttons). 


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, January 15, 2018 5:56 PM
    Moderator
  • It is the best/easiest (and only supported) way to handle the DPI scaling issue.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

     Reed,  is that only true in the case that you do not set the application to be DpiAware in the app.manifest?  I never cared for working with TableLayoutPanels because,  from my experience with them,  you can not always get your controls to lay out how you want them arranged on the form.

       I have gotten use to setting my applications to be DpiAware since i started using VS2015 where all you need to do is un-comment a few lines in the app.manifest to do it.


    If you say it can`t be done then i`ll try it

    Monday, January 15, 2018 6:32 PM
  • It is the best/easiest (and only supported) way to handle the DPI scaling issue.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

     Reed,  is that only true in the case that you do not set the application to be DpiAware in the app.manifest?  I never cared for working with TableLayoutPanels because,  from my experience with them,  you can not always get your controls to lay out how you want them arranged on the form.

       I have gotten use to setting my applications to be DpiAware since i started using VS2015 where all you need to do is un-comment a few lines in the app.manifest to do it.


    If you say it can`t be done then i`ll try it

    No, the layout issues can still exist even with that setting in place, particularly when the Form autoscale mode is set to Font.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, January 15, 2018 9:13 PM
    Moderator