473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Closing all forms

I found this nifty routine that closes a form one by one every time it
is called. However, I need to hide them instead of closing them. Is
there a way to alter the code? I assume it has something to do with
this line here...
PostMessage(i, CInt(&H10), vbNullString, vbNullString)

Thanks

k
Public Sub closeallforms()
Dim objType As Type() =
Reflection.Assembly.GetExecutingAssembly.GetTypes( )
Dim x As Integer, i As Integer
Try
For x = LBound(objType) To UBound(objType)
If Not objType(x).Name = Me.Name Then '/// make sure
you dont unload this form yet.
i = FindWindow(vbNullString, objType(x).Name)
PostMessage(i, CInt(&H10), vbNullString,
vbNullString)
End If
Next
Catch ex As Exception
MessageBox.Show("Oops, the following error occured:" &
ex.Message)
Finally
'MyBase.Close() '/// now that all the other forms are
closed , unload this one.
End Try
End Sub

Jun 27 '08 #1
12 1470
<kr*******@yahoo.co.ukschrieb
I found this nifty routine that closes a form one by one every time
it is called. However, I need to hide them instead of closing them.
Is there a way to alter the code? I assume it has something to do
with this line here...
PostMessage(i, CInt(&H10), vbNullString, vbNullString)
I do not even quote this really _awesome_ code.

Sorry for not answering your other question, yet. I thought that you
should really get into some OOP basics and learn about variable scope
before writing more lines of code.

Well, where do I start...? Sorry, if you already know some of the
following things:

First, which VB version do you have? in VB 2008 (or maybe 2005) there is
the OpenForms property (please use symbol search to find it). However,
I've never used it because I've never needed a property like that.
However, of course, you can use the OpenForms property.

Second, a UI thread (user interface) has a message loop. The OS is
message based. The message loop (see F1) is a loop that processes all
messages put into a queue by the OS. In the Framework, the message loop
is inside Application.Run (see object browser). There are overloaded
versions: One has no args, one has one arg expecting a Form object. The
one w/o args runs til the thread calls Application.ExitThread. The other
one does it's job til the passed Form has been closed.

3rd, "application framework". If enabled, you can choose the startup
Form. There is an additional option that says "til all Forms have been
closed" (or similar). VB will create an invisible Sub Main - the UI
thread's main procedure that every thread has - that behaves like
instructed in the project's properties. If the "application framework"
is disabled, you can set your own Sub Main where you can, for example,
open a database connnection before showing any form or whatever.

Now, which version did you choose? Why do you need to "close all
Forms", as the subject says? Or, maybe you have an additional NotifyIcon
in the task bar? If you have a startup Form (therefore the explanation
above), just close that one. If you have your own Sub Main, show us how
it looks like.

To be able to answer your other question, please describe the scenario
as detailled as possible. I mean, which Form opens from where and which
one needs to close another one? Do you have an MDI From? Currently, I
can only give the multi-purpose answer: If you need a reference to an
object, you have to make it available. How to do it, depends on the
situation.

(sry for a lot to read; I always like writing about one of my favorite
topics. ;-) )
Armin

Jun 27 '08 #2
On May 3, 9:17 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
<kronec...@yahoo.co.ukschrieb
I found this nifty routine that closes a form one by one every time
it is called. However, I need to hide them instead of closing them.
Is there a way to alter the code? I assume it has something to do
with this line here...
PostMessage(i, CInt(&H10), vbNullString, vbNullString)

I do not even quote this really _awesome_ code.

Sorry for not answering your other question, yet. I thought that you
should really get into some OOP basics and learn about variable scope
before writing more lines of code.

Well, where do I start...? Sorry, if you already know some of the
following things:

First, which VB version do you have? in VB 2008 (or maybe 2005) there is
the OpenForms property (please use symbol search to find it). However,
I've never used it because I've never needed a property like that.
However, of course, you can use the OpenForms property.

