Reputation: 1920
I have a lot of labels in VB that I use in With
statement to set their properties.
Problem Is there any way I can do something like the following:
With lblA, lblB, lblC
.fontColor = color.Red
End With
Is this possible, or do I have to manually do a With
statement for each of them?
Upvotes: 5
Views: 10618
Reputation: 26414
There is a shorter and more readable version of your solution:
For Each lbl As Label In {lblA, lblB, lblC}
With lbl
'...
End With
Next
Upvotes: 6
Reputation: 12739
I would keep those types of items in a list and then apply a for each loop on them, assuming they are all of the same type (or at least base type). Assuming you are using controls of type label
this would be a solution. Note that I've modified .fontColor
to .ForeColor
so that this example works with the Label class:
Dim lblList as new List(of Label) ({lblA, lblB, lblC})
lblList.ForEach(sub(x) x.Fore Color = color.red)
Since you've posted your solution, you could still do the following to avoid the iteration loop over the array you made (which is why I do this as a list) not having to take into account the array size or anything:
lblList.ForEach(Sub(x)
With x
.BackColor = Color.Black
.Dock = DockStyle.Top
.TextAlign = ContentAlignment.MiddleCenter
End With
End Sub)
Upvotes: 5
Reputation: 1920
Here is the way I ended up doing it:
Dim arrayMe As Label() = {lblA, lblB, lblC}
For count = 0 To arrayMe.Length - 1 Step 1
With arrayMe(count)
.BackColor = Color.Black
.Dock = DockStyle.Top
.TextAlign = ContentAlignment.MiddleCenter
End With
Next
There are other ways to do it, but I found this to be useful.
Upvotes: 0