[英]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.