Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Visual Basic引用数组中的.Net对象_Arrays_Vb.net - Fatal编程技术网

Arrays Visual Basic引用数组中的.Net对象

Arrays Visual Basic引用数组中的.Net对象,arrays,vb.net,Arrays,Vb.net,我在VisualBasic2010的设计视图中有一个很大的对象列表,我需要为这些对象更改一系列属性,因此我当然尝试使用数组,而不是使用50-60行来执行重复任务。但在引用对象时似乎存在一个问题,它似乎只是从中获取信息。我知道那是一个糟糕的解释,但也许你看到它就会明白 Dim objectsToClear As Array = _ {lblDailyRoundTrip, lblDaysWorked, lblFillBoxes, lblMilesPerGallon, lblMonth

我在VisualBasic2010的设计视图中有一个很大的对象列表,我需要为这些对象更改一系列属性,因此我当然尝试使用数组,而不是使用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
请尝试以下方法:

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

注意-您确实应该对
选项进行严格的限制,并且应该强键入数组。

因为您似乎只想更改
.Text
.Visible
属性,那么您可以按名称查找控件,如下所示:

Dim returnValue As Control()
returnValue = Me.Controls.Find(objectsToClear(i), True)
For Each c As Control In returnValue
    c.Text = ""
    c.Visible = False
Next
注意:
True
参数用于是否搜索所有子项,听起来像是要搜索。阅读文档了解更多信息

现在您有了一个与指定名称匹配的控件集合,可以循环该集合中的控件并设置属性值,如下所示:

Dim returnValue As Control()
returnValue = Me.Controls.Find(objectsToClear(i), True)
For Each c As Control In returnValue
    c.Text = ""
    c.Visible = False
Next

谢谢,这是我刚开始学习的一门VB课程,本书在本书的后半部分教授Option Strict On和其他类似的内容。从他发布的代码中无法判断,但我认为
objectsToClear
的元素可能已经是控件了。