Add control ontop of another control

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
 
P

Peter Foot [MVP]

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
 
P

PeterB

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);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top