Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vb.net - Setting a controls margin value

So, I'm adding a label programatically and I'm in need of altering the top margin a little bit to the value 8. I can't do that the obvious way, so what's wrong with my thinking?

Dim LabelAdapter As New Label
LabelAdapter.text = "Adapter"
LabelAdapter.Margin.Top = 8

This gives me the error "Expression is a value and therefore cannot be the target of an assignment".

like image 938
Kenny Bones Avatar asked Jan 29 '11 14:01

Kenny Bones


1 Answers

Label.Margin returns a Padding object.

Since Padding is a structure, it will actually return a copy. You are changing the Top value of that copy, not of the actual control’s margin. Since that would have no noticeable effect, VB correctly prevents it.

You need to assign a whole new margin. In fact, the Margin property (or rather, the Padding class) is arguably broken since it doesn’t allow an easy way to change the individual values.

Unfortunately, we just have to live with it. So to change just the Top value, we need to write:

Dim old As Padding = LabelAdapter.Margin
LabelAdapter.Margin = New Padding(old.Left, 8, old.Right, old.Bottom)

Weird, huh?

like image 131
Konrad Rudolph Avatar answered Nov 16 '22 02:11

Konrad Rudolph