Dock order respected at design time, but not always at runtime

Viewed 44

I'm facing a weird and erratic issue in a WinForms project. In a user control I have a toolbar (3rd party control) and an a subclassed RichTextBox.

The toolbar has dock=top, while the RTB has dock=fill. I used "bring to front" on the RTB to avoid Toolbox overlapping RTB.

At design time everything is fine, both in control design and in forms where the control is used:

enter image description here

enter image description here

At runtime I experience an erratic behaviour: the control is always rendered fine as long as I place it on the form without touching any property.

If I change some property value (for example, I have a ShowFont property that I can set to false to hide fonts dropdown, both at design time and at runtime) it sometimes gets screwed and from that moment at runtime the bar overlaps the RTB:

enter image description here

This is strange because there is no code in the control that adds/removes subcontrols, thus altering their order.

1 Answers

In general, this problem is caused by dock=fill.

I don't know why your bring to front fails.

You can try to open the Designer.cs file of the corresponding form and find the statement that adds controls to the form at the end of the InitializeComponent method, such as:

this.Controls.Add(this.RTB);
this.Controls.Add(this.toolbar);

Change the order to:

this.Controls.Add(this.toolbar);
this.Controls.Add(this.RTB);

If it still doesn't work, I suggest you try using anchor.

Related