The Forms in my 'EXE' program seperate when moved

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tomservo
    New Member
    • Mar 2007
    • 23

    The Forms in my 'EXE' program seperate when moved

    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
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Hi. What do you mean when you say they separate?

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Originally posted by Tomservo
      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.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        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.
        Code:
        Option Explicit
        DefLng A-Z
        
        Private LatestTop As Single, LatestLeft As Single
        Private Const SLOW As Long = 250, FAST As Long = 50
        
        
        Private Sub Form_Load()
          With Me
            LatestTop = .Top
            LatestLeft = .Left
            Load Form2
            Form2.Move .Left + .Width, .Top
            Form2.Show
            DoEvents
            .Show
          End With
        End Sub
        
        Private Sub Timer1_Timer()
          With Me
            If .Top <> LatestTop Or .Left <> LatestLeft Then
              LatestTop = .Top
              LatestLeft = .Left
              Form2.Move .Left + .Width, .Top
              .Timer1.Interval = FAST
            Else
              .Timer1.Interval = SLOW
            End If
              
          End With
        End Sub
        Also, keep in mind that there are probably much better ways of achieving this kind of thing.

        Comment

        • willakawill
          Top Contributor
          • Oct 2006
          • 1646

          #5
          Would this work better in the form click event?

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by willakawill
            Would this work better in the form click event?
            I don't think the form click event fires when you drag the form.

            Comment

            • willakawill
              Top Contributor
              • Oct 2006
              • 1646

              #7
              Originally posted by Killer42
              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

              Comment

              • Tomservo
                New Member
                • Mar 2007
                • 23

                #8
                You Guys are Brilliant!
                Thankyou!
                regards
                Tomservo

                Comment

                • willakawill
                  Top Contributor
                  • Oct 2006
                  • 1646

                  #9
                  Oops! Edited wrong message - sorry.

                  Originally posted by Tomservo
                  You Guys are Brilliant!
                  Thankyou!
                  regards
                  Tomservo
                  You are welcome. For the record, Killer is brilliant. I am just waiting to be 'found out'.

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by willakawill
                    ... 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?)

                    Comment

                    Working...