Second, a UI thread (user interface) has a message loop. The OS is
message based. The message loop (see F1) is a loop that processes all
messages put into a queue by the OS. In the Framework, the message loop
is inside Application.Run (see object browser). There are overloaded
versions: One has no args, one has one arg expecting a Form object. The
one w/o args runs til the thread calls Application.ExitThread. The other
one does it's job til the passed Form has been closed.

3rd, "application framework". If enabled, you can choose the startup
Form. There is an additional option that says "til all Forms have been
closed" (or similar). VB will create an invisible Sub Main - the UI
thread's main procedure that every thread has - that behaves like
instructed in the project's properties. If the "application framework"
is disabled, you can set your own Sub Main where you can, for example,
open a database connnection before showing any form or whatever.

Now, which version did you choose? Why do you need to "close all
Forms", as the subject says? Or, maybe you have an additional NotifyIcon
in the task bar? If you have a startup Form (therefore the explanation
above), just close that one. If you have your own Sub Main, show us how
it looks like.

To be able to answer your other question, please describe the scenario
as detailled as possible. I mean, which Form opens from where and which
one needs to close another one? Do you have an MDI From? Currently, I
can only give the multi-purpose answer: If you need a reference to an
object, you have to make it available. How to do it, depends on the
situation.

(sry for a lot to read; I always like writing about one of my favorite
topics. ;-) )

