Add control ontop of another control

  • Thread starter Thread starter PeterB
  • Start date Start date
P

PeterB

Hi!

If I have a combobox on a form, I then create a textbox which takes the
combobox as an argument. The textbox uses the location and size of the
combobox to define it's postion on the form. However, I am unable to get the
textbox to be ontop of the combobox... This should be really simple but I
just can't find the way to do it.. I can see the textbox behind the
combobox...

BringToFront() doesn't work.

regards,

Peter
 
You should be able to call SetChildIndex on the ControlCollection of the
form or container control on which these controls reside. e.g.

Me.Controls.SetChildIndex(textBox1, 0);

The z-order of the controls should be affected by their order in the
controls collection - with 0 being the foremost control. By default new
controls are added to the end of the collection and the back of the z-order

Peter
 
Thanks Peter, that's exactly what I need. I am doing it like this, could
that cause any unexpected behaviour?

this = the textbox, so the code is run in the textbox's constructor.
// Add this instance to the same parent as the combobox

int iCmbIndx = this.Parent.Controls.GetChildIndex( cmb );

int iTxtIndx = this.Parent.Controls.GetChildIndex( this );

if( iCmbIndx < iTxtIndx )

{

this.Parent.Controls.SetChildIndex(this, iCmbIndx);

this.Parent.Controls.SetChildIndex(cmb, iTxtIndx);

}
 
Back
Top