How can I programmatically set the Column and Row properties of controls that are dynamically created to be inserted into a TableLayoutPanel?

When I drop a control, such as a shortcut or text block, into a TableLayoutPanel, it has (among many others, of course) the following properties:

Cell.Column Cell.Row Column Row 

... but these controls usually do not have these properties (IOW, Labels and TextBoxes, which are not discarded in TLP, do not have them).

How can I programmatically assign values ​​to these properties (the controls that I want to embed in TLP)?

This is my existing shortcut code:

 . . . lblName = string.Format("label{0}", i); var lbl = new Label() { Name = lblName, Parent = tableLayoutPanelPlatypi, Column = ColNum, // Doesn't compile; Column property not recognized Row = i - 1, // Doesn't compile; Row property not recognized Dock = DockStyle.Fill, Margin = 0, TextAlign = ContentAlignment.MiddleCenter, Text = GettysburgAddressObfuscation() }; 
+4
source share
1 answer

You can add them using the TableLayoutPanel controls. Add Method:

 tableLayoutPanelPlatypi.Controls.Add(lbl, ColNum, i - 1); 

or, as you indicated, each property can be set individually:

 tableLayoutPanelPlatypi.SetColumn(lbl, ColNum); tableLayoutPanelPlatypi.SetRow(lbl, i - 1); 

The form designer adds these properties conveniently for you, but if you look at the constructor file in the control that was added to the TableLayoutPanel, it uses this format above.

+4
source

All Articles