繁体   English   中英

VB.NET中的按钮数组

[英]Button array in VB.NET

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

我设法生成一个32x32的按钮矩阵。 每个按钮应向PHP文件发送两个变量,即字符串。 假设第一行的内容为y =“ 001”,该行的32个按钮x =“ 001”,“ 002”等。

我显然有一个执行发送的功能,但是我如何告诉您应该为第一行发送y =“ 001”,并根据该行的按钮将其x从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

我通常用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行中使用if吗?

最后的工作代码归功于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

您必须以某种方式将按钮的坐标放置在按钮本身中,以便单击时可以将其发送。 假设您的按钮是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。由于行和列均始终用3位数字表示(ToString方法中的“ D3”参数),因此很容易在读取位置进行过滤按钮。

这样,所有名称都设置为: 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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM