Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-03-30
18
Medium Priority
?
205 Views
Last Modified: 2010-04-23
A Very simple question. A good example is the splash screen. How to do that? Thanks.
0
Comment
Question by:maidinhtai
  • 8
  • 6
  • 3
17 Comments
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16335533
Create a form_click event
and write me.close in it
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16335536
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        Me.Close()
    End Sub
0
 
LVL 3

Author Comment

by:maidinhtai
ID: 16335660
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Assisted Solution

by:xersoft
xersoft earned 400 total points
ID: 16336140
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
 
LVL 3

Author Comment

by:maidinhtai
ID: 16338181
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
 
LVL 5

Expert Comment

by:xersoft
ID: 16338565
Although I'm sure there are other ways, probably some easer and shorter, I can think of none.
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16339574
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
 
LVL 3

Author Comment

by:maidinhtai
ID: 16340849
PockyMaster, I'm waiting for you :D.
0
 
LVL 3

Author Comment

by:maidinhtai
ID: 16383368
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
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16383390
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
 
LVL 5

Expert Comment

by:xersoft
ID: 16383485
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
 
LVL 3

Author Comment

by:maidinhtai
ID: 16384611
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
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16384629
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
 
LVL 14

Accepted Solution

by:
PockyMaster earned 1600 total points
ID: 16384779
 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
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16384784
you don't need the debug.writeline of course :D but that will let you view all the messages being posted
0
 
LVL 3

Author Comment

by:maidinhtai
ID: 16384912
That's exact what I want. If possible, I will change the accept answer. Thank you two :D.
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16384920
You're welcome
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question