我有一个标签,我想放在图片框的顶部。我希望它有一个透明的背景。
在这里搜索答案后,我发现了这个有效的解决方案:
infoText1.Parent = bg1; infoText1.BackColor = Color.Transparent;
然而,当我尝试运行该项目时,标签移动到图片框的最底部。
控件的
Location
是相对于它的父控件的,所以如果你改变Parent
,你改变它相对于窗体的位置,新父控件相对于旧控件的偏移量。对此进行补偿的方法是在更改 Parent
之前获取控件的绝对位置,然后更改 Parent
,然后将其放回相同的绝对位置。你可以这样做:
'Get the screen coordinates of the child control's Location.
Dim screenLocation = childControl.PointToScreen(Point.Empty)
'This would do the same thing:
'Dim screenLocation = 'childControl.Parent.PointToScreen(childControl.Location)
'Change the Parent.
childControl.Parent = newParent
'Set the child control's Location to those same screen coordinates.
childControl.Location = newParent.PointToClient(screenLocation)
如果您还没有,您应该将
Label
准确定位在设计器中您想要的位置,然后该代码将在更改父级时保持该位置。