RachetRacoon RachetRacoon - 5 months ago 112
Vb.net Question

Button Array in VB.net

I have this php file that communicates with a server, the file requires two variables from the vb.net programm the coordinates x,y.
I've managed to generate a 32x32 matrix of buttons, each button should send the php file the two variables witch are string, let's say the first row has as y="001" and the 32 buttons of that row x="001","002" ecc.I've obbviusly got a funtion that does the sending but how do i tell that for the first row you should send y="001" and based on the button of that row his x witch goes from 001 to 032.

Imports System.IO
Imports System.Net

Public Class frmButton
Inherits System.Windows.Forms.Form

' Declaring array of (1024) Buttons
Private btnArray(1024) As System.Windows.Forms.Button

'Funzione che invia sorgente e destinazione al file PHP
Public Function inviaphp(ByVal x As String, ByVal y As String) As String
Dim strReq As String
Dim strData As String
Dim dataStream As Stream
Dim reader As StreamReader
Dim request As WebRequest
Dim response As WebResponse
Dim src As String = x
Dim dest As String = y
strReq = "http://localhost/Matrice_PHPVBNET/R1/prova1.php?src=" & src & "&&dest=" & dest
request = WebRequest.Create(strReq)
response = request.GetResponse()
dataStream = response.GetResponseStream()
reader = New StreamReader(dataStream)
strData = reader.ReadToEnd()
reader.Close()
response.Close()
End Function
Private Sub AddButtons()
Dim xPos As Integer = 0
Dim yPos As Integer = -20
Dim n As Integer = 0

For i As Integer = 0 To 1023
' Initialize one variable
btnArray(i) = New System.Windows.Forms.Button
Next i


While (n < 1024)
With (btnArray(n))
.Tag = n + 1 ' Tag of button
.Width = 24 ' Width of button
.Height = 20 ' Height of button
If (n Mod 32 = 0) Then ' Location of buttons:
xPos = 0
yPos += 20

End If
' Location of button:
.Left = xPos
.Top = yPos
' Add buttons to a Panel:
pnlButtons.Controls.Add(btnArray(n)) ' Let panel hold the Buttons
xPos = xPos + .Width
AddHandler .Click, AddressOf Me.ClickButton
n += 1
End With
End While

btnAddButton.Enabled = False ' not need now to this button now
label1.Visible = True
End Sub

' Result of (Click Button) event, get the text of button
Public Sub ClickButton(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
Dim btn As Button
Dim x As String = "000"
Dim y() As String = {"000", "001", "002", "003", "004", "005", "006", "007", "008", "009", "010", "011", "012", "013",
"014", "015", "016", "017", "018", "019", "020", "021", "022", "023", "024", "025", "026", "027", "028", "029", "030", "031",
"032"}
Dim j As Integer = 32
'For i As Integer = 1 To 1024
'If (i <= j) Then
'inviaphp(x, y(i))
'End If
' Next




End Sub

Private Sub btnAddButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddButton.Click
AddButtons()
End Sub

Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

Private Sub pnlButtons_Paint(sender As Object, e As PaintEventArgs) Handles pnlButtons.Paint

End Sub

Private Sub frmButton_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub
End Class


I usually code in c++. Can i do something like this in vb.net

void btn(button btnarray(), int d)
{
char y;
char x()={"001","002""0xx","032"};
for(int i=0;i<d;i++)
{
if(i<=32)
{
btnarray(i).click=
y="001";
inviaphp(y,x(i));
}
}
}


and have if's for all 32 row?

Final working code thank to DrDonut

'Funzione che invia sorgente e destinazione al file PHP
Public Function inviaphp(ByVal comand As String) As Object
Dim strReq As String
Dim strData As String
Dim dataStream As Stream
Dim reader As StreamReader
Dim request As WebRequest
Dim response As WebResponse
strReq = "http://localhost/Matrice_PHPVBNET/R1/prova1.php?comand=" & comand
request = WebRequest.Create(strReq)
response = request.GetResponse()
dataStream = response.GetResponseStream()
reader = New StreamReader(dataStream)
strData = reader.ReadToEnd()
reader.Close()
response.Close()
End Function
Private Sub AddButtons()
Dim xPos As Integer = 0
Dim yPos As Integer = -20
Dim n As Integer = 0

For i As Integer = 0 To 1023
' Initialize one variable
btnArray(i) = New System.Windows.Forms.Button
Next i


While (n < 1024)
With (btnArray(n))
.Tag = n + 1 ' Tag of button
.Width = 24 ' Width of button
.Height = 20 ' Height of button
If (n Mod 32 = 0) Then ' Location of buttons:
xPos = 0
yPos += 20

End If
' Location of button:
.Left = xPos
.Top = yPos
' Add buttons to a Panel:
pnlButtons.Controls.Add(btnArray(n)) ' Let panel hold the Buttons
xPos = xPos + .Width
AddHandler .Click, AddressOf ClickButton
n += 1
End With
End While

For row As Integer = 0 To 31
For col As Integer = 0 To 31
btnArray(row * 32 + col).Name = (row + 1).ToString("D3") + (col + 1).ToString("D3")
Next
Next

btnAddButton.Enabled = False ' not need now to this button now
label1.Visible = True
End Sub

' Result of (Click Button) event, get the text of button
Public Sub ClickButton(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
Dim row As String = sender.name.subString(0, 3)
Dim col As String = sender.name.subString(3, 3)
MsgBox(sender.name)
Dim comando As String = row + "," + col
inviaphp(comando)


End Sub

Private Sub btnAddButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddButton.Click
AddButtons()
End Sub

Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

Private Sub pnlButtons_Paint(sender As Object, e As PaintEventArgs) Handles pnlButtons.Paint

End Sub

Private Sub frmButton_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub
End Class

Answer

Somehow you have to put the coördinates of the button in the button itself, so it can send it when it's clicked. Assuming that your buttons are Windows Forms Buttons, you can use the name field of the buttons:

for row as integer = 0 to 31
    for col as integer = 0 to 31
        btnArray(row*32+col).name = (row+1).ToString("D3") + (col+1).ToString("D3")
    end for
end for

You can add this code right after the loop where you make your button array.

This code sets the name property of the button to rowColumn. Example: A button at row 14, column 5 would get the name: 014005. Because both the row and the column are always written as 3 digits (the 'D3' argument in the ToString method), it is easy to filter where you read the button.

This way all the names are set to: 00x00y.

In your clickButton function, the sender object is the button, so you can extract the position by:

dim row as integer = Convert.toInt32(sender.name.subString(0,3))
dim col as integer = Convert.toInt32(sender.name.subString(3,3))
Comments