Solved

Detect X button click event on cmd window

Posted on 2010-09-03
3
427 Views
Last Modified: 2012-06-27
How do I detect from within a vb.net console application when the X button on its cmd window has been clicked?
0
Comment
Question by:gprodri
  • 2
3 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33601042
If you need that functionality then you should probably be using a WinForm instead...   =\

Can you give more details?
0
 
LVL 14

Expert Comment

by:Daniel Junges
ID: 33601049
SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCloseEvent), true);

private static bool ConsoleCloseEvent(CtrlTypes ctrlType)
        {
            switch (ctrlType)
            {
                case CtrlTypes.CTRL_C_EVENT:
                    isclosing = true;
                    Console.WriteLine("CTRL+C received!");
                    break;

                case CtrlTypes.CTRL_BREAK_EVENT:
                    isclosing = true;
                    Console.WriteLine("CTRL+BREAK received!");
                    break;

                case CtrlTypes.CTRL_CLOSE_EVENT:
                    isclosing = true;
                    Console.WriteLine("Program being closed!");
                    break;

                case CtrlTypes.CTRL_LOGOFF_EVENT:
                case CtrlTypes.CTRL_SHUTDOWN_EVENT:
                    isclosing = true;
                    Console.WriteLine("User is logging off!");
                    break;
            }
            return true;
        }
        [DllImport("Kernel32")]

        public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);

 

        public delegate bool HandlerRoutine(CtrlTypes CtrlType);



        public enum CtrlTypes

        {

            CTRL_C_EVENT = 0,

            CTRL_BREAK_EVENT,

            CTRL_CLOSE_EVENT,

            CTRL_LOGOFF_EVENT = 5,

            CTRL_SHUTDOWN_EVENT

        }

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 33601104
*Not for points...here's a VB implementation of junges's approach:
Imports System.Runtime.InteropServices

Module Module1



    Public isclosing As Boolean = False



    <DllImport("Kernel32")> _

    Public Function SetConsoleCtrlHandler(ByVal Handler As HandlerRoutine, ByVal Add As Boolean) As Boolean

    End Function



    Public Delegate Function HandlerRoutine(ByVal CtrlType As CtrlTypes) As Boolean



    Public Enum CtrlTypes

        CTRL_C_EVENT = 0

        CTRL_BREAK_EVENT

        CTRL_CLOSE_EVENT

        CTRL_LOGOFF_EVENT = 5

        CTRL_SHUTDOWN_EVENT

    End Enum



    Sub Main()

        SetConsoleCtrlHandler(New HandlerRoutine(AddressOf ConsoleCloseEvent), True)

        While Not isclosing



            ' ...



            System.Threading.Thread.Sleep(100)

        End While

    End Sub



    Private Function ConsoleCloseEvent(ByVal ctrlType As CtrlTypes) As Boolean

        Select Case ctrlType

            Case CtrlTypes.CTRL_C_EVENT

                Console.WriteLine("CTRL+C received!")



            Case CtrlTypes.CTRL_BREAK_EVENT

                Console.WriteLine("CTRL+BREAK received!")



            Case CtrlTypes.CTRL_CLOSE_EVENT

                Console.WriteLine("")

                Console.WriteLine("Program being closed!")

                Console.WriteLine("")

                Console.WriteLine("Press Enter to continue...")

                Console.ReadLine()



            Case CtrlTypes.CTRL_LOGOFF_EVENT, CtrlTypes.CTRL_SHUTDOWN_EVENT

                Console.WriteLine("User is logging off!")



        End Select



        isclosing = True

        Return True

    End Function



End Module

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Replacing HTML tags in Textarea/Textbox 5 47
Make a border less form movable 2 22
Protecting vb6 & .Net code Obfuscation 18 94
Convert an ASPX page into PDF 7 23
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now