We help IT Professionals succeed at work.

Manually creating a Table Layout Panel

2,424 Views
Last Modified: 2012-08-13
Hi, can someone help me out with this one?
I'm trying to manually create a table on a custom user control.
Here's my code,

        private void button1_Click(object sender, EventArgs e)
        {
            // Create new table layout
            TableLayoutPanel tbl = new TableLayoutPanel();

            // Set table borderstyle
            tbl.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;

            // Set number of columns
            tbl.ColumnCount = 8;

            // Set number of rows
            tbl.RowCount = 1;

            // Set growstyle
            tbl.GrowStyle = TableLayoutPanelGrowStyle.AddColumns;

            // Set padding
            tbl.Padding = new Padding(3, 3, 3, 3);

            // Set margin
            tbl.Margin = new Padding(0, 0, 0, 0);

            // Row 0
            tbl.RowStyles[0].SizeType = SizeType.Percent;
            tbl.RowStyles[0].Height = 100;

            // Column 0 - Javascript icon
            tbl.ColumnStyles[0].SizeType = SizeType.Absolute;
            tbl.ColumnStyles[0].Width = 22;

            // Column 1 - Status label
            tbl.ColumnStyles[1].SizeType = SizeType.Percent;
            tbl.ColumnStyles[1].Width = 100;

            // Column 2 - Progress Bar
            tbl.ColumnStyles[2].SizeType = SizeType.AutoSize;

            // Column 3 - User
            tbl.ColumnStyles[3].SizeType = SizeType.AutoSize;

            // Column 4 - Ip
            tbl.ColumnStyles[4].SizeType = SizeType.Absolute;
            tbl.ColumnStyles[4].Width = 94;

            // Column 5 - SSL
            tbl.ColumnStyles[5].SizeType = SizeType.Absolute;
            tbl.ColumnStyles[5].Width = 20;

            // Column 6 - GPRS
            tbl.ColumnStyles[6].SizeType = SizeType.Absolute;
            tbl.ColumnStyles[6].Width = 20;

            // Column 7 - LAN
            tbl.ColumnStyles[7].SizeType = SizeType.Absolute;
            tbl.ColumnStyles[7].Width = 20;

            // Add table layout panel
            this.Controls.Add(tbl);

            // Position the table
            //tbl.Location = new Point(0, 0);

           
        }

When I click my button I'm getting the following error:

System.ArgumentOutOfRangeException was unhandled
  Message="Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"
  Source="mscorlib"
  ParamName="index"
  StackTrace:
       at System.Collections.ArrayList.get_Item(Int32 index)
       at System.Windows.Forms.TableLayoutStyleCollection.System.Collections.IList.get_Item(Int32 index)
       at System.Windows.Forms.TableLayoutRowStyleCollection.get_Item(Int32 index)
       at ctl.stsMain.button1_Click(Object sender, EventArgs e) in D:\Programming\Cafe_programming\Controls\ctl\ctl\stsMain.cs:line 148
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at testapp.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Any idea what's going wrong here? Tnx in advance!
Comment
Watch Question

Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
You need to instantiate a new style before accessing if from its index. Use the Add method mentioned by Jim before accessing it by the index

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.