Link to home
Start Free TrialLog in
Avatar of maidinhtai
maidinhtai

asked on

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.
Avatar of PockyMaster
PockyMaster
Flag of Netherlands image

Create a form_click event
and write me.close in it
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        Me.Close()
    End Sub
Avatar of maidinhtai
maidinhtai

ASKER

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
SOLUTION
Avatar of xersoft
xersoft

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Although I'm sure there are other ways, probably some easer and shorter, I can think of none.
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
PockyMaster, I'm waiting for you :D.
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"
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.
keep in mind that your not "rerouting events" you are just adding another handler. You can still have other events handling the click event.
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?
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you don't need the debug.writeline of course :D but that will let you view all the messages being posted
That's exact what I want. If possible, I will change the accept answer. Thank you two :D.
You're welcome