By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,101 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,101 IT Pros & Developers. It's quick & easy.

The Forms in my 'EXE' program seperate when moved

P: 23
Hello Everybody!
my program is made up of several Forms, and once compiled to an exe, if I were to move it across the desktop, the Forms seperate; can this be prevented?

regards
Tomservo
Mar 18 '07 #1
Share this Question
Share on Google+
9 Replies


100+
P: 1,646
Hi. What do you mean when you say they separate?
Mar 19 '07 #2

Expert 5K+
P: 8,434
Hello Everybody!
my program is made up of several Forms, and once compiled to an exe, if I were to move it across the desktop, the Forms seperate; can this be prevented?
I'm guessing you mean that you want the forms to stick together and move around the desktop in unison.

Off-hand, I can't think of any elegant way to accomplish this, but you could try something like a timer control which keeps watch for the .Top or .Left property changing, and moves the other forms to keep them together. I can think of a couple of things you might want to keep in mind...
  • Don't get into an infinite loop. this could happen if your "form has moved" routine moves the form to catch up, and the movement triggers the "form has moved" routine again.
  • Ordinarily you wouldn’t want the timer to be looping too fast and wasting too many cycles, so I would set the Interval fairly long (250 perhaps?). When it spots a move and triggers the “I’m coming too” routine, you could set the timer interval shorter so that things move a bit more smoothly. Once they stop moving, set it back to the original value.
Hm... this is an interesting one. I’m on lunch right now, so I think I’ll throw together a simple trial program.
Mar 19 '07 #3

Expert 5K+
P: 8,434
Okay, I've done that. It's a bit rough, but may be of some interest. The second form actually doesn't snap into position until I drop the first form, but that's probably because I have "show forms while dragging" switched off in my Windows display settings.

If you want to try it, just create a new project, add two forms (default names) then paste this code into the code window for Form1.

Then run it and try dragging form1 around.

Note that it doesn’t have any safeguards against things like re-entering routines.
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. DefLng A-Z
  3.  
  4. Private LatestTop As Single, LatestLeft As Single
  5. Private Const SLOW As Long = 250, FAST As Long = 50
  6.  
  7.  
  8. Private Sub Form_Load()
  9.   With Me
  10.     LatestTop = .Top
  11.     LatestLeft = .Left
  12.     Load Form2
  13.     Form2.Move .Left + .Width, .Top
  14.     Form2.Show
  15.     DoEvents
  16.     .Show
  17.   End With
  18. End Sub
  19.  
  20. Private Sub Timer1_Timer()
  21.   With Me
  22.     If .Top <> LatestTop Or .Left <> LatestLeft Then
  23.       LatestTop = .Top
  24.       LatestLeft = .Left
  25.       Form2.Move .Left + .Width, .Top
  26.       .Timer1.Interval = FAST
  27.     Else
  28.       .Timer1.Interval = SLOW
  29.     End If
  30.  
  31.   End With
  32. End Sub
  33.  
Also, keep in mind that there are probably much better ways of achieving this kind of thing.
Mar 19 '07 #4

100+
P: 1,646
Would this work better in the form click event?
Mar 19 '07 #5

Expert 5K+
P: 8,434
Would this work better in the form click event?
I don't think the form click event fires when you drag the form.
Mar 19 '07 #6

100+
P: 1,646
I don't think the form click event fires when you drag the form.
You're right.
Better to subclass the form window and catch the 'moved' windows message.
Download a code sample here
Mar 19 '07 #7

P: 23
You Guys are Brilliant!
Thankyou!
regards
Tomservo
Mar 19 '07 #8

100+
P: 1,646
You Guys are Brilliant!
Thankyou!
regards
Tomservo
You are welcome. For the record, Killer is brilliant. I am just waiting to be 'found out'.
Mar 19 '07 #9

Expert 5K+
P: 8,434
... For the record, Killer is brilliant...
Ahem... you forgot "modest".

How many times do I have to tell everyone... "brilliant, but modest". :p


Incidentally, thank for the link Will - I'll have to check that out.

(Has anyone tried my example with "show window contents while dragging" on?)
Mar 19 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.