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".
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With