ProGrammer ProGrammer - 1 month ago 10
Vb.net Question

How to center label text when length varies in VB?

I have recently started a new project in Visual Basic which requires me to have a Preferences box where a user can enter his own text which will display in a label on the main form of the application, once the user has clicked the apply button in the Preferences box.

Whenever I test it out, the label and/or the writing in it shifts over to the right or the left...

I have tried to anchor it to the sides or top but it doesn't work.

How do I set the label up so that the text, no matter how short, always perfectly aligns to the top middle of the application even when the text is then changed to a new one of a different length?

Just like clicking the label and selecting 'Format', then 'Center in Form', then click on 'Horizontally' in the Menu of Visual Basic. But instead of aligning text of new length manually, I want it to automatically do this whenever the user clicks the apply button in the Preferences.

Any ideas will be greatly appreciated. Thanks

Answer

Another possible solution would be to perhaps use a Textbox and set it to Read-Only.

Why? Because the Textbox offers the TextAlign property: Public Property TextAlign As HorizontalAlignment

Ref: http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.textalign%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1