Close the form when clicking anywhere of the form or anywhere of the screen

A Very simple question. A good example is the splash screen. How to do that? Thanks.
LVL 3
maidinhtaiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PockyMasterCommented:
Create a form_click event
and write me.close in it
0
PockyMasterCommented:
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        Me.Close()
    End Sub
0
maidinhtaiAuthor Commented:
The matter is when I click on a label control of that form, the click event of the form is not fired. When the user press anykey on the keyboard, the form will close. But when he click the mouse on the form, nothing happens.
Here is the function makes the form closed when the user press any key on the keyboard. But it cannot trap the mouse button.

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Scroll Then ChamLai()
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

xersoftCommented:
Try this code:

    Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddCloseHandler(AddressOf MainForm_Click, Me.Controls)
    End Sub
    Private Sub AddCloseHandler(ByVal Handler As EventHandler, ByVal ctls As Windows.Forms.Control.ControlCollection)
        For Each ctl As Windows.Forms.Control In ctls
            AddHandler ctl.Click, Handler
            AddCloseHandler(Handler, ctl.Controls)
        Next
    End Sub
    Private Sub MainForm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
        Me.Close()
    End Sub
0
maidinhtaiAuthor Commented:
Xersoft, I'd used the  same code before I posted this question. Is there another method that is shorter. Something likes Function ProcessCmdKey that can trap all the keys on the keyboard. Or your codes is the only choice?
0
xersoftCommented:
Although I'm sure there are other ways, probably some easer and shorter, I can think of none.
0
PockyMasterCommented:
I also know there are easier ways, I know there was a way to reroute all form objects' events to the main form, but I have to sit at it and try to remember it :D
0
maidinhtaiAuthor Commented:
PockyMaster, I'm waiting for you :D.
0
maidinhtaiAuthor Commented:
PockyMaster, it's been a long time waiting for your remembering. Or you have forgot about that? I mean "I also know there are easier ways, I know there was a way to reroute all form objects' events to the main form, but I have to sit at it and try to remember it :D"
0
PockyMasterCommented:
Heheh.... Yeah, I remember the easier ways were easier in a far past, now the old easier ways have become the more difficult ways :D

I would say, go for the reroute events stuff.
0
xersoftCommented:
keep in mind that your not "rerouting events" you are just adding another handler. You can still have other events handling the click event.
0
maidinhtaiAuthor Commented:
OK. It seems that there's no shorter ways to do it. Why doesn't VB.NET have any function like ProcessCmdKey to solve my problem?
0
PockyMasterCommented:
Well, there are events that you can catch when you click anywhere on the screen.. but uhm...
ok, let me look at it again :D
0
PockyMasterCommented:
 Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)
        Debug.WriteLine(m.Msg)
        If (m.Msg And 528) = 528 Then

            Me.Close()

        End If
        MyBase.DefWndProc(m)

    End Sub

This also works :D
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PockyMasterCommented:
you don't need the debug.writeline of course :D but that will let you view all the messages being posted
0
maidinhtaiAuthor Commented:
That's exact what I want. If possible, I will change the accept answer. Thank you two :D.
0
PockyMasterCommented:
You're welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.