label's autosize

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
i need it to be set to true, but i also need to extend the width by 5
like so:
VB.NET:
Label1.AutoSize = True
Label1.Width += 5
that doesnt work, how would one go about accomplishing this?

also whenever the text property changes i need the width to be 5 greater than what the new text is, like:
VB.NET:
 Label1.AutoSize = True
Label1.Text = "New Text"
 Label1.Width += 5

it seems that with autosize being set to true you cant specify a width or height whatsoever, it's picky like that lol
 
Last edited:

mzim

Well-known member
Joined
Jun 3, 2004
Messages
187
Location
Other side of the rock
Programming Experience
1-3
yeah i think you cant specify a size to the label when property autosize is set to true bec. its provide its own size from the word itself autosize. :D
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
hmm, i'll that

or i could provide my own autosize and measure the strings and stuff too... i was hoping to avoid a lotta work
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,328
Location
Sydney, Australia
Programming Experience
10+
Try making use of the PreferredWidth (and PreferredHeight, if necessary) properties of the label, i.e:
VB.NET:
label.AutoSize = False
label.Width = label.PreferredWidth + 5
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
jmcilhinney said:
Try making use of the PreferredWidth (and PreferredHeight, if necessary) properties of the label, i.e:
VB.NET:
label.AutoSize = False
label.Width = label.PreferredWidth + 5

actually i've been doing just that my current problem is that i need it to be 5 greater than whatever word is in the text property (it could be 2 to 15 characters at any given time) but i need it to be 5 pixels greater than the total pixel length of the word like so:
VB.NET:
Private Sub lbl1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblWord1.TextChanged
	Dim PaintArgs As System.Windows.Forms.PaintEventArgs
	lbl1.Height = Convert.ToInt32(PaintArgs.Graphics.MeasureString(lbl1.Text, lbl1.Font).Height)
	lblWord1.Width = Convert.ToInt32(PaintArgs.Graphics.MeasureString(lbl1.Text, lbl1.Font).Width + 5)
End Sub

I get the error 'Object reference not set to an instance of the object' when i try to use the PaintArgs.Graphics.MeasureString method in the TextChanged event there... i havent explored as to why yet

that's as far as i got tonight, but i am really tired so i havnt played with it much
 
Top Bottom