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. C#
  4. Get rid of label margins

Get rid of label margins

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
15 Posts 5 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.
  • T Offline
    T Offline
    Tony Pottier
    wrote on last edited by
    #1

    I'm using several labels located next to each other thanks to left docking and the autosize property. The thing is, even if you set margin/padding to 0, there's still a margin: Picture explaining the problem: http://uppix.net/5/1/2/69b945ab92a5598fb93aca1d50be6.jpg[^] How to get rid of this? I'm using labels because each label can have a different color/font/ The RTF box is a no go as it doesn't have any autosize property and it doesnt behave like a label (you can select the text). "RTF label" control on this website doesnt have the autosize property and you can still select the text...

    D realJSOPR 2 Replies Last reply
    0
    • T Tony Pottier

      I'm using several labels located next to each other thanks to left docking and the autosize property. The thing is, even if you set margin/padding to 0, there's still a margin: Picture explaining the problem: http://uppix.net/5/1/2/69b945ab92a5598fb93aca1d50be6.jpg[^] How to get rid of this? I'm using labels because each label can have a different color/font/ The RTF box is a no go as it doesn't have any autosize property and it doesnt behave like a label (you can select the text). "RTF label" control on this website doesnt have the autosize property and you can still select the text...

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You can't get rid of it. If this is a problem, why even use a Label? Why not just render the text yourself with DrawString? You'd probably end up doing the same thing if you made your own Label control and supplied your own drawing code anyway.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      T 1 Reply Last reply
      0
      • T Tony Pottier

        I'm using several labels located next to each other thanks to left docking and the autosize property. The thing is, even if you set margin/padding to 0, there's still a margin: Picture explaining the problem: http://uppix.net/5/1/2/69b945ab92a5598fb93aca1d50be6.jpg[^] How to get rid of this? I'm using labels because each label can have a different color/font/ The RTF box is a no go as it doesn't have any autosize property and it doesnt behave like a label (you can select the text). "RTF label" control on this website doesnt have the autosize property and you can still select the text...

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #3

        You could write your own label class that simply overrides the base paint method.

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          You can't get rid of it. If this is a problem, why even use a Label? Why not just render the text yourself with DrawString? You'd probably end up doing the same thing if you made your own Label control and supplied your own drawing code anyway.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          T Offline
          T Offline
          Tony Pottier
          wrote on last edited by
          #4

          I can't use DrawString because I need to know the exact length of the text: that's why the autosize property is so convenient. I just set the texts of the labels and add the width properties to know the final length of all the labels. If I'm using DrawString or something that overrides the WM_PAINT of the label, how do I know the length in pixels of the text?

          D 1 Reply Last reply
          0
          • T Tony Pottier

            I can't use DrawString because I need to know the exact length of the text: that's why the autosize property is so convenient. I just set the texts of the labels and add the width properties to know the final length of all the labels. If I'm using DrawString or something that overrides the WM_PAINT of the label, how do I know the length in pixels of the text?

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Tony_P wrote:

            I can't use DrawString because I need to know the exact length of the text

            That's what MeasureString is for.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            T 2 Replies Last reply
            0
            • D Dave Kreskowiak

              Tony_P wrote:

              I can't use DrawString because I need to know the exact length of the text

              That's what MeasureString is for.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              T Offline
              T Offline
              Tony Pottier
              wrote on last edited by
              #6

              Alright I've just found this in the meantime. Thank you for your time!

              1 Reply Last reply
              0
              • D Dave Kreskowiak

                Tony_P wrote:

                I can't use DrawString because I need to know the exact length of the text

                That's what MeasureString is for.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                T Offline
                T Offline
                Tony Pottier
                wrote on last edited by
                #7

                this is what I came up with:

                public class EnhancedLabel : Label
                {
                protected override void OnPaint(PaintEventArgs e)
                {
                Graphics g = e.Graphics;
                Brush fore = new SolidBrush(this.ForeColor);
                Brush back = new SolidBrush(this.BackColor);

                        SizeF s = g.MeasureString(this.Text, this.Font);
                        this.Width = (int)s.Width;
                        this.Height = (int)s.Height;
                
                        g.FillRectangle(back, this.Left, this.Top, this.Width, this.Height);
                        g.DrawString(this.Text, this.Font, fore, this.Location);
                    }
                }
                
                T 1 Reply Last reply
                0
                • T Tony Pottier

                  this is what I came up with:

                  public class EnhancedLabel : Label
                  {
                  protected override void OnPaint(PaintEventArgs e)
                  {
                  Graphics g = e.Graphics;
                  Brush fore = new SolidBrush(this.ForeColor);
                  Brush back = new SolidBrush(this.BackColor);

                          SizeF s = g.MeasureString(this.Text, this.Font);
                          this.Width = (int)s.Width;
                          this.Height = (int)s.Height;
                  
                          g.FillRectangle(back, this.Left, this.Top, this.Width, this.Height);
                          g.DrawString(this.Text, this.Font, fore, this.Location);
                      }
                  }
                  
                  T Offline
                  T Offline
                  Tony Pottier
                  wrote on last edited by
                  #8

                  And a version 2.0, much better ;o

                  protected override void OnPaint(PaintEventArgs e)
                  {
                  Graphics g = e.Graphics;
                  Brush fore = new SolidBrush(this.ForeColor);
                  Brush back = new SolidBrush(this.BackColor);

                          g.FillRectangle(back, this.Location.X, this.Location.Y, this.Width, this.Height);
                          g.DrawString(this.Text, this.Font, fore, Point.Empty);
                      }
                  
                      protected override void OnTextChanged(EventArgs e)
                      {
                          Graphics g = System.Drawing.Graphics.FromHwnd(this.Handle);
                          SizeF s = g.MeasureString(this.Text, this.Font);
                          this.Width = (int)s.Width;
                          this.Height = (int)s.Height;
                  
                          base.OnTextChanged(e);
                      }
                  
                  D 1 Reply Last reply
                  0
                  • T Tony Pottier

                    And a version 2.0, much better ;o

                    protected override void OnPaint(PaintEventArgs e)
                    {
                    Graphics g = e.Graphics;
                    Brush fore = new SolidBrush(this.ForeColor);
                    Brush back = new SolidBrush(this.BackColor);

                            g.FillRectangle(back, this.Location.X, this.Location.Y, this.Width, this.Height);
                            g.DrawString(this.Text, this.Font, fore, Point.Empty);
                        }
                    
                        protected override void OnTextChanged(EventArgs e)
                        {
                            Graphics g = System.Drawing.Graphics.FromHwnd(this.Handle);
                            SizeF s = g.MeasureString(this.Text, this.Font);
                            this.Width = (int)s.Width;
                            this.Height = (int)s.Height;
                    
                            base.OnTextChanged(e);
                        }
                    
                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #9

                    Just a couple of points - You should dispose of your brushes, or better still put them in using blocks.

                    using (Brush back = new SolidBrush(ForeColor))
                    {
                    e.Graphics.FillRectangle(back, Location.X, Location.Y, Width, Height);
                    }
                    using (Brush fore = new SolidBrush(BackColor))
                    {
                    e.Graphics.DrawString(Text, Font, fore, Point.Empty);
                    }

                    You can use the Size struct's static Round method to convert the SizeF

                    Size = Size.Round(s);

                    Dave
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                    T 1 Reply Last reply
                    0
                    • D DaveyM69

                      Just a couple of points - You should dispose of your brushes, or better still put them in using blocks.

                      using (Brush back = new SolidBrush(ForeColor))
                      {
                      e.Graphics.FillRectangle(back, Location.X, Location.Y, Width, Height);
                      }
                      using (Brush fore = new SolidBrush(BackColor))
                      {
                      e.Graphics.DrawString(Text, Font, fore, Point.Empty);
                      }

                      You can use the Size struct's static Round method to convert the SizeF

                      Size = Size.Round(s);

                      Dave
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                      T Offline
                      T Offline
                      Tony Pottier
                      wrote on last edited by
                      #10

                      Right, thank you for the tips. I used the brushes without even checking if it was a disposable object. Shame on me.

                      L 1 Reply Last reply
                      0
                      • T Tony Pottier

                        Right, thank you for the tips. I used the brushes without even checking if it was a disposable object. Shame on me.

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

                        Hi, the most expensive one, the one you really should dispose of, is the result of Graphics.FromHwnd() for the brushes, you might consider keeping them in a class member, rather than creating and disposing them all the time. creating a Graphics, as you do, is something I avoid even at modest cost; so I am not in favor of your 2.0 solution; instead I would do everything in OnPaint, possibly do MeasureString inside a test on text changed. :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        Fixturized forever. :confused:


                        T realJSOPR 2 Replies Last reply
                        0
                        • L Luc Pattyn

                          Hi, the most expensive one, the one you really should dispose of, is the result of Graphics.FromHwnd() for the brushes, you might consider keeping them in a class member, rather than creating and disposing them all the time. creating a Graphics, as you do, is something I avoid even at modest cost; so I am not in favor of your 2.0 solution; instead I would do everything in OnPaint, possibly do MeasureString inside a test on text changed. :)

                          Luc Pattyn [Forum Guidelines] [My Articles]


                          Fixturized forever. :confused:


                          T Offline
                          T Offline
                          Tony Pottier
                          wrote on last edited by
                          #12

                          Well the problem is that I need the width/height of the label without painting it, so I can't do everything in the paint event. The best way is probably to store the brushes, for sure. Can I store the Graphics.FromHwnd() the same way or can the value of Graphics.FromHwnd() change?

                          L 1 Reply Last reply
                          0
                          • T Tony Pottier

                            Well the problem is that I need the width/height of the label without painting it, so I can't do everything in the paint event. The best way is probably to store the brushes, for sure. Can I store the Graphics.FromHwnd() the same way or can the value of Graphics.FromHwnd() change?

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

                            Tony_P wrote:

                            I need the width/height of the label without painting it

                            Huh? if you need it inside OnPaint only, then do the MeasureString in OnPaint just before you use DrawString. if you need its result outside OnPaint, I would try and execute OnPaint once before you need the sizes. Keeping a Graphics does not feel right; for one it is an expensive object, and it contains many things that may be different each time you get a graphics; e.g. every time you move ab object over a Form, the Form may get Paint events with varying clip regions in the Graphics object. Also the user might change the resolution of the screen, and whatever more. :)

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            Fixturized forever. :confused:


                            1 Reply Last reply
                            0
                            • L Luc Pattyn

                              Hi, the most expensive one, the one you really should dispose of, is the result of Graphics.FromHwnd() for the brushes, you might consider keeping them in a class member, rather than creating and disposing them all the time. creating a Graphics, as you do, is something I avoid even at modest cost; so I am not in favor of your 2.0 solution; instead I would do everything in OnPaint, possibly do MeasureString inside a test on text changed. :)

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              Fixturized forever. :confused:


                              realJSOPR Offline
                              realJSOPR Offline
                              realJSOP
                              wrote on last edited by
                              #14

                              Why don't you just write a function that does something like this:

                              class BlahBlah
                              {
                              SizeF m_textSize;

                              public SizeF MeasureText(string text)
                              {
                                  (using Graphics graphics = Graphics.FromHwnd(this))
                                  {
                                      m\_textSize = graphics.MeasureString(text, someFont);
                                  }
                                  return m\_textSize;
                              }
                              

                              }

                              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                              -----
                              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                              L 1 Reply Last reply
                              0
                              • realJSOPR realJSOP

                                Why don't you just write a function that does something like this:

                                class BlahBlah
                                {
                                SizeF m_textSize;

                                public SizeF MeasureText(string text)
                                {
                                    (using Graphics graphics = Graphics.FromHwnd(this))
                                    {
                                        m\_textSize = graphics.MeasureString(text, someFont);
                                    }
                                    return m\_textSize;
                                }
                                

                                }

                                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                -----
                                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

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

                                I will do such things only if I see no reasonable alternative; I try and avoid creating a new Graphics as much as I can. In OnPaint the Graphics is for free, so why not take advantage of that one? In the few 100K lines of C# code I have at hand, I found only two instances of Graphics.FromHwnd; I do use Graphics.FromImage of course when drawing onto a Bitmap, there simply is no alternative there. :)

                                Luc Pattyn [Forum Guidelines] [My Articles]


                                Fixturized forever. :confused:


                                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