Click here to Skip to main content
11,804,008 members (80,708 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# C#4.0
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.

Posted 12-Dec-12 22:49pm
Edited 10-Jan-13 22:25pm
Simon_Whale at 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 at 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 at 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 at 28-Dec-12 1:31am
did you check my comments?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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 at 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 at 14-Dec-12 0:22am
i dont see row+1 code anywhere in the shared code. can you share the FormClosing subscription code...
jibesh at 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)

  Print Answers RSS
0 OriginalGriff 430
1 CPallini 320
2 Sergey Alexandrovich Kryukov 234
3 CHill60 230
4 Suvendu Shekhar Giri 200
0 OriginalGriff 3,000
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,755
4 Richard MacCutchan 1,187

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100