We help IT Professionals succeed at work.

VB Forms moving around...

sbennetts
sbennetts asked
on
374 Views
Last Modified: 2010-05-01
This is probably a silly question!

How do I set the properties on a VB form so that it is movable and when you move the form, close you application then run the application again, it remembers where you had it last time.

Is that possible?

Thanks
Simon
Comment
Watch Question

Author

Commented:
Sorry, didn't mention VB version 6
Thanks :-)
You'll need to save the info somewhere. Registry, ini file, etc.

Try this:

Public Sub SaveFormPosition(frm As Form)
  SaveSetting App.EXEName, REGKEY_FORM_POSITION, frm.Name & " " & REGKEY_FORM_POSITION_LEFT, CStr(frm.Left)
  SaveSetting App.EXEName, REGKEY_FORM_POSITION, frm.Name & " " & REGKEY_FORM_POSITION_TOP, CStr(frm.Top)
  SaveSetting App.EXEName, REGKEY_FORM_POSITION, frm.Name & " " & REGKEY_FORM_POSITION_WIDTH, CStr(frm.Width)
  SaveSetting App.EXEName, REGKEY_FORM_POSITION, frm.Name & " " & REGKEY_FORM_POSITION_HEIGHT, CStr(frm.Height)
End Sub

Public Sub RestoreFormPosition(frm As Form)

  Dim Str As String
  Dim Left As Long
  Dim Top As Long
  Dim Width As Long
  Dim Height As Long
 
  Str = GetSetting(App.EXEName, REGKEY_FORM_POSITION, frm.Name & " " & REGKEY_FORM_POSITION_LEFT)
 
  If Len(Str) > 0 Then
    Left = CLng(Str)
    Str = GetSetting(App.EXEName, REGKEY_FORM_POSITION, frm.Name & " " & REGKEY_FORM_POSITION_TOP)
    Top = CLng(Str)
    Str = GetSetting(App.EXEName, REGKEY_FORM_POSITION, frm.Name & " " & REGKEY_FORM_POSITION_WIDTH)
    Width = CLng(Str)
    Str = GetSetting(App.EXEName, REGKEY_FORM_POSITION, frm.Name & " " & REGKEY_FORM_POSITION_HEIGHT)
    Height = CLng(Str)
    frm.Move Left, Top, Width, Height
  End If

End Sub

Then call:
  RestoreFormPosition Me
in  your startup form's load event.

call:
  SaveFormPosition ME
in your startup form's unload event.

Sorry, you'll need these too:

Public Const REGKEY_FORM_POSITION As String = "Form Position"
Public Const REGKEY_FORM_POSITION_LEFT As String = "Left"
Public Const REGKEY_FORM_POSITION_TOP As String = "Top"
Public Const REGKEY_FORM_POSITION_WIDTH As String = "Width"
Public Const REGKEY_FORM_POSITION_HEIGHT As String = "Height"
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Perfect, thanks Idle_Mind
Top Expert 2004

Commented:
1. To move a form set the form's Moveable property to True
2. To save the last position:
Private Sub Form_Unload(Cancel As Integer)
'save top and left into registry
SaveSetting "MyAppName", "Startup", Me.Name & "Top", Me.Top
SaveSetting "MyAppName", "Startup", Me.Name & "Left", Me.Left
End Sub
3. To read when sart the app:
Private Sub Form_Load()
'read top left from registry
Me.Top = GetSetting("MyAPPName", "Startup", Me.Name & "Top", "100")
Me.Left = GetSetting("MyAPPName", "Startup", Me.Name & "Left", "100")
End Sub
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.