我无法在 onload 事件中检索从运行时生成的数组文本框的值。这是代码。
来自表单 onload 事件:
private void frmMain_Load(object sender, EventArgs e)
{
TextBox[] txtFldNames = new TextBox[15];
int x = 155, y = 65, w = 300, h = 20;
for (int i = 0; i < 15; i++)
{
y = y + 30;
txtFldNames[i] = new TextBox();
txtFldNames[i].Location = new System.Drawing.Point(x, y);
txtFldNames[i].Size = new System.Drawing.Size(w, h);
this.Controls.Add(txtFldNames[i]);
txtFldNames[i].ReadOnly = true;
txtFldNames[i].BackColor = Color.White;
}
}
现在如何从按钮访问文本框值?
private void button1_Click(object sender, EventArgs e)
{
//what to do here?
}
我已经在下面尝试过这个,但它不起作用。我得到的只是空值。所以请指出我正确的方向
private void button1_Click(object sender, EventArgs e)
{
TextBox[] txtFldNames = new TextBox[15];
for (int j = 0; j < 15; j++)
{
txtFldNames[j] = new TextBox();
MessageBox.Show("" + txtFldNames[j].Text);
}
}
这是完整的代码:
public partial class classMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
TextBox[] txtFldNames = new TextBox[15];
int x = 155, y = 65, w = 300, h = 20;
for (int i = 0; i < 15; i++)
{
y = y + 30;
txtFldNames[i] = new TextBox();
txtFldNames[i].Location = new System.Drawing.Point(x, y);
txtFldNames[i].Size = new System.Drawing.Size(w, h);
this.Controls.Add(txtFldNames[i]);
txtFldNames[i].ReadOnly = true;
txtFldNames[i].BackColor = Color.White;
}
}
private void button1_Click(object sender, EventArgs e)
{
//what to do here?
}
}