Vb.net - 设置控件边距值
因此,我以编程方式添加标签,并且需要将上边距稍微更改为值 8。我无法以明显的方式做到这一点,那么我的想法有什么问题吗?
Dim LabelAdapter As New Label
LabelAdapter.text = "Adapter"
LabelAdapter.Margin.Top = 8
这给了我错误“表达式是一个值,因此不能成为赋值的目标”。
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Label.Margin
返回 <代码>填充对象。由于
Padding
是一个结构,因此它实际上会返回一个副本。您正在更改该副本的Top
值,而不是实际控件的边距。由于这不会产生明显的影响,VB 正确地阻止了它。您需要分配一个全新的边距。事实上,Margin 属性(或者更确切地说,Padding 类)可以说已经损坏,因为它不允许以简单的方式更改各个值。
不幸的是,我们只能忍受它。因此,要仅更改
Top
值,我们需要编写:奇怪,是吗?
Label.Margin
returns aPadding
object.Since
Padding
is a structure, it will actually return a copy. You are changing theTop
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, thePadding
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:Weird, huh?