c3ntury
c3ntury

Reputation: 73

How can I change a toolbox item property by referring to the item through a variable?

Sorry for the confusing title - here is the code I'm using.

Example code -

    If bolCorrect = False Then
        intIncorrect += 1
        temp3 = "picture" + CStr(intIncorrect)
        temp3.Visible = True

I've got several images all, with names of picture[number-from-0-to-10], and I want them to show depending on the count of a variable.

The error it throws up is that 'Visible' is not part of 'String'. How can I get the interpreter to look at 'temp3' in this instance, and refer to the toolbox item rather than the type of the variable (e.g. string)?

Upvotes: 0

Views: 178

Answers (2)

Steven Ackley
Steven Ackley

Reputation: 591

You need to refer to the actual name property you have set for the picturebox control (if you are using the picturebox control) So if your picture box control is named pb1

pb1.Image = System.Drawing.Image.FromFile("picture" + counter + ".jpg")
pb1.Visible = True

Upvotes: 1

Konrad Rudolph
Konrad Rudolph

Reputation: 546133

You should generally try to avoid addressing controls via strings, that’s usually just a hack around a proper solution. Instead, maintain a variable to that control, or, in your case, maintain an array of the relevant controls and access them via an index.

That said, it is possible to get a control given its name via the Form.Controls collection:

Dim ctl = Me.Controls("picture" + CStr(intIncorrect))

Upvotes: 0

Related Questions