Dock Control programmatically



I get 2 strange effects using the appended small sample.

The Code has a main form and a button. Using the button, by code I create a new label (with incremented text) and dock it on the left side of the form.

  1. The label is created, docked to the left. When the next label is created, it pushes the existing docked label to the right and docks itself to the left.

So that after some click I get labels ordered : 987654321 were I expect to read 123456789.


2. After some button clicks the program is no more responsive. Clicking the button does nothing, even placing a break point in the code.

The count of labels you are able to create is related to the width of the created label. Reducing it’s width allows to get some more created before becoming unsresponsive.


If a label is first docked on top, it will also be pushed to the right at each creation.

Same behavior for all major browsers.


  • You must to post comments


I didn’t knew that Z-order story, thanks for this training :).

For point 2 the solid border makes it obvious (the height of the control is the docked height !!).

Now it works like a charm 🙂

For other users interested by the case, see the remark about the placement of this.Controls.SetChildIndex(Lbl1, 0)

private void button1_Click(object sender, EventArgs e)
Label Lbl1 = new Wisej.Web.Label();
Lbl1.Text = BtnIdx.ToString();
Lbl1.BorderStyle = BorderStyle.Solid;
Lbl1.Dock = DockStyle.Left;
//Needs to be ordered AFTER adding to avoid error ‘child’ is not a child control of this parent.
this.Controls.SetChildIndex(Lbl1, 0);

BtnIdx += 1;

Thanks… Super support, super product, long life to both.

  • You must to post comments

Hi Eric,

I run the sample and it looks correct. You can try the same in WinForms. This is what happens:

  1. When you add the label docked to the left, it docks to the left-most position pushing the other labels to the right. Docking uses the controls in their z-order and the z-order is the opposite of the control order in the collection, we kept it to be compatible with WinForms. You can try that in Winforms and get the same result. If you add this to your code you can see the label’s border: Lbl1.BorderStyle = BorderStyle.Solid.  If you add this.Controls.SetChildIndex(Lbl1, 0) you will reverse the order and dock from the left outward.
  2. The unresponsive issue is simply the button gradually being covered by the labels and at one point the clicks go to the labels. Add this.button1.BringToFront() to make sure it’s in front.



  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.