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

时间:2013-02-07 22:19:17

标签: c# arrays winforms textbox onload-event

我无法在onload事件中检索从运行时生成的数组文本框的值。这是代码。

来自form 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 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分: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);
        }
    }
}

答案 2 :(得分:0)

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

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

(记得添加using System.Linq;

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

由于您在循环中创建new TextBox而不是引用已存在的TextBox,因此您的方法不起作用。

答案 3 :(得分:0)

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

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

e.g。

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.