0

I have a big list of objects in the Design view of Visual Basic 2010 that I need to change a bunch of properties for, so of course I tried using an array rather than taking 50-60 lines for a repetitive task. But there seems to be an issue referencing to the object and it seems to be just taking the information from it. I know that was a shitty explanation, but maybe you'll understand it when you see it.

    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

2 Answers 2

1

Try this instead:

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. - you REALLY should have Option Strict On, and you should have strongly typed your array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is for a VB class I just started and the book teaches Option Strict On and other such things in the later part of the book.
1

Since you seem to be interested in changing only .Text and .Visible properties, then you can just find the control by name, like this:

Dim returnValue As Control()
returnValue = Me.Controls.Find(objectsToClear(i), True)

Note: The True argument is for whether or not to search all children, which it sounds like you want to do. Read Control.ControlCollection.Find Method documentation for more information.

Now that you have a collection of controls that match the name you specified, loop through the controls in that collection and set the property values, like this:

For Each c As Control In returnValue
    c.Text = ""
    c.Visible = False
Next

1 Comment

It's impossible to tell from the code he posted, but I think that the elements of objectsToClear might already be controls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.