I have a program that I'm writing that connects to a database, calls a new form , and dynamicly creates controls (textbox, and label) for each item so that they can be edited. The problem I'm having, is I don't know how to pass these values back to the main part of my program. I'm just not sure how to access these dynamic controls after they've been created. They are being created durning the OnLoad function. Here is the code:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
for (m_iRow = 0; m_iRow < m_iTotalRows; m_iRow++)
{
int iColumn = 0, iMyColumn = 0;
if (m_iRow == (m_iTotalRows - 1))
{
txtOutsideTxtBox.Text = m_strDetails[m_iRow, iColumn];
m_iRow = m_iTotalRows;
}
else
{
if (m_strDetails[m_iRow, iColumn] != null)
{
label1 = new Label();
label1.Name = "lblAmount" + (m_iRow + 1).ToString();
label1.Text = "Amount:";
tableLayoutPanel1.Controls.Add(label1, iColumn, m_iRow);
iColumn++;
txtBox1 = new TextBox();
txtBox1.Name = "txtAmount" + (m_iRow + 1).ToString();
txtBox1.Text = m_strDetails[m_iRow, iMyColumn];
tableLayoutPanel1.Controls.Add(txtBox1, iColumn, m_iRow);
iColumn++;
iMyColumn++;
label1 = new Label();
label1.Name = "lblUnit" + (m_iRow + 1).ToString();
label1.Text = "Unit:";
tableLayoutPanel1.Controls.Add(label1, iColumn, m_iRow);
iColumn++;
txtBox1 = new TextBox();
txtBox1.Name = "txtUnit" + (m_iRow + 1).ToString();
txtBox1.Text = m_strDetails[m_iRow, iMyColumn];
tableLayoutPanel1.Controls.Add(txtBox1, iColumn, m_iRow);
m_iRowCount++;
}
}
}
}
The Error I'm getting is
The name txtUnit1 does not exist in the current context
My first thought is DUH because it's created in a different function (I'm using a button I call save to pass back
DialogResults.OK
along with the values of each text box. So how do I do this. I suppose I'm needing to be creating the controls else where? Any suggestions?