我已经编写了以下代码来在 VB.NET 中实现这一点:
Public Class TBdata
Public txtBox() As TextBox = {Form1.TextBox1, Form1.TextBox2, Form1.TextBox3, Form1.TextBox4}
Public aTextBoxes(3) As String
Public Sub DataToArray()
For i As Integer = 0 To 3
aTextBoxes(i) = txtBox(i).Text
Next
End Sub
End Class
'On the Form, to capture all entries in the text boxes
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim d As TBdata = New TBdata()
d.DataToArray()
Dim msg As String = ""
For i As Integer = 0 To 3
msg = msg & d.aTextBoxes(i) & " : "
Next
MessageBox.Show(msg)
End Sub
现在,为什么这不能在 C# 中完成?
class TBdata
{
public string[] aTextBoxes = new string[3];
public TextBox[] txtBox = new TextBox[] { Form1.textBox1, Form1.textBox2, Form1.textBox3, Form1.textBox4 };
public void DataToArray()
{
for (int i = 0; i < 4; i++)
{
aTextBoxes(i) = txtBox(i).Text;
}
}
}