Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Good day all.
I am currently working on a little side project where I want to create a database via a windows forms application.
I have managed to create the database fine from the application, my problem comes in when I need to create the tables.
On the application I dynamically create text boxes (on link label click events) for the table name and column names. This part works fine, I create each text box with a unique name in an attempt to create a bit more clarity when it comes to creating the tables with their columns.
My problem comes in when I need to select all the column text boxes for the table i need to create.
I have found some code(as in the create_click) that I tried to see if i could do this, but it ends up only recognizing the last table text box added and not any of the others.
Now my question is. Is there any way for me to be able to do this or do I need to totally rethink my approach to this?
I am aware that the table insert code is not done yet, that is not my problem, I just simply want the text from the Column text boxes to be linked under each Table text box.
Its hard to explain exactly what I need to do I know, but if you need any more clarification on this please ask.
My code as to what I have done so far is below.(This is the entire form code so you can copy and reproduce the application for testing)

I thank you in advance and sorry for the long post.

C#
private void AddColumn_Click(object sender, EventArgs e)
       {
           if (i == 0)
           {
               i = 1;
           }
           //Label
           lblColumns = new Label();
           Column = new TextBox();
           Types = new TextBox();
           nulls = new CheckBox();
           notnulls = new CheckBox();
           lblColumns.Text = "Column " + i;
           lblColumns.Location = new Point(345, Table.Bottom + (i * 25));
           lblColumns.AutoSize = true;
           //TextBox
           //Column = new TextBox();
           Column.Location = new Point(lblColumns.Location.X + 60, lblColumns.Location.Y);
           Column.Size = Columns.Size;
           Column.Name = Table.Name + Column.Name + "Coulmn" + i;

           //Combobox
           Types.Location = new Point(Type.Location.X, Column.Location.Y);
           Types.Size = Type.Size;
           Types.Name = "Type" + i;

           //Radio Buttons
           nulls.Text = "NULL";
           nulls.Location = new Point(Null.Location.X, Column.Location.Y);
           nulls.AutoSize = true;
           nulls.Checked = false;

           nulls.Name = "nulls" + i;

           notnulls.Text = "NOT NULL";
           notnulls.Location = new Point(NotNull.Location.X, Column.Location.Y);
           notnulls.AutoSize = true;
           notnulls.Checked = true;

           notnulls.Name = "notnulls" + 1;

           CheckBox IDEN = new CheckBox();
           IDEN.Text = "IDEN";
           IDEN.Name = "IDEN" + i;
           IDEN.Location = new Point(Types.Location.X - 60, Types.Location.Y);
           IDEN.AutoSize = true;
           IDEN.Checked = false;
           this.Controls.Add(IDEN);

           this.Controls.Add(nulls);
           this.Controls.Add(notnulls);
           this.Controls.Add(lblColumns);
           this.Controls.Add(Column);
           this.Controls.Add(Types);

           if (i == 1)
           {
               IDEN.Checked = true;
           }

           i++;

           AddTable.Visible = true;


           AddTable.Location = new Point(Tables.Location.X + 154, Column.Location.Y);
           AddColumn.Location = new Point(Columns.Location.X + (Column.Width / 3), Column.Bottom + 5);
           Create.Visible = true;
           Create.Location = new Point(this.Width / 2 - Create.Width / 2, AddColumn.Bottom + 20);
           this.AutoScrollPosition = new Point(Math.Abs(this.AutoScrollPosition.X), this.VerticalScroll.Maximum);

       }
       private void AddTable_Click(object sender, EventArgs e)
       {

           lblTables = new Label();
           Table = new TextBox();
           Table.Name = Table.Name + "Table" + j.ToString();
           lblTables.Text = "Table " + j;
           Table.Size = Tables.Size;

           if (i == 1)
           {
               lblTables.Location = new Point(10, Columns.Bottom + (j * 25));
               Table.Location = new Point(lblTables.Location.X + 60, lblTables.Location.Y);
           }

           if (i > 1)
           {
               lblTables.Location = new Point(10, Column.Bottom + (j * 25));
               Table.Location = new Point(lblTables.Location.X + 60, Column.Bottom + (j * 25));
               AddColumn.Location = new Point(Columns.Location.X + 154, Table.Location.Y);
           }
           this.Controls.Add(Table);
           this.Controls.Add(lblTables);
           i = 0;
           j++;
           AddColumn.Visible = true;
           AddTable.Visible = false;
           Create.Visible = false;
           this.AutoScrollPosition = new Point(Math.Abs(this.AutoScrollPosition.X), this.VerticalScroll.Maximum);


       }
       private void Create_Click(object sender, EventArgs e)
       {
           foreach (Control ctrl in this.Controls)
           {
               if (ctrl.Name.Contains(Table.Name))
               {
                   MessageBox.Show(ctrl.Name);
               }
           }
       }
Posted
Updated 4-Jun-13 19:37pm
v2
Comments
Sunasara Imdadhusen 4-Jun-13 5:50am    
Do not post your entire code over here. post only error or problematic part
BulletVictim 4-Jun-13 5:54am    
Unless it is a problem for you to load the page. I am not bothered to share my code, Like I said this is just a side project I'm working on, and the reason my entire code is posted is to help clarify what is going on.
Sunasara Imdadhusen 4-Jun-13 5:58am    
You are correct, but if your question or code is to long then it will less chances to get correct answer from the experts. instead of copy past entire code with variables and property post only that logic you face the problem
BulletVictim 4-Jun-13 6:02am    
I see your point, but this is just a shot in the dark any way.
Maciej Los 4-Jun-13 16:52pm    
I agree with Sunasara Imdadhusen. Sorry...
This is not well framed question. Please, clarify your problem and remove unnecessary code (it's useless!).
Use "Improve question" widget.

1 solution

If I understand your question, your issue is that you are referencing a global variable that is being overwritten each time a new object is created. There is no variable maintaining a reference to all the objects created, like a List<t></t> or something. So when you loop through the controls on the form, it searches for the Table.Name, and Table holds only the latest object created, none of the previous ones. You would probably need to extract a list of all the table names on the form, then looping through that list, you could loop through the controls to find objects that contained the table name.

C#
private void Create_Click(object sender, EventArgs e)
{
     foreach (Control ctrl in this.Controls)
     {
          if (ctrl.Name.Contains(Table.Name)) // <-- references a global variable
          {
               MessageBox.Show(ctrl.Name);
          }
     }
}


Having said that, you would probably be better served to rethink the process of dynamically building the tables and columns, including the type and scope of the variables, so that the association would be maintained as the creation occurred.
 
Share this answer
 
Comments
BulletVictim 5-Jun-13 2:56am    
Great thanks I knew I missed something like that somewhere. I globally declared a List<string> and added the Table.Name value to it when the table textbox is created.
On the Create_Click event I added another for loop that goes through the List<string> and then the if is contained within the second for loop.(contents being the List<string>). I know this is kinda sloppy coding and I will have to rethink the entire way of doing this, but your advise really helped. Thank you again

private void Create_Click(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
foreach (string cont in contents)
{
if (ctrl.Name.Contains(cont) && ctrl.Name != cont)
{
MessageBox.Show(ctrl.Name);
}
}
}
}
JOAT-MON 5-Jun-13 3:06am    
Nice, glad it helped :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900