borderAndreaVB free resources for Visual Basic developersborder

AndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincani

AndreaVB Home | News Home | Forum Home | Downloads | Register | Search | PM | Profile

Previous Topic (binary file read and write in VB)Next Topic (XP SKIN) New Topic Post Reply
AndreaVB OnLine : VB General : closing forms
Poster Resource
confusedbrat
Level: Sage

Registered: 29-08-2005
Posts: 64
icon closing forms

Hey ppl

i have a couple of questions....

1. i created an exe with many forms in it. i have the buttons for all the forms in the main form and just show and hide them. when i close the exe program from the main page, the program is closed but then when i close it from other pages, though it is closed, when i go to the task manager, it is still running. Is there a way to stop the program from other pages also.

2. when i hit a drop down button with some commands inside it, the first time when i do that it just comes and goes and then the second time it stays. guess i am not clear. the first time it just drops down and rolls back immediately. when i hit it the second time it drop downs and stays open. this happens to all the buttons which has somethin inside the combo_click.

Please help me out.

thanx
brat

11-12-2005 at 07:54 PM
View Profile Send Email to User Show All Posts | Add Comment
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: closing forms

Hello.
1) I suggest you to create in a module a public routine like following:
Public Sub CloseAllForms(CallingForm As Form)
    Dim frm As Form
    For Each frm in Forms
        If Not frm.Name = CallingForm.Name Then Unload frm
    Next frm
End Sub
Then, you can implement a feature to call it from any form you need. Just call it as following
Private Sub btnCloseAll_Click()
    ' unload any other open form first
    CloseAllForms Me
    ' at the end, unload the calling form (it has to be unloaded after all the others)
    Unload Me
End Sub

The working criteria is to surf the Forms collection, that's containing all the loaded forms. There are several ways for doing it. An alternative way is by referring to the Forms.Count property.
Private Sub Form_Unload()
    Dim i As Long
    For i = Forms.Count - 1 To 0 Step -1
        Unload Forms(i)
    Next i
End Sub
You can use this second method if you put the CloseAll feature only into one single form, and always go to this point to stop the entire program run. In this case, you have to unload all the forms, including the calling form, and the routine above does so.

When dealing with an MDI project, you can just unload the MDIParent form and all the non-MDIChild forms.

2) It could depend on what it happens during dropdown. Say, if you add some records without deleting the existing ones, and maybe you re-compute some data, requery some other combos and controls, it's normal if it takes time. The more time is needed because now the records are more than before.
But we can't know it, only trying to figure it out. You only know what you have put down in code.

Hope it helps

____________________________
Real Programmer can count up to 1024 on his fingers

11-12-2005 at 09:01 PM
View Profile Send Email to User Show All Posts | Add Comment
confusedbrat
Level: Sage

Registered: 29-08-2005
Posts: 64
icon Re: closing forms

Hey

thanx for the reply, i understand what you do, but got couple of questions. What is the sub btncloseall_click . is that a button that i need to create to close the application. actually i was askin abt the X button on the right top. I want to close all forms when they hit that button. How can i do that.

thanx
brat

11-12-2005 at 09:33 PM
View Profile Send Email to User Show All Posts | Add Comment
Dave Green
Level: Professor


Registered: 20-10-2005
Posts: 90
icon Re: closing forms

Hi cb
If you have a lot of forms open (although hidden) you may be using up a lot of system resources. Is it critical they are just hidden, or can they be unloaded whenever the user has finished looking at them? Normally it is better to unload a form rather than hiding it, unless there is information on it that took a while to load etc. (it is visually slower to load a form than to switch its visibility).
It seems from what you say that the exe is still running after you close all the windows. Have you got an End statement in your code? Without (at least) one, it is very possible to close everything visible, but meanwhile your program is still merrily looping (or idling) in the background eating resources. If it's an mdi interface and you have a sub Main(), try sticking an End in the mdi_unload event.

____________________________
While Breath.Count>0
       Live(gbRelax)
Wend

11-12-2005 at 09:58 PM
View Profile Send Email to User Show All Posts | Add Comment
Dave Green
Level: Professor


Registered: 20-10-2005
Posts: 90
icon Re: closing forms

Hi again
Regarding your 2nd question about combos, that's a bit strange, combos should stay open until you shift focus, re-click on the dropdown button or click an item in the dropdown list. Check to see if you set the focus to another control in your gotfocus event as that could cause this problem.

____________________________
While Breath.Count>0
       Live(gbRelax)
Wend

11-12-2005 at 10:09 PM
View Profile Send Email to User Show All Posts | Add Comment
confusedbrat
Level: Sage

Registered: 29-08-2005
Posts: 64
icon Re: closing forms

hey

thanx for ur idea...

i have somethin like this

sub btn_click

first.hide
second.show

end sub


how do i change it if i want to unload

just put

sub btn_click
second.show
unload me
end sub

is it how i do?

thanx

11-12-2005 at 10:59 PM
View Profile Send Email to User Show All Posts | Add Comment
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: closing forms

