Click here to Skip to main content
15,917,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys !
let say we have a label on form for showing any notifications. i wish to place it at top center of form. and on notification the label should remain at exact center of form (left & right bounds, both grow or shrink on base of message length)
share your ideas please that what should i do with this label.
Posted
Comments
Sergey Alexandrovich Kryukov 3-Nov-11 0:33am    
Always tag your UI library when you use any; what is that: WPF, Forms, Silverlight, ASP.NET?
--SA

Use Dock[^] and TextAlign[^] property.
 
Share this answer
 
Comments
Zubair Alie 2-Nov-11 15:46pm    
Dock property set to (left,right,top) and TextAlign property is set to center but the textbox is growing from left to right. means the left side position of label remains static and do not grow or shrink
Sergey Alexandrovich Kryukov 3-Nov-11 0:45am    
No, you could not set this property to (left,right,top); probably you mixed it up with Anchor (try to avoid in in favor of Dock). Not to worry, I describe you a solution, tested.
--SA
Sergey Alexandrovich Kryukov 3-Nov-11 0:47am    
Sorry, I voted 3 for providing untested solution of this simple problem. I provided exact tested solution and explain why your advice does not work, please see.
--SA
Maciej Los 3-Nov-11 13:11pm    
I'm not angry or something like that ;) I understand and accept your opinion.
Zubair Alie 3-Nov-11 1:18am    
Sorry Iosmac, i had to write anchor but used dock instead. anyways i have achieved the solution.
thanks for your participation.
What iosmac advised cannot work, because by default AutoSize is true. Even when docking is correct, the label cannot take all dock area because of this property.

Do the following: set Dock to TopCenter, MiddleCenter or ButtomCenter, Dock to Top or Bottom and AutoSize to false. In this way, the label will stretch left-to-right to occupy full width of the parent control, and the text itself (not Label control) will be centered inside the Label control.

It works, tested.

—SA
 
Share this answer
 
v2
Comments
Zubair Alie 3-Nov-11 1:17am    
Issue resolved. i would like to add a bit to your solution. if i directly put the label on form, set dock to i.e. top and AutoSize to false.. it was not effective.
but when i put the panel on form, doc the panel to i.e TOP and then add label inside the panel and set following properties of label. then i achieved what we are discussing.
thanks for your answer. :)
Sergey Alexandrovich Kryukov 3-Nov-11 1:25am    
Sorry, you are right, but I did not even played with the idea of putting a label right in the form; I almost never do it. I said "parent control". It's possible though, in very simple forms, rarely makes sense. A good design use a lot of nested docked panels. I don't even use designer for anything else, add almost everything else in code.

You're welcome.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900