开发者

Label attributes

Is there an effecient way to give one label the ForeColor, BackColor and Text of another one of 3 possible labels? I need to do this frequently for a dozen开发者_如何学Python labels in a windows form.


No. You can't assign multiple attributes from one label to another all in one VB.NET call.

But what you can do is build a helper method: pass in the source and destination controls and assign properties that way.

Sub ApplyStyle(Label destination, Label source)   
     destination.ForeColor = source.ForeColor  
     destination.BackColor = source.BackColor   
     destination.Text =source.Text 
End Sub

' example call 
ApplyStyle(unstyledLabel, styledLabel)


Giving all labels the same Text doesn't make any sense, let's assume you meant "Font". Yes, this is possible, all three properties are "ambient" properties. Which means that when you don't explicitly give them a value, either in the designer or in your code, then they'll 'inherit' the property value of their container.

Ambient properties were designed to easily give your UI consistent look and feel. Just assign the parent control's property and the label property automatically get the same value. You can put them in, say, a Panel to isolate them from the other controls in the form if necessary.

Another way in the designer is to select them all by dragging a larger rectangle around them and setting the property. Ctrl+Click to add/remove controls from that selection. Favor the ambient property approach though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