spanky4_3

Hi everyone,

I have a baseform which has 3 controls (a Panel, a splitter and a UserControl) that add to the form's Controls collection. I inherit from this form and add some controls to Panel (not directly to the form's Controls collection). The 3 base controls are docked such that the usercontrol and the splitter are on the right and the panel fills the remaining space. Obviously I want the splitter between the Panel and the usercontrol, which is what shows at design time, however I found at runtime the splitter is on the far right with the usercontrol to the left (making the splitter useless). Debugging this I find that the form's controls collection reorders itself when the form is told to show.

Has anyone else experienced this and have any information as to what is causing the controls collection to re-order. I don't want to constantly check the control order when I call show.

Thanks,

Mark.



Re: Windows Forms General Form controls reorder on show

nobugz

Murky description, seeing the code from the Designer.cs of the two forms might help. For docking to work properly, the order in which controls are added to the form's Controls collection is important. You might have a problem with form inheritance, the base form controls will always be added first. Not much you can do but reorder by using the Controls.SetChildIndex() method in the derived form's constructor, after the InitializeComponent() call.





Re: Windows Forms General Form controls reorder on show

spanky4_3

Hi Nobugz,

Thanks for your reply, sorry my description isn't clear. Simply put, a baseform adds three controls, I derive from this form and add NO controls, upon calling show the 3 controls reorder themselves (well specifically the last 2 controls swap positions in the controls collection). I can debug the form and see that the controls are in the correct order until Show() is called.

Here is a snippet from the designer.vb file:

Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)

Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

Me.ClientSize = New System.Drawing.Size(737, 533)

Me.Controls.Add(Me.Control1)

Me.Controls.Add(Me.Control2)

Me.Controls.Add(Me.Control3)

Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog

So the order I find after calling Show() is Control1, Control3, Control2..





Re: Windows Forms General Form controls reorder on show

nobugz

Okay, one way to get them in a different order is to give them an out-of-sequence TabIndex property. That will override the order of Control.Add() calls.





Re: Windows Forms General Form controls reorder on show

spanky4_3

Hi nobugz, thanks again for your answer, I will try that out.

Some information that I left out (which is probably very important sorry) is that the 2nd and 3rd controls have visible=false set in the designer. Their visibility is controlled by other events. So the form starts up with these 2 controls being invisible. I tried setting visible=true in the designer and the controls work properly (that is don't re-order). I have no idea why having visible=false to start with would cause re-ordering.





Re: Windows Forms General Form controls reorder on show

spanky4_3

OK now this is REALLY strange,

I tried to re-create my situation with a simply test case. I created a new form then added a treeview control dock=right, then a splitter dock=right, then a tabcontrol dock=right then another splitter dock=right and finally a panel (with a button on it) dock=fill. I set visible=false for the treeview and splitter1 in the designer and set the button click to change visible=true for the 2 controls. I found that the form's control collection order was maintained, so seems like nothing to do with visibility.

Next I thought it was perhaps because my first control is a usercontrol. So I created a simple usercontrol (that only had a textbox inside it) and replaced the treeview with this usercontrol. Again I found that the form's control collection order was maintained.

As in my original situation I have a usercontrol that contains a webbrowser, I created a usercontrol with a webbrowser inside it. Then I replaced my previous usercontrol with this new one and as if by magic the form's control collection gets reordered. So I guess the WebBrowser control embedded in the usercontrol is somehow stuffing up the base form's control collection order (HOW ANNOYING!), but at least I might be able to focus my workaround. Am I right in guessing the WebBrowser is still some type of ActiveX control

Mark.





Re: Windows Forms General Form controls reorder on show

spanky4_3

Although I don't understand this I think I found the combination of things that result in this odd behaviour. So in the situation I described in my previous message, if I have a usercontrol with a webbrowser in it and this usercontrol is docked to the right along with a splitter, and then they both have visible=false set in the designer and are made visible at runtime, then the usercontrol swaps its position with the adjacent control when Show() is called.

HOWEVER, if in the usercontrol I set the webbrowser's ScrollBarsEnabled=false then the usercontrol does NOT swap itself. Further, if I want scroll bars, I can set ScrollBarsEnabled=true in the constructor of the usercontrol and order is still maintained, so it appears only if ScrollBarsEnabled=true is in the designer then the problem occurs.

If anyone else ends up with this convoluted situation and behaviour I hope this helps.