1

我无法在 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?
    }
}
4

4 回答 4

1

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

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

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

于 2013-02-07T22:21:22.550 回答
1

给定完整的代码,您可以这样做:

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);
        }
    }
}
于 2013-02-07T22:45:07.833 回答
0

您可以使用Enumerable.OfType来查找您的文本框:

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

(记得加using System.Linq;

您还可以通过foreach(TextBox txt in txtFldNames).

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

于 2013-02-07T22:22:38.633 回答
0

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

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

例如

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

编辑:

发布完整代码后,您需要创建txtFldNames一个实例变量。

IE

public partial class classMain : Form
{
    TextBox[] txtFldNames = new TextBox[15]; // <--- Move txtFldNames outside of your frmMain_Load() method.
于 2013-02-07T22:28:09.250 回答