在 VB 中的 With 语句中有多个对象吗? [英] Have multiple objects in a With statement in VB?

查看:39
本文介绍了在 VB 中的 With 语句中有多个对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VB 中有很多标签,我在 With 语句中使用它们来设置它们的属性.

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

这是可能的,还是我必须为每个人手动执行 With 语句?

Is this possible, or do I have to manually do a With statement for each of them?

推荐答案

我会将这些类型的项目保存在一个列表中,然后对它们应用 for 每个循环,假设它们都是相同的类型(或者至少是基本的类型).假设您使用的是 label 类型的控件,这将是一个解决方案.请注意,我已将 .fontColor 修改为 .ForeColor 以便此示例适用于 Label 类:

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)

这篇关于在 VB 中的 With 语句中有多个对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