Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Size of Labels

Size of Labels

Scheduled Pinned Locked Moved Managed C++/CLI
winformsquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cherrymotion
    wrote on last edited by
    #1

    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

    D L 2 Replies Last reply
    0
    • C cherrymotion

      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

      D Offline
      D Offline
      dybs
      wrote on last edited by
      #2

      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

      C 1 Reply Last reply
      0
      • D 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

        C Offline
        C Offline
        cherrymotion
        wrote on last edited by
        #3

        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! :)

        D 1 Reply Last reply
        0
        • C cherrymotion

          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! :)

          D Offline
          D Offline
          dybs
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • C cherrymotion

            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

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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.


            D 1 Reply Last reply
            0
            • L Luc Pattyn

              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.


              D Offline
              D Offline
              dybs
              wrote on last edited by
              #6

              Don't know why I didn't think of that. :) I've spent a good part of this week working on resizing the cells in the TLP!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups