|
-
Sep 29th, 2004, 11:55 AM
#1
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:
Dim _tmp_Graphics As System.Drawing.Graphics
Dim [hl=yellow]_tmp_SizeF As New System.Drawing.SizeF(_tmp_Graphics.MeasureString(_lbl.Text, _lbl.Font))[/hl]
Dim _lblSize_Height As Single
Dim _lblSize_Width As Single
_lblSize_Height = _tmp_SizeF.Height
_lblSize_Width = _tmp_SizeF.Width
.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.
-
Sep 29th, 2004, 12:01 PM
#2
Dim _tmp_Graphics As Graphics = Graphics.FromImage(new Bitmap(1,1))
-
Sep 29th, 2004, 03:30 PM
#3
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...
-
Sep 29th, 2004, 03:34 PM
#4
I just found out, that you don't need a rectangle.
The width will suffice.
VB Code:
Dim G As Graphics = Me.CreateGraphics
Dim txtSize As SizeF
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
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...
-
Sep 30th, 2004, 04:24 AM
#5
Great, thanks a lot. Today is a good day. 
I ended up using this:
VB Code:
Dim G As Graphics = _lbl.CreateGraphics
Dim txtSize As SizeF
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
txtSize = G.MeasureString(.Text, _lbl.Font, 250, sf)
Dim _lblSize_Height As Single
Dim _lblSize_Width As Single
_lblSize_Height = txtSize.Height
_lblSize_Width = txtSize.Width
.Size = New System.Drawing.Size(_lblSize_Width, _lblSize_Height)
-
Sep 30th, 2004, 08:20 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|