I have a class which derived from the TableLayoutPanel. This class makeup the tabel corrects (grid 3x8) and add some checkboxes in some cells. All this is done by overriding the function InitLayout().
public class TableLayoutPanelHours : TableLayoutPanel
{
protected override void InitLayout()
{
RowCount = 3;
ColumnCount = 8;
// Set some column and row styles
RowStyles[0].SizeType = SizeType.Percent;
RowStyles[0].Height = (100 / RowCount);
// ... etc ...
// ... create checkbox with the name checkbox1
Controls.Add(checkbox1, 1, 1); // Put in cell 1x1
// ... etc ...
}
}
After building, the Control is available from the Toolbox of VS2010.
Then, putting the control on a simple Windows Form, some things are happen that I don't userstand: - the control is not makeup yet during design mode. The TableLayoutPanel is displaying the default 2x2 grid and checkboxes are on a strange place. After runtime, the control is displaying correctly (3x8 grid with Checkb开发者_如何转开发ox on the correctly places) - And: in the InitializeComponent() of the Form, I see these lines appears:
//
// tableLayoutPanelHours1
//
this.tableLayoutPanelHours1.ColumnCount = 8;
this.tableLayoutPanelHours1.ColumnCount = 3;
this.tableLayoutPanelHours1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
// ....
I have excepted that these lines are not visible in the InitializeComponent() of the Windows Forms, but why is this happen?
Thanks.
You are doing battle with the designer for TLP. It is a custom one, as you can clearly see by the way it behaves in design mode. Using the InitLayout() method to initialize the TLP is not correct, it's too late. You should do it in the constructor instead.
But that still won't get you what you want, the TLP designer hard-codes the number of rows and columns when it initializes to 2x2. And it can't handle code changing it at design time (like you did in InitLayout), there are no events to listen to. You will have to give up on that designer.
Should be okay since you already initialize the TLP the way you want it. Use the [Designer] attribute to fall back to ControlDesigner. If that's hardship for your control then you will have to create your own designer. Use Reflector to look at the internal TableLayoutPanelDesigner class to get a start on that. Beware that it isn't a simple designer.
You see those property assignments in InitializeComponent because the [DefaultValue] attribute on those properties declares a different default. You can fix that by adding a private ShouldSerializeRowCount (and ColumnCount) method to your class. Return false to prevent the property from being serialized.
精彩评论