Move a form while shown

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm trying to move a form while shown changing it's location or top,left
properties without results. I'm doing a simple loop like this (VB):

Dim r as integer
dim frm as new Form1

frm.top=600
frm.left=100
frm.show()
for r= 600 to 0 step -1
frm.top=r
application.DoEvents()
Next

This code executes from a button in other form and don't works. I have tried
insertting a delay inside the loop without success. Sometimes I get strange
behavior, like the form doing partially a movement, then dissapearing...

How should I do this?

TIA

Pucara.
 
Run the movement of the form in a seperate thread and invoke the operation
that contains the actual movement

Something like this:

....
Thread moveForm = new Thread(new ThreadStart(MoveForm));
moveForm.Priority = ThreadPriority.Lowest;
moveForm.Start();
....

private void MoveForm()
{
for (int y = 1000; y > 0; y--)
MoveFormTop(y);
return;
}

private delegate void SetFormTopDelegate(int top);
private void MoveFormTop(int top)
{
if (form.InvokeRequired)
{
form.Invoke(new SetFormTopDelegate(MoveFormTop), new object[] {
top });
}
else
{
//form.SetBounds(x, 0, 0, 0, BoundsSpecified.X);
form.Top = top;
}
}

Gabriel Lozano-Morán
 
Back
Top