How to move a control at run time

  • Thread starter Thread starter Alan T
  • Start date Start date
A

Alan T

I want to move a textbox to the same position as another combobox at run
time, but got compilation error.

I use the Location.X and Location.Y but no luck.
 
Hi Alan,
use the Left and Top properties instead to change the control location.
The reason you cannot say:

myControl.Location.X = 50;

is because Location returns a Point structure, this is passed by value so a
copy is made when the value is returned from the call to the property, so you
would not be assigning to the point structure located inside the control
instance but to a temporary copy that would be lost, in effect this is not an
lvalue.



Mark.
 
An alternative method is to replace the Point in Location with a new
Point, for instance the Location of another control.

textBox1.Location = comboBox1.Location;
 
Morten said:
An alternative method is to replace the Point in Location with a new
Point, for instance the Location of another control.

textBox1.Location = comboBox1.Location;

Agreed. This is what I would do. If you have to make some modifications
to the location, say 10 pixels to the right of the combo box and 4
pixels down, use a construction like:

textBox1.Location = new Point(comboBox.Location.X + 10,
comboBox.Location.Y + 4);

or perhaps it's nicer to read:

textBox1.Location = new Point(comboBox.Left + 10, comboBox.Top + 4);
 
Nvm

The thread title says "run time" but the OP says "compile time"

I didn't read it thoroughly ;)
 

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

Back
Top