quote:
confusedbrat wrote:
.... actually i was askin abt the X button on the right top. I want to close all forms when they hit that button....
...so, you want to unload all forms when unloading a certain one.

Put the public routine in a module...
Public Sub CloseAllForms(CallingForm As Form)
    Dim frm As Form
    For Each frm in Forms
        If Not frm.Name = CallingForm.Name Then Unload frm
    Next frm
End Sub


... then put the call to the CloseAllForms routine in form's Unload event
Private Sub Form_Unload()
    ' unload any other open form first
    CloseAllForms Me
    ' at the end, unload the calling form (it has to be unloaded after all the others)
    Unload Me
End Sub

If you do it, say, in Form1, Form2 and Form3, when you close any of these three forms you unload the whole application. I mean, you can't just unload none of these three forms without closing the whole app, so you better choose carefully what forms you have to put the call in: you should put the call only in the last form to unload, so this form should always be the same. Usually I design a main form and prevent user to close it. I put the Exit feature there, and all the other forms point to this one. Every other form can only unload itself, but when the main one is unloaded every other one is unloaded too. However, it's just a hint.

Hope it helps


____________________________
Real Programmer can count up to 1024 on his fingers
11-12-2005 at 11:01 PM
View Profile Send Email to User Show All Posts | Add Comment
confusedbrat
Level: Sage

Registered: 29-08-2005
Posts: 64
icon Re: closing forms

hey

on my second question, for example i have this

Private Sub Combo1_Click()
Combo2.Clear
List7.Clear
List8.Clear
List9.Clear
List10.Clear
List11.Clear
List2.Clear
List12.Clear
Combo5.Clear
List1.Clear
SQLStr9 = "Select DISTINCT fname from finalweeksel where suppliercode = " + "'" + Combo1.Text + "'"

Set datecalc = OpenDatabase(App.Path & "\datecalc.mdb")
Set RecSet9 = datecalc.OpenRecordset(SQLStr9)
While Not RecSet9.EOF()

    Combo2.AddItem (RecSet9.Fields(0))
    RecSet9.MoveNext
Wend
RecSet9.Close
datecalc.Close
SQLStr1 = "Select suppliername, suppliercode from supplier where suppliercode = " + "'" + Combo1.Text + "'"
Set dn = OpenDatabase(App.Path & "\datecalc.mdb")
Set RecSet1 = dn.OpenRecordset(SQLStr1)
txtName.Text = RecSet1.Fields(0)
RecSet1.Close

End Sub


when i click on this drop down the first time, i can see all the items in the drop down but it roll backs immediately in a second and then when i click it the next time it stays. is there somethin wrong i do.

thanx
brat

11-12-2005 at 11:02 PM
View Profile Send Email to User Show All Posts | Add Comment
steve_w
Level: Moderator


Registered: 18-04-2003
Posts: 1156
icon Re: closing forms

Can't see anything that stands out, I would suggest commenting blocks of the code out to see if you can find which line is causing you grief.

Steve  

12-12-2005 at 09:28 AM
View Profile Send Email to User Show All Posts | Add Comment
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 891
icon Re: closing forms

Hi,
Just a small note. You should not use End keyword in your applications.
This keyword can lead to memory leaks and does not release memory/resources back to windows.
VB will automatically "end" when all forms are unloaded or the Sub Main is completed.
Regards,
Kieron


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

12-12-2005 at 10:22 AM
View Profile Send Email to User Show All Posts | Add Comment
Dave Green
Level: Professor


Registered: 20-10-2005
Posts: 90
icon Re: closing forms

I didn't know that Kieron. Thanks for the info.
Dave

____________________________
While Breath.Count>0
       Live(gbRelax)
Wend

12-12-2005 at 04:42 PM
View Profile Send Email to User Show All Posts | Add Comment
MaxMouse
Level: Protégé

Registered: 04-01-2006
Posts: 6
icon Re: closing forms

if you want to close all forms on exit of the main application

just put:

set me = nothing


in the unload section of each of your forms

and in the main form (used to load all other forms)
put:

end


You don't really need to worry too much about setting all the other forms to nothing, its recommended but not entirely needed. so basically on your main form's unload sub just stick an "End"

04-01-2006 at 01:35 PM
View Profile Send Email to User Show All Posts Visit Homepage | Add Comment
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 891
icon Re: closing forms

Hi,
Just a comment to MaxMouse.
Pls see my earlier comment about using the End keyword. Also note that setting Me=nothing has no effect. This is because ME does not reference itself so will not have an effect on the dereference count. Unload Me will have an effect, but that is also limited to if you have a reference to the form elsewhere. If you do, then any subsequent call of a property/method on this form will reinstantiate it.
Regards,
Kieron


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

04-01-2006 at 11:10 PM
View Profile Send Email to User Show All Posts | Add Comment
AndreaVB OnLine : VB General : closing forms
Previous Topic (binary file read and write in VB)Next Topic (XP SKIN)New Topic Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder