I'm working on a Windows Form Application in Visual Studio 2010.
I have a Database that I am using to pull data to dynamically create checkboxes inside a loop. In between each of these, I'm creating a child loop to create more checkboxes from a sorted DataSet. At the end of each of the internal loops, I'm creating a TextBox and a Button to bring up a new form which requires 2 parameters. It needs to get a current value of a variable set on the parent loop, and the current text of the TextBox.
This is the code for the loops:
int count = (int)CategoryTableAdapter.ScalarQuery();
int c = 1; // textbox name increment
int x = 1; // category checkbox name increment
int y = 50; // point
int z = 50; // point
int tabCount = 1;
//
// New Loop to Create New CheckBox for each Category
//
for (int i = 0; i < count; i++)
{
string categoryName =
Convert.ToString(this._projectmanager_dbDataSet.category.Rows[i]["name"]);
Int64 categoryId =
Convert.ToInt64(this._projectmanager_dbDataSet.category.Rows[i]["id"]);
CheckBox checkBox1 = new System.Windows.Forms.CheckBox();
this.panel2.Controls.Add(checkBox1);
checkBox1.AutoSize = true;
checkBox1.Location = new System.Drawing.Point(y,z);
checkBox1.Name = "categoryCheckBox" + x;
x++;
checkBox1.Size = new System.Drawing.Size(80, 17);
checkBox1.TabIndex = tabCount;
checkBox1.Text = categoryName;
checkBox1.UseVisualStyleBackColor = true;
z = (z + 20);
tabCount++;
if (checkBox1.Checked == true)
{
int count2 = (int)ActivityTableAdapter.ScalarQuery(categoryId);
y = (y + 30);
// Create sorted DataTable from activity with correct Catery ID
DataTable sortedActivityDataTable =
_projectmanager_dbDataSet.activity.Clone();
string strExpr = ("category_id == " + categoryId);
string strSort = "name ASC";
DataRow[] results =
_projectmanager_dbDataSet.activity.Select(strExpr, strSort);
foreach (DataRow dr in results)
sortedActivityDataTable.ImportRow(dr);
DataRow[] foundrows =
_projectmanager_dbDataSet.activity.Select(strExpr, strSort);
// Loop to create Activity CheckBoxes for each Activity for a Category
for (int e = 0; e < count2; e++)
{
string activityName = sortedActivityDataTable.Rows[e]["name"].ToString();
string activityIdA = sortedActivityDataTable.Rows[e]["id"].ToString();
Int64 activityId = Convert.ToInt64(activityIdA);
CheckBox checkbox1 = new System.Windows.Forms.CheckBox();
this.panel2.Controls.Add(checkbox1);
checkbox1.AutoSize = true;
checkbox1.Location = new System.Drawing.Point(y, z);
checkbox1.Name = "activityCheckBox" + d;
d++;
checkbox1.Size = new System.Drawing.Size(80, 17);
checkbox1.TabIndex = tabCount;
checkbox1.Text = activityName;
checkBox1.UseVisualStyleBackColor = true;
z = (z + 20);
tabCount++;
}
y = (y - 30);
//
//Creat A TextBox to add a New activity During Each category
//
TextBox newActivityTextBox = new System.Windows.Forms.TextBox();
newActivityTextBox.Location = new System.Drawing.Point(y, z);
newActivityTextBox.Name = ("newActivityTextBox" + i);
c++;
newActivityTextBox.RightToLeft = System.Windows.Forms.RightToLeft.No;
newActivityTextBox.Size = new System.Drawing.Size(250, 20);
newActivityTextBox.TabIndex = tabCount;
newActivityTextBox.Text = "<New Category>";
this.panel2.Controls.Add(newActivityTextBox);
tabCount++;
//
// New Activity Button
//
Button newActivityButton = new System.Windows.Forms.Button();
panel2.Controls.Add(newActivityButton);
newActivityButton.Location = new System.Drawing.Point((y + 275), (z - 1));
newActivityButton.Name = ("newActivityButton" + i);
newActivityButton.Size = new System.Drawing.Size(75, 23);
newActivityButton.TabIndex = 8;
newActivityButton.Text = "Add Activity";
newActivityButton.UseVisualStyleBackColor = true;
newActivityButton.Click += new System.EventHandler(newActivityButton_Click);
z = (z + 20);
}
}
And here is the basic code for the eventhandler:
public void newActivityButton_Click(object sender, EventArgs e)
{
this.categoryTableAdapter.Fill(this._projectmanager_dbDataSet.category);
NewActivityForm1 frm = new NewActivityForm1(newActivityTextBox.Text, categoryId);
frm.Show();
}
This however, doesn't even recognize the the newActivityTextBox or the variable "CategoryId" (created on line 12). What I need to accomplish is to not only have it recognize these values, but pull the correct values for them at the time that the dynamic controls are created.