Results 1 to 16 of 16

Thread: Newbie questions regarding Forms

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    74
    1) I made a program with more than one form, and I noticed that whenever I click on the close button in the upper right corner to close the program, the program doesn't really close but hides itself. Can someone explain why?

    2) What is the best way to switch between forms?

    3) How do I make my forms have scrollbars when I resize the form to a smaller size?

    I greatly appreciate the help gurus! =)

  2. #2
    Addicted Member
    Join Date
    Jul 1999
    Location
    St-Élie d'Orford, Quebec, Canada
    Posts
    133
    Hi,

    1) Do you have functions that never exit like a "For ever" loop or a "While 1" loop in your form ? This may explain why the form never unloads but hides.

    1) After the form closes do you refer to a control on it from another form ? This would cause the unloaded form to load again and give you the impression it never closed...

    2) For the interface its frm.setfocus or frm.show. For the code its frm. but I'am quite sure you already knew this, did I understand your question right ?


    3) Got me there, never had to do this...


  3. #3
    Addicted Member
    Join Date
    Feb 2000
    Location
    London, UK
    Posts
    145
    Hello1
    The problem with exiting is that when you click the x in the upper right corner you unnload the form, but don't close the application.
    Even if all forms are unloaded the applicaiton will still be running.
    To exit the app you must use the command End.
    You can for instance decide which of the forms that shall be unloaded last, and in the event Form_Unload() put the command End.

    As for the second, I use:

    frmSecond.Enabled = True
    frmSecond.Show

    to switch from frmFirst to frmSecond.
    You just use the same code (but with different names) to switch back again.
    If you want you can extend it to

    frmFirst.Enabled = False

    as well, so the user can't use the "wrong" form, depending on what app you're doing.
    In that case, just remember to enable the other form when you unload one.

    As for the scrollbars, I haven't got a clue.
    I don't know if it's possible.

    Good luck,
    Pentax
    Wilhelm Tunemyr,
    Swede in London

    [email protected]

    "Dort, wo man Bücher verbrennt, verbrennt man am Ende auch Menschen"
    Heinrich Heine (1797-1856)

    Pravda vítezi!
    (Truth prevails!)

  4. #4
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    Scrollbars: I have heard that SetWindowLong can add scrollbars, but I do not know.

    Closing app: never use end. It is evil. Its only safe when you need to demolish the app regardless of whether or not it causes GPFs etc. When doing complicated things, End will screw your app, OS, machine, life, etc.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  5. #5
    Addicted Member
    Join Date
    Jul 1999
    Location
    St-Élie d'Orford, Quebec, Canada
    Posts
    133
    I'am with you MLEWIS, I think the END statement can lead to errors and surely leads to "spagetti" code when you start putting those anywhere in the code. Your application should exit when there are no more forms loaded in the forms collection and obviously no more code running in backgroud (loop)...

  6. #6
    Guest
    Use the following to unload all Forms properly.
    Code:
    Sub DestroyForms()
       Dim frm As Form
       For Each frm In Forms
        Unload frm
        Set frm = Nothing
       Next frm
    End Sub
    
    Private Sub Command1_Click()
        'End the project
        DestroyForms
    End Sub

  7. #7
    Addicted Member
    Join Date
    Feb 2000
    Location
    London, UK
    Posts
    145
    Dear me...
    Is it really so terrible to use the End command?
    I've used it an a (rather) complicated app, and I haven't experienced any problems, but maybe I just haven't noticed them.
    What exactly can happen (apart from the risk of being resented by every vb-programmer from here to Calcutta)?
    Pentax
    Wilhelm Tunemyr,
    Swede in London

    [email protected]

    "Dort, wo man Bücher verbrennt, verbrennt man am Ende auch Menschen"
    Heinrich Heine (1797-1856)

    Pravda vítezi!
    (Truth prevails!)

  8. #8
    Addicted Member
    Join Date
    Jul 1999
    Location
    St-Élie d'Orford, Quebec, Canada
    Posts
    133
    Hi Pentax,

    here from the VB help topics (poor references but...)

    The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Code you have placed in the Unload, QueryUnload, and Terminate events of forms and class modules is not executed. Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed. Object references held by other programs are invalidated.

    it always depends of the context of your application. The part that bothers me is that it CLEARS the variables not knowing if they have been saved right (database envir.) if in edit... just little things like that.

    The thing is that when you decide you need to call an END statement I presume you know everything is fine in your application so you can do so...

    Anyways... it the weekend so my brain is swiching OFF right about now !

  9. #9
    Addicted Member
    Join Date
    Feb 2000
    Location
    London, UK
    Posts
    145
    Okay, thanks for clearing it up.
    But, as you wrote, I use End only when I'm sure that's what is needed. And before that, the user gets his/her chance to abort, save files etc.
    So I think I can go on using it (I'll have to take the risk about being resented...)
    Cheers, Pentax
    Wilhelm Tunemyr,
    Swede in London

    [email protected]

    "Dort, wo man Bücher verbrennt, verbrennt man am Ende auch Menschen"
    Heinrich Heine (1797-1856)

    Pravda vítezi!
    (Truth prevails!)

  10. #10
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    End is only evil when you haven't destroyed class objects, cleaned up variables, etc. but if you have made everything tidy its a handy way to demolish an app.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    74

    Wow!

    Wow thanks guys I really learned alot form this thread!

    So let me see if I got this right, to close the app I should clear all variables and objects before I use the end statement right?

    Is there a way to cancel the unload command when you click on the x on the upper right hand side of the form? I want to prompt the user if they really want to quit before I unload all the forms (Thanks Megatron!) and end the app gracefully.

    Now my only problem is the scrollbars for the form. Whenenver I resize the form the objects in the form get clipped. So I figured if I put scrollbars similar to all other window applications I'm golden. Problem is I couldn't so I set my form's Border Style to fixed single. I still want to be able to minimize my program but make it a fixed size. Anyway I can work around this?

    Thanx again guys!

  12. #12
    Guest
    For cancelling the Unload event try this code:

    Code:
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        Dim res As VbMsgBoxResult
        
        res = MsgBox("Are you sure you want to close?", vbYesNo + vbApplicationModal + vbQuestion, "Close program")
        If res = vbNo Then 
            Cancel = True
        else
            ' Unload all forms, collections etc...
        end if
    End Sub
    As for re-sizing try this:

    Code:
    Private Sub Form_Resize()
        Text1.Width = Me.Width / 2
        Text1.Height = Me.Height / 2
    End Sub
    Ofcourse you'll have to do this with every object and you'll have to change the "formula" (the "/ 2" part) to anything to fit your needs, like

    Code:
        Text1.Width=Me.width-1000
    etc...

    Enjoy!


  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    74

    Thanks!

    Thanks RobIII!

    A lot of work for the resize though but it's effective. Thanks again, I greatly appreciate the help

  14. #14
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362

    Re: Thanks!

    Originally posted by gin
    Thanks RobIII!

    A lot of work for the resize though but it's effective. Thanks again, I greatly appreciate the help
    Couldn't you just enumerate your controls in the form and skip having to change them one by one?
    Donald Sy - VB (ab)user

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    74
    Well since I'm just a newbie could you explain how I go about doing that? I'd really like to learn how. Thanks

  16. #16
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    This will only work if all the controls need the exact same resizing performed on them; not easy. here's one idea:

    Dim PrevW, PrevH

    Private Sub Form_Resize()

    On error resume next

    Dim q as control
    Dim deltaw as single
    Dim deltah as single

    deltaw = me.scalewidth / prevw
    deltah = me.scaleheight / prevh

    For Each q in Me.Controls
    q.top = q.top * deltah
    q.left = q.left * deltaw
    q.height = q.height * deltah
    q.width = q.width * deltaw
    Next q

    Prevh = me.scaleheight
    Prevw = me.scalewidth

    End Sub

    This should work.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width