Armin
I have an application which uses Windows media player on a separate
form + other bits and pieces also on separate forms. I can close them
all using the above code but for some reason when I close windows
media player it will not open again unless I re-start the program.
Therefore hiding it is a better thing to do so hiding rather than
closing will do for me.
However, one of my forms will not hide using the usual formx.hide( )
method (it won't close either). So how to hide all the forms one by
one...
K.
Jun 27 '08 #3
<kr*******@yahoo.co.ukschrieb
>
I have an application which uses Windows media player on a separate
form + other bits and pieces also on separate forms. I can close
them all using the above code but for some reason when I close
windows media player it will not open again unless I re-start the
program. Therefore hiding it is a better thing to do so hiding
rather than closing will do for me.
However, one of my forms will not hide using the usual formx.hide( )
method (it won't close either). So how to hide all the forms one by
one...
What's the project's startup object? From where do you open the WMP Form
and the others? From where do you want to close them? Do you have an MDI
Form? I do understand that you want to hide a Form (or some), but the
relations between the Forms is still not clear to me. Which of the Forms
"lives" always? Do you specify a Form as the Owner of other(s)? Are
there modal Forms (related to the problem)?
Armin

Jun 27 '08 #4
On May 3, 11:43 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
<kronec...@yahoo.co.ukschrieb
I have an application which uses Windows media player on a separate
form + other bits and pieces also on separate forms. I can close
them all using the above code but for some reason when I close
windows media player it will not open again unless I re-start the
program. Therefore hiding it is a better thing to do so hiding
rather than closing will do for me.
However, one of my forms will not hide using the usual formx.hide( )
method (it won't close either). So how to hide all the forms one by
one...

What's the project's startup object? From where do you open the WMP Form
and the others? From where do you want to close them? Do you have an MDI
Form? I do understand that you want to hide a Form (or some), but the
relations between the Forms is still not clear to me. Which of the Forms
"lives" always? Do you specify a Form as the Owner of other(s)? Are
there modal Forms (related to the problem)?

Armin
Ok I figured it out. Clear all forms at once like this

Private Const SW_HIDE As Integer = 0

Private Const SW_RESTORE As Integer = 9

Private Declare Function FindWindow Lib "user32.dll" Alias
"FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As
String) As Integer

<DllImport("User32")_
Private Shared Function ShowWindow(ByVal hwnd As Integer, ByVal
nCmdShow As Integer) As Integer

Private Const SW_HIDE As Integer = 0

Private Const SW_RESTORE As Integer = 9
Public Sub hideallforms()
' Actually this hides them one by one
Dim objType As Type() =
Reflection.Assembly.GetExecutingAssembly.GetTypes( )
Dim x As Integer, i As Integer

Try
For x = LBound(objType) To UBound(objType)
If Not objType(x).Name = Me.Name Then '/// make sure
you dont unload this form yet.
i = FindWindow(vbNullString, objType(x).Name)
ShowWindow(i, SW_HIDE)

End If
Next
Catch ex As Exception
MessageBox.Show("An error occured hiding forms:" &
ex.Message)

End Try
End Sub
Works a treat and you can restore them all at once too using
SW_RESTORE. Now that wasn't hard was it..

k.

Jun 27 '08 #5
<kr*******@yahoo.co.ukschrieb
Works a treat and you can restore them all at once too using
SW_RESTORE. Now that wasn't hard was it..
If you had answered some questions, I would have been able to give a
solution, too. Anyway, problem seems to be "solved"...
Armin

Jun 27 '08 #6
On May 4, 10:39 am, "Armin Zingler" <az.nos...@freenet.dewrote:
<kronec...@yahoo.co.ukschrieb
Works a treat and you can restore them all at once too using
SW_RESTORE. Now that wasn't hard was it..

If you had answered some questions, I would have been able to give a
solution, too. Anyway, problem seems to be "solved"...

Armin
Thanks but I was after a general method rather than just specific to a
particular case.

K.
Jun 27 '08 #7
Kronecker,

Be aware that in Net almost everything exists from arrays, collections and
lists (and generic versions of that)

Every form has a childcollection, therefore this should do it if you have
started your childforms from a form.

\\\
For i = Me.OwnedForms.Count To -1 Step -1
Me.OwnedForms(i).Close()
Next
///
Backwards to be sure you peeling everytime one from the collection, confirm
the so by me called "Armin Zingler" method.

(Not tested by the way)

Cor
<kr*******@yahoo.co.ukschreef in bericht
news:5d**********************************@w4g2000p rd.googlegroups.com...
>I found this nifty routine that closes a form one by one every time it
is called. However, I need to hide them instead of closing them. Is
there a way to alter the code? I assume it has something to do with
this line here...
PostMessage(i, CInt(&H10), vbNullString, vbNullString)

Thanks

k
Public Sub closeallforms()
Dim objType As Type() =
Reflection.Assembly.GetExecutingAssembly.GetTypes( )
Dim x As Integer, i As Integer
Try
For x = LBound(objType) To UBound(objType)
If Not objType(x).Name = Me.Name Then '/// make sure
you dont unload this form yet.
i = FindWindow(vbNullString, objType(x).Name)
PostMessage(i, CInt(&H10), vbNullString,
vbNullString)
End If
Next
Catch ex As Exception
MessageBox.Show("Oops, the following error occured:" &
ex.Message)
Finally
'MyBase.Close() '/// now that all the other forms are
closed , unload this one.
End Try
End Sub
Jun 27 '08 #8
<kr*******@yahoo.co.ukschrieb
On May 4, 10:39 am, "Armin Zingler" <az.nos...@freenet.dewrote:
<kronec...@yahoo.co.ukschrieb
Works a treat and you can restore them all at once too using
SW_RESTORE. Now that wasn't hard was it..
If you had answered some questions, I would have been able to give
a solution, too. Anyway, problem seems to be "solved"...

Armin

Thanks but I was after a general method rather than just specific to
a particular case.
I think it's a very bad method in any case. But again, use it if it
works for you.

Maybe I should be fair and give some reasons:
- There are managed ways instead of using pInvoke.
- hWnd must be IntPtr. Yes, Integer works on a 32-bit System. Not on
64-bit systems. I'd always use Intptr without thinking about the
destination platform
- Well... the whole approach is b...sh.. Yes, even if it works. Sry, I
can't explain - but I bet i'd win every voting 95:5. ;-)

Armin

Jun 27 '08 #9
"Cor Ligthert[MVP]" <no************@planet.nlschrieb
Backwards to be sure you peeling everytime one from the collection,
confirm the so by me called "Armin Zingler" method.
I'm still trying to understand whether this a positive or negative
statement. ;)
Armin

Jun 27 '08 #10
>
I'm still trying to understand whether this a positive or negative
statement. ;)
Why?

Cor
Jun 27 '08 #11
Armin,

I will make it easier for you, I don't know if you invented it, but in my
idea was you the first one from which I readed it.

Therefore I call it an Armin Zingler method.

I have to Herfried methods, which is the UrlEncoded and there is an terrible
good solution he had with the favorites.

When I have to do something with Serializing, then I always have to think on
Tom.

So decide your self if it is a positive or negative statement.

:-)

Cor

"Armin Zingler" <az*******@freenet.deschreef in bericht
news:e9**************@TK2MSFTNGP05.phx.gbl...
"Cor Ligthert[MVP]" <no************@planet.nlschrieb
>Backwards to be sure you peeling everytime one from the collection,
confirm the so by me called "Armin Zingler" method.

I'm still trying to understand whether this a positive or negative
statement. ;)
Armin
Jun 27 '08 #12
On May 4, 9:16 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
<kronec...@yahoo.co.ukschrieb
On May 4, 10:39 am, "Armin Zingler" <az.nos...@freenet.dewrote:
<kronec...@yahoo.co.ukschrieb
Works a treat and you can restore them all at once too using
SW_RESTORE. Now that wasn't hard was it..
If you had answered some questions, I would have been able to give
a solution, too. Anyway, problem seems to be "solved"...
Armin
Thanks but I was after a general method rather than just specific to
a particular case.

I think it's a very bad method in any case. But again, use it if it
works for you.

Maybe I should be fair and give some reasons:
- There are managed ways instead of using pInvoke.
- hWnd must be IntPtr. Yes, Integer works on a 32-bit System. Not on
64-bit systems. I'd always use Intptr without thinking about the
destination platform
- Well... the whole approach is b...sh.. Yes, even if it works. Sry, I
can't explain - but I bet i'd win every voting 95:5. ;-)

Armin
I kind of agree but it works for the present - yes it's a bodge and
not elegant.

K.
Jun 27 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ThunderMusic | last post by:
Hi, I'm trying to have a MSN Messenger like form/app closing behavior. When I click on the X button, I only want the form to disappear and when I double-click on the notify icon or right-click...
6
by: Barry Gast | last post by:
Hi. I have an MDI Parent form with multiple child windows. When I close the parent form, the Closing events of the children forms are not executing. Do I have to close all the child forms in the...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
3
by: Oenone | last post by:
I'm writing an application with various MDI child forms. In the Closing event of many of the forms, I have code that asks the user whether he is sure he wants to close the form, because it...
2
by: Tom | last post by:
How is the best way to avoid validation when closing a window? For instance, I have a Windows Forms window which has a validation event for a text box. However, if one enters invalid data in then...
4
by: Academic | last post by:
Does it make sense to put this If e.Cancel Then Exit Sub at the beginning of form closing events so if the user cancels the app's exiting in one Closing routine he will not be asked again by...
14
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005 and .net 2.0. I'm creating an application that has 3 forms. I want allow users to move forward and backward with the forms and retain the data users have entered. I thought...
19
by: rbrowning1958 | last post by:
Hello, I am confused by dispose etc. and hope someone can set me right. 1. The Dispose(Bool) the IDE generates for a form has nothing to do with IDisposable, right? 2. So when is this called?...
2
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
I am (still) relatively new to Windows applications, most of my experience has been Web based, and I am confused about what exactly happens when the Main() method is called and how to manipulate...
4
Fr33dan
by: Fr33dan | last post by:
Hi, I'm having trouble with a multi-threaded program crashing on a specific machine when the worker thread is not initialized at when the _FormClosing method of my main form called. Here is my...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.