Size of Labels
-
Hi! I have a windows forms control of the following type with 4 Labels in it: ***************************** *label1......*label2........* ***************************** *label3......*label4........* ***************************** Each oft the labels has to fill exactly one quarter of the whole form, because the background color of the labels is changed at runtime and it doesn't look good if the color fills just the back oft the text. Labelproperty autosize seems like a simple solution, but if i set it, the label is always just as big as the text is. Without autosize, it looks not bad, but I don't get it to split in the center. It looks like that: ******************************* *label1...............*label2 * *label3...............*label4 * ******************************* The rightern labels don't grow with the control, the remain on rightern side in their usual size. Anyone an idea? Thanks, cherry
-
Hi! I have a windows forms control of the following type with 4 Labels in it: ***************************** *label1......*label2........* ***************************** *label3......*label4........* ***************************** Each oft the labels has to fill exactly one quarter of the whole form, because the background color of the labels is changed at runtime and it doesn't look good if the color fills just the back oft the text. Labelproperty autosize seems like a simple solution, but if i set it, the label is always just as big as the text is. Without autosize, it looks not bad, but I don't get it to split in the center. It looks like that: ******************************* *label1...............*label2 * *label3...............*label4 * ******************************* The rightern labels don't grow with the control, the remain on rightern side in their usual size. Anyone an idea? Thanks, cherry
I would set the Anchor property of labels 1 and 3 to Left, and set labels 2 and 4 to Right (this keeps the left/right edge of the label a fixed distance from the corresponding edge of the form. Then implement on OnResize event for your form, and set the Width of labels 1 and 3, and the Left of labels 2 and 4, like so:
void OnResize(Object^ sender, EventArgs^ e)
{
label1->Width = Width / 2;
label3->Width = Width / 2;
label2->Left = Width / 2;
label4->Left = Width / 2;
}You may need to add a slight offset to one of these if the labels look like they overlap at all, but this should get you started. You can also do something similar to this for making your labels fill the form vertically. Dybs
-
I would set the Anchor property of labels 1 and 3 to Left, and set labels 2 and 4 to Right (this keeps the left/right edge of the label a fixed distance from the corresponding edge of the form. Then implement on OnResize event for your form, and set the Width of labels 1 and 3, and the Left of labels 2 and 4, like so:
void OnResize(Object^ sender, EventArgs^ e)
{
label1->Width = Width / 2;
label3->Width = Width / 2;
label2->Left = Width / 2;
label4->Left = Width / 2;
}You may need to add a slight offset to one of these if the labels look like they overlap at all, but this should get you started. You can also do something similar to this for making your labels fill the form vertically. Dybs
Thanks Dybs, that might help - I just thought it could be made more nicely. Something like autosizing with setting the anchors to all sides... Thanks and bye! :)
-
Thanks Dybs, that might help - I just thought it could be made more nicely. Something like autosizing with setting the anchors to all sides... Thanks and bye! :)
I've tried setting both the left and right anchors, and it didn't quite get me there. If labels 1 and 2 start out as:
|label 1|
|label 2|and you set both the Left and Right anchors for both labels and then stretch the form, your labels will overlap like so:
|label 1|
|label 2|Like I mentioned above, the anchor keeps the edge of your control a fixed distance from the edge of its container. This fixed distance is in pixels, not percentage or proportion, unfortunately. If you find a nicer way to do this though, let me know! This is something I run into all the time, and this is the best I've been able to do so far. Dybs
-
Hi! I have a windows forms control of the following type with 4 Labels in it: ***************************** *label1......*label2........* ***************************** *label3......*label4........* ***************************** Each oft the labels has to fill exactly one quarter of the whole form, because the background color of the labels is changed at runtime and it doesn't look good if the color fills just the back oft the text. Labelproperty autosize seems like a simple solution, but if i set it, the label is always just as big as the text is. Without autosize, it looks not bad, but I don't get it to split in the center. It looks like that: ******************************* *label1...............*label2 * *label3...............*label4 * ******************************* The rightern labels don't grow with the control, the remain on rightern side in their usual size. Anyone an idea? Thanks, cherry
Hi, this is what I would do: - add a TableLayoutPanel to your Form; by default it has two rows, two columns, resulting in four identical areas. - dock or anchor it as appropriate. - add a label in each cell of the TableLayoutPanel; dock-fill them and set TextAlign=ContentAlignment.MiddleCenter. Now the labels are centered and their background exactly fills a quarter of the TLP. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
Hi, this is what I would do: - add a TableLayoutPanel to your Form; by default it has two rows, two columns, resulting in four identical areas. - dock or anchor it as appropriate. - add a label in each cell of the TableLayoutPanel; dock-fill them and set TextAlign=ContentAlignment.MiddleCenter. Now the labels are centered and their background exactly fills a quarter of the TLP. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.