vb.net中的按钮阵列



我有此PHP文件与服务器通信。该文件需要VB.NET程序中的两个变量,即坐标x,y。

我设法生成了一个32x32按钮矩阵。每个按钮应将PHP文件发送两个字符串的变量。假设第一行具有y =" 001"和该行x =" 001"," 002"等的32个按钮,等等。

显然,我有一个可以执行发送的函数,但是我如何分辨第一行您应该发送y =" 001",并基于该行的按钮,该按钮从001到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 ' No 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通常在C 中代码。我可以在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));
        }
    }
}

和是否适用于所有32行?

最终工作代码要感谢Drdonut:

Public Class frmButton

    '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

以某种方式,您必须将按钮本身的按钮的coRdine放在按钮本身中,因此单击时可以将其发送。假设您的按钮是Windows表单按钮,则可以使用按钮的name字段:

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

您可以在制作按钮阵列的循环后立即添加此代码。

此代码将按钮的name属性设置为RowColumn。示例:第14行的按钮,第5列将获取名称:014005。按钮。

这样,所有名称都设置为: 00x00y

在您的clickButton函数中,sender对象是按钮,因此您可以通过:

提取位置
dim row as integer = Convert.toInt32(sender.name.subString(0,3))
dim col as integer = Convert.toInt32(sender.name.subString(3,3))

最新更新