'Visual Basic 2019 - Can you reference an object without its name?
I am programming chess, all of my objects are pictureboxes. I have all of the pictureboxes in a 2-dimensional array. If I get the name of the picturebox they click on, then substring the name to get the number it is (Example:54), can I use the number in order to reference a different picturbox (Example: Dim intNum as integer = picClick.Name.Substring(3,2) Dim strName as string = "pic" & intNum-8 Dim picNext as PicturBox = strName |or| picNext.Name = strName) Something similar to that. I know the 'picNext.Name = strName' doesn't work, because I have already tried it. Another idea, would I be able to reference the picturebox using the tag property? I have all of the pictureboxes with tags from 0,0-7,7. Bottom line is , Is there any way to reference/grab an object for future use, instead of using a ludicrous amount of If Structures. Sorry if formatting is weird.
[enter image description here][1]
[enter image description here][2]
[enter image description here][3]
[enter image description here][4]
Public Class frmChess
Private strBoard(7, 7) As String, Click1 As PictureBox, Click2 As PictureBox,
blnWhite As Boolean, bln1stClick As Boolean
Private Sub PictureBoxClick(sender As Object, e As EventArgs) Handles pic1.Click,
pic2.Click, pic3.Click, pic4.Click, pic5.Click, pic6.Click, pic7.Click,
pic8.Click, pic9.Click, pic10.Click, pic11.Click, pic12.Click, pic13.Click,
pic14.Click, pic15.Click, pic16.Click, pic17.Click, pic18.Click, pic19.Click,
pic20.Click, pic21.Click, pic22.Click, pic23.Click, pic24.Click, pic25.Click,
pic26.Click, pic27.Click, pic28.Click, pic29.Click, pic30.Click, pic31.Click,
pic32.Click, pic33.Click, pic34.Click, pic35.Click, pic36.Click, pic37.Click,
pic38.Click, pic39.Click, pic40.Click, pic41.Click, pic42.Click, pic43.Click,
pic44.Click, pic45.Click, pic46.Click, pic47.Click, pic48.Click, pic49.Click,
pic50.Click, pic51.Click, pic52.Click, pic53.Click, pic54.Click, pic55.Click,
pic56.Click, pic57.Click, pic58.Click, pic59.Click, pic60.Click, pic61.Click,
pic62.Click, pic63.Click, pic64.Click
Dim picClick As PictureBox = sender
Dim intRow As Integer, intCol As Integer
intRow = picClick.Tag.Substring(0, 1)
intCol = picClick.Tag.substring(2, 1)
If blnWhite = True Then
If bln1stClick = True Then
Click1 = picClick
picClick.Enabled = False
Dim intPlace As Integer = Click1.Name.Substring(3, 2)
If strBoard(intRow, intCol) <> "" Then
Dim strPiece As String = strBoard(intRow, intCol)
If strPiece = "White Pawn" Then
If Click1.Tag Like "6,#" Then
'Two places
Dim strName As String, picNext As PictureBox
strName = "pic" & intPlace - 8
picNext.Name = strName
If strBoard(picNext.Tag.substring(0, 1),
picNext.Tag.substring(2, 1)) <> "" Then
'No Change
Else
If intPlace - 8 = 42 Or intPlace - 8 = 44 Or intPlace
- 8 = 46 Or intPlace - 8 = 48 Then 'Light Squares
picNext.Image =
My.Resources.Light_Square_HighLightedMove
Else '41,43,45,47 | Dark Squares
picNext.Image =
My.Resources.Dark_Square_HighLightedMove
End If
strName = "pic" & intPlace - 16
picNext.Name = strName
If intPlace - 16 = 33 Or intPlace - 16 = 35 Or
intPlace - 16 = 37 Or intPlace - 16 = 39 Then 'Light Squares
picNext.Image =
My.Resources.Light_Square_HighLightedMove
Else '34,36,38,40 | Dark Squares
picNext.Image =
My.Resources.Dark_Square_HighLightedMove
End If
End If
Else
'One place
End If
ElseIf strPiece = "White Rook" Then
ElseIf strPiece = "White Knight" Then
ElseIf strPiece = "White Bishop" Then
ElseIf strPiece = "White Queen" Then
Else 'White King
End If
End If
Else
End If
'Check for Check
'If so, Check for Win
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
End
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles
btnReset.Click
'Clear
'Reset
Static picPictureBox(,) As PictureBox = {{pic1, pic2, pic3, pic4, pic5, pic6,
pic7, pic8},
{pic9, pic10, pic11, pic12, pic13,
pic14, pic15, pic16},
{pic17, pic18, pic19, pic20, pic21,
pic22, pic23, pic24},
{pic25, pic26, pic27, pic28, pic29,
pic30, pic31, pic32},
{pic33, pic34, pic35, pic36, pic37,
pic38, pic39, pic40},
{pic41, pic42, pic43, pic44, pic45,
pic46, pic47, pic48},
{pic49, pic50, pic51, pic52, pic53,
pic54, pic55, pic56},
{pic57, pic58, pic59, pic60, pic61,
pic62, pic63, pic64}}
strBoard(0, 0) = "Black Rook"
strBoard(0, 1) = "Black Knight"
strBoard(0, 2) = "Black Bishop"
strBoard(0, 3) = "Black Queen"
strBoard(0, 4) = "Black King"
strBoard(0, 5) = "Black Bishop"
strBoard(0, 6) = "Black Knight"
strBoard(0, 7) = "Black Rook"
For intIndex = 0 To 7
strBoard(1, intIndex) = "Black Pawn"
strBoard(6, intIndex) = "White Pawn"
Next intIndex
strBoard(7, 0) = "White Rook"
strBoard(7, 1) = "White Knight"
strBoard(7, 2) = "White Bishop"
strBoard(7, 3) = "White Queen"
strBoard(7, 4) = "White King"
strBoard(7, 5) = "White Bishop"
strBoard(7, 6) = "White Knight"
strBoard(7, 7) = "White Rook"
blnWhite = True
bln1stClick = True
End Sub
Private Sub frmChess_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Static picPictureBox(,) As PictureBox = {{pic1, pic2, pic3, pic4, pic5, pic6,
pic7, pic8},
{pic9, pic10, pic11, pic12, pic13,
pic14, pic15, pic16},
{pic17, pic18, pic19, pic20, pic21,
pic22, pic23, pic24},
{pic25, pic26, pic27, pic28, pic29,
pic30, pic31, pic32},
{pic33, pic34, pic35, pic36, pic37,
pic38, pic39, pic40},
{pic41, pic42, pic43, pic44, pic45,
pic46, pic47, pic48},
{pic49, pic50, pic51, pic52, pic53,
pic54, pic55, pic56},
{pic57, pic58, pic59, pic60, pic61,
pic62, pic63, pic64}}
strBoard(0, 0) = "Black Rook"
strBoard(0, 1) = "Black Knight"
strBoard(0, 2) = "Black Bishop"
strBoard(0, 3) = "Black Queen"
strBoard(0, 4) = "Black King"
strBoard(0, 5) = "Black Bishop"
strBoard(0, 6) = "Black Knight"
strBoard(0, 7) = "Black Rook"
For intIndex = 0 To 7
strBoard(1, intIndex) = "Black Pawn"
strBoard(6, intIndex) = "White Pawn"
Next intIndex
strBoard(7, 0) = "White Rook"
strBoard(7, 1) = "White Knight"
strBoard(7, 2) = "White Bishop"
strBoard(7, 3) = "White Queen"
strBoard(7, 4) = "White King"
strBoard(7, 5) = "White Bishop"
strBoard(7, 6) = "White Knight"
strBoard(7, 7) = "White Rook"
blnWhite = True
bln1stClick = True
End Sub
End Class
[1]: https://i.stack.imgur.com/kZ6Wm.png
[2]: https://i.stack.imgur.com/CklD0.png
[3]: https://i.stack.imgur.com/6OAm6.png
[4]: https://i.stack.imgur.com/UDsu0.png
Solution 1:[1]
Instead of grabbing the image, I have put all of the pictureboxes into an array. Using the array, I can use my intPlace variable to grab an index from the array.
Example: Tiles(intPlace-8).Image = My.Resources.Light_Square_HighLightedMove
This allowed me to grab the index of the first picturebox I clicked on, then grab the next picturebox by subtracting 8 from the index, and finally change the properties of the new picturebox.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Henry Weiser |
