Click here to Skip to main content
15,072,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I want some help.
I want to get the form control which I have added in the tablelayoutpanel
using this code.
DataTable dtDashboardForms = objClsDataAccess.GetDataTable("select * from advt_dashboard_custom");
                if (dtDashboardForms.Rows.Count > 0)
                    for (int i = 0; i < dtDashboardForms.Rows.Count; i++)
                            Form objForm = OpenForm(dtDashboardForms.Rows[i]["formloc"].ToString());
                            tblWidgetContainer.Controls.Add(objForm, Convert.ToInt32(dtDashboardForms.Rows[i]["column"]), Convert.ToInt32(dtDashboardForms.Rows[i]["row"]));
                            objForm.Dock = DockStyle.Fill;

public Form OpenForm(String formName)
            Form oForm = null;
                Type type = Type.GetType(formName, true);
                Form obj = (Form)Activator.CreateInstance(type, ObjfrmCommonScreen);
                obj.MdiParent = ObjfrmCommonScreen;
                oForm = obj;
            return oForm;

And after this all my Forms are added in the tablelayoutpanel.
And when I close any form through the tablelayoutpanel, I call this function on FormClosing event of the form:

public void Reset(Form obj)
            TableLayoutPanel table = (TableLayoutPanel)this.Controls.Find("tblWidgetContainer", true).First();

            int rowNumber = table.GetRow(obj);
            int colNumber = table.GetColumn(obj);

            for (int i = 0; i < table.RowCount; i++)
                Form newForm = (Form)table.GetControlFromPosition(colNumber, i + 1);
                if (newForm != null)
                    table.Controls.Add(newForm, colNumber, rowNumber);

I am able to the row number and column number of the closing form using
int rowNumber = table.GetRow(obj);
            int colNumber = table.GetColumn(obj);

And want to get the form at row+1 rownumber in the column,so the found form should take the place of the closing form.
but not able to get the form from this code:
Form newForm = (Form)table.GetControlFromPosition(colNumber, i + 1);

I used this code before and also able to get the control but that was the case of control.Not for the form.

So please help me to get the form object from the tablelayoutpanel column.

Hope you understand the question.

Updated 10-Jan-13 22:25pm
Simon_Whale 13-Dec-12 6:58am
with your "But not able to get the form from this code"

is there any error message?
is it not working how you would expect it?
leonidasvijay 13-Dec-12 23:55pm
Actually I want to get the form from the colnumber and row number.
But using this code I m not able to get the form.
Form newForm = (Form)table.GetControlFromPosition(colNumber, i + 1);

But in case of a control rather than the form, this code works properly.
Jibesh 17-Dec-12 17:16pm
i tried you code and for me i can get the Form from a cell. I used a fixed row and column number to test my code.

probably you can debug in VisualStudo for what Column and Row index the control returns null. double check you have Form object inside the tablelayourpanel.controls list for the column and row index you are using.
Jibesh 28-Dec-12 1:31am
did you check my comments?

1 solution

What are you trying to achieve here ?

If you want to dispose your form object and remove from the tableLayout panel
you don't need iterate the tableLayout for finding the form object again use the incoming parameter to dispose it.
To remove the same control from your tableLayout panel you can simply call
TableLayoutPanel table = (TableLayoutPanel)this.Controls.Find("tblWidgetContainer", true).First();

no need to query the table again.

interestingly there is another way to get the Cell Position of your control in TableLayoutPanel
TableLayoutPanelCellPosition pos = table .GetCellPosition(obj)
//pos.Row Gives you the Row Index
//pos.Column gives you the Column Index 
leonidasvijay 13-Dec-12 23:59pm
When I click to close the form of the particular row and column, then the form exist just below at row+1 rownumber should take the closing form position in the table.
That's why I want to get the form at the row+1 row.And I m disposing only the closing form.Not that form which I want to get.
Jibesh 14-Dec-12 0:22am
i dont see row+1 code anywhere in the shared code. can you share the FormClosing subscription code...
Jibesh 14-Dec-12 0:39am
Ok i think i have some clue, when you remove a control from the panel the row count automatically reduced so when you tried to use rowCount + 1 there wont be any control in that location since its automatically adjusted to the next cell. try to debug what is inside the adjacent cell. may be you can try with same rowNumber or rowNumber-1

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