Resizing labels in Winforms to Left
I have 15 to 20 labe开发者_JS百科ls with variable text sizes and text boxes arranged in the forms. Text boxes are arranged next to labels. The font and color of the form and hence the form controls can be configured by the user at run time. When I right align the labels and set auto grow property to true and whenever the font style changes (say from Arial to Georgia) the right aligned labels are no more right aligned.
I need a solution on labels (for winforms) to automatically grow to the left when the font size changes.
Set your label properties as folowing;
AutoSize = false;
TextAlign = TopRight (or anything to right)
Then manually resize your label to a maximum area to fit your longest text. That worked for me.
Set the anchor to "Right" rather than "Left" (you will probably also have "Top" in which case it's "Right Top" rather than "Left Top"), it should grow in the right (left) direction I believe. Been a while since I did any of this so try it and let me know how it goes.
You could probably use a TableLayoutPanel
.
I had the same problem. My fix was to create a simple function to move the label, which I called after any event or code that changed the label size.
Enter the name of the label you want to grow left, and the X coordinate of the right end of the label. Then you call this function after any changes to the label.
private void repositionLabel(Label lab, int endPoint)
{
lab.Left = endPoint - lab.Width;
}
For example, you have a label named myLabel positioned at (75,75) and it currently has a width of 25 and you always want it to end at (100,75). Then when this happens:
myLabel.Text = "blah blah blah blah blah blah";
you then follow the text change with:
repostitionLabel(myLable, 100);
This will make it look like the label expanded to the left.
You could use also RightToLeft label property instead of modifying Anchor.
精彩评论