Results 1 to 6 of 6

Thread: Sizing a label properly [Resolved]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Resolved Sizing a label properly [Resolved]

    I'm trying to create a custom messagebox. The MessageBox's form obviously contains a label.

    Just like the Windows MessageBox:
    The text in the label could be of any length.
    The label is of fixed width.
    The label can have varying height.

    The third point is the problem right now, I don't know how to make it have a varying height, I'd like to make it size itself accordingly with the text in there.


    Here is what I tried:

    VB Code:
    1. Dim _tmp_Graphics As System.Drawing.Graphics
    2.  
    3.  
    4.             Dim [hl=yellow]_tmp_SizeF As New System.Drawing.SizeF(_tmp_Graphics.MeasureString(_lbl.Text, _lbl.Font))[/hl]
    5.  
    6.             Dim _lblSize_Height As Single
    7.             Dim _lblSize_Width As Single
    8.  
    9.             _lblSize_Height = _tmp_SizeF.Height
    10.             _lblSize_Width = _tmp_SizeF.Width
    11.  
    12.             .Size = New System.Drawing.Size(_lblSize_Width, _lblSize_Height)

    _lbl is a System.Windows.Forms.Label.

    This errors out with a

    NullReferenceException
    Object reference not set to an instance of an object.
    on the yellow, highlighted line above.

    I can see that _tmp_Graphics = Nothing, but System.Drawing.Graphic's New() is private.

    Really not sure if this is even the right approach.

    Guidance, please.
    Last edited by mendhak; Sep 30th, 2004 at 04:24 AM.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Dim _tmp_Graphics As Graphics = Graphics.FromImage(new Bitmap(1,1))
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    You could also use _lbl.CreateGraphics, but that is not important.

    The important thing is, that you need a layout rectangle in your measurestring. If don't have that, the measurestring doesn't know about you fixed with, so the height will always be the height of 1 line.
    If you use a layoutrectangle (the height of this does not matter) it will know the width to use, and give you the right height.
    You could also add a StringFormat, to instruct it how to divide the lines and words and such.

    Did that make any sense at all?
    If not, let me know. I can try to elaborate further
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  4. #4
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    I just found out, that you don't need a rectangle.
    The width will suffice.

    VB Code:
    1. Dim G As Graphics = Me.CreateGraphics
    2.         Dim txtSize As SizeF
    3.         Dim sf As New StringFormat
    4.  
    5.         sf.Alignment = StringAlignment.Center
    6.         sf.LineAlignment = StringAlignment.Center
    7.  
    8.         txtSize = G.MeasureString("A very long text that will probably be too long to fit into 50 pixels", Me.Font, 50, sf)
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Great, thanks a lot. Today is a good day.

    I ended up using this:

    VB Code:
    1. Dim G As Graphics = _lbl.CreateGraphics
    2.             Dim txtSize As SizeF
    3.             Dim sf As New StringFormat
    4.             sf.Alignment = StringAlignment.Center
    5.             sf.LineAlignment = StringAlignment.Center
    6.             txtSize = G.MeasureString(.Text, _lbl.Font, 250, sf)
    7.             Dim _lblSize_Height As Single
    8.             Dim _lblSize_Width As Single
    9.             _lblSize_Height = txtSize.Height
    10.             _lblSize_Width = txtSize.Width
    11.             .Size = New System.Drawing.Size(_lblSize_Width, _lblSize_Height)

  6. #6
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width