我在Visual Basic 2010的设计视图中有一个很大的对象列表,我需要更改一堆属性,所以当然我尝试使用数组而不是为重复任务花费50-60行。但似乎存在一个引用该对象的问题,它似乎只是从中获取信息。我知道这是一个糟糕的解释,但也许你会在看到它时理解它。
Dim objectsToClear As Array = _
{lblDailyRoundTrip, lblDaysWorked, lblFillBoxes, lblMilesPerGallon, lblMonthlyInsurance, _
lblMonthlyMaintenance, lblMonthlyParking, tbDailyRoundTrip, tbDaysWorked, tbMilesPerGallon, _
tbMonthlyInsurance, tbMonthlyMaintenance, tbMonthlyParking}
For i = LBound(objectsToClear) To UBound(objectsToClear)
objectsToClear(i).Text = ""
objectsToClear(i).Visible = False
Next
答案 0 :(得分:0)
请改为尝试:
Dim objectsToClear As Array = { lblDailyRoundTrip,
lblDaysWorked,
lblFillBoxes,
lblMilesPerGallon,
lblMonthlyInsurance,
lblMonthlyMaintenance,
lblMonthlyParking,
tbDailyRoundTrip,
tbDaysWorked,
tbMilesPerGallon,
tbMonthlyInsurance,
tbMonthlyMaintenance,
tbMonthlyParking }
For Each item In objectsToClear
item.Text = String.Empty
item.Visible = False
Next item
P.S。 - 你真的应该有Option Strict On
,你应该强烈输入你的数组。
答案 1 :(得分:0)
由于您似乎只想更改.Text
和.Visible
属性,因此您可以按名称查找控件,如下所示:
Dim returnValue As Control()
returnValue = Me.Controls.Find(objectsToClear(i), True)
注意:True
参数用于是否搜索所有孩子,这听起来像你想要做的。有关详细信息,请阅读Control.ControlCollection.Find Method文档。
现在您有一组与您指定的名称匹配的控件,循环遍历该集合中的控件并设置属性值,如下所示:
For Each c As Control In returnValue
c.Text = ""
c.Visible = False
Next