问题
Is there a way that I can use a variable to control which PictureBox I am using in Visual Basic?
I.e.:
CurrentNumber = 1
PictureBox(CurrentNumber).backcolour = backcolour
回答1:
You can use the Me.Controls(String) indexer. It lets you specify the name (as a string) of the control you want to access, thus you can dynamically access a picture box by concatenating the string "PictureBox" with a number.
Dim TargetPictureBox As PictureBox = TryCast(Me.Controls("PictureBox" & CurrentNumber), PictureBox)
'Verifying that the control exists and that it was indeed a PictureBox.
If TargetPictureBox IsNot Nothing Then
TargetPictureBox.BackColor = Color.Red
End If
Alternatively, to save processing power by avoiding looping through the entire control collection every time you can call the OfType() extension on Me.Controls
, storing the result in an array sorted by the controls' names. That way it'd only have to iterate the control collection once.
'Class level - outside any methods (subs or functions).
Dim PictureBoxes As PictureBox() = Nothing
'Doesn't necessarily have to be done in a button, it's just an example.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If PictureBoxes Is Nothing Then
PictureBoxes = Me.Controls.OfType(Of PictureBox).OrderBy(Function(p As PictureBox) p.Name).ToArray()
End If
'NOTE: CurrentNumber - 1 is necessary when using an array!
PictureBoxes(CurrentNumber - 1).BackColor = Color.Red
End Sub
NOTE: This solution will only work properly if all your picture boxes are named "PictureBox1", "PictureBox2", etc. If you suddenly skip a number ("PictureBox3", "PictureBox5", "PictureBox6") then PictureBoxes(CurrentNumber - 1)
for CurrentNumber = 5
would return PictureBox6
rather than PictureBox5
.
回答2:
What you really should do is create a PictureBox()
and use that to reference your picture boxes via an index.
The best way to build your array is to create a method that builds the array from the references created by the designer. This lets you continue to use the designer to create your controls and it makes your code check for deleted controls at design-time. Using Me.Controls(...)
suffers from run-time errors if controls you are looking for have been deleted.
Here's the code you need:
Private _PictureBoxes As PictureBox() = Nothing
Sub AssignPictureBoxesArray
_PictureBoxes = {PictureBox1, PictureBox2, PictureBox3}
End Sub
Then you access them like this:
Sub SomeMethod
Dim CurrentNumber = 1
Dim PictureBox = _PictureBoxes(CurrentNumber - 1)
PictureBox.BackColor = System.Drawing.Color.Red
End Sub
回答3:
Check out the API docs for the PictureBox class, there are some examples.
I barely known anything about VB, but the constructor New PictureBox
will return an instance of the PictureBox class that you can track as a variable.
So something like
Dim my_pic_box = New PictureBox
my_pic_box.BackColor = some_background_color
but you probably want an array of PictureBox
s, so something like:
Dim picboxs(2) As PictureBox
picboxes(0) = New PictureBox
picboxes(0).BackColor = some_background_color
picboxes(1) = New PictureBox
picboxes(1).BackColor = some_background_color2
In the case of the array, using a loop would be better. Depends on how you are using these objects. If they are labeled parts of a UI, I wouldn't put them in an array at all, instead just make them individual Dim
s. If you are generating them based on some other input and the number of them changes at run time, then use an array.
来源:https://stackoverflow.com/questions/47374240/can-i-use-variables-to-control-which-picturebox-i-am-using