如何获取在from onload事件期间生成的数组文本框的值

本文关键字:数组 文本 事件 何获取 获取 onload from | 更新日期: 2023-09-27 17:58:28

在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?
    }
}

如何获取在from onload事件期间生成的数组文本框的值

给定完整的代码,您可以按照以下方式执行操作:

public partial class classMain : Form
{
    // Move your list to a global scope in the classMain form.
    TextBox[] txtFldNames = new TextBox[15];
    public frmMain()
    {
        InitializeComponent();
    }
    private void frmMain_Load(object sender, EventArgs e)
    {
        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?
        // Now you can access the global array variable.
        for (int i = 0; i < 15; i++)
        {
            MessageBox.Show(txtFldNames[i].Text);
        }
    }
}

如果你想稍微清理一下代码:

public partial class classMain : Form
{
    // Move your list to a global scope in the classMain form.
    TextBox[] txtFldNames = new TextBox[15];
    public frmMain()
    {
        InitializeComponent();
    }
    private void frmMain_Load(object sender, EventArgs e)
    {
        int x = 155, y = 65, w = 300, h = 20;
        for (int i = 0; i < 15; i++)
        {
            y = y + 30;
            var t = new TextBox
            {
                Location = new System.Drawing.Point(x, y),
                Size = new System.Drawing.Size(w, h),
                ReadOnly = true,
                BackColor = Color.White
            };
            txtFldNames[i] = t;
            this.Controls.Add(t);
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        //what to do here?
        // Now you can access the global array variable.
        for (int i = 0; i < txtFldNames.Length; i++)
        {
            MessageBox.Show(txtFldNames[i].Text);
        }
    }
}

您需要访问先前创建的文本框,而不是新的文本框:

private void button1_Click(object sender, EventArgs e)
{
    for (int j = 0; j < 15; j++)
    {
        MessageBox.Show("" + txtFldNames[j].Text);
    }
}

如果txtFldNames是加载事件中的局部变量,则需要将其更改为表单的实例字段。

您可以使用Enumerable.OfType查找TextBoxes:

foreach(TextBox txt in this.Controls.OfType<TextBox>())
{
    string text = txt.Text; 
}

(记得加using System.Linq;

您也可以通过foreach(TextBox txt in txtFldNames)访问您的阵列。

您的方法不起作用,因为您正在循环中创建new TextBoxes,而不是引用已经存在的。

我从您的代码示例中推测txtFldNames是一个实例变量(即在表单范围内声明,而不是在表单加载事件内声明)。

因此,在按钮单击处理程序中,您需要使用txtFldNames数组中的TextBox对象,即加载表单时创建的对象。您当前的代码会创建一个新的TextBox对象数组。

例如

foreach(TextBox textBox in tbFldNames) {
    MessageBox.Show(textBox.Text);
}

编辑:

在发布完整的代码之后,您需要将txtFldNames作为一个实例变量。

public partial class classMain : Form
{
    TextBox[] txtFldNames = new TextBox[15]; // <--- Move txtFldNames outside of your frmMain_Load() method.