Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Pressing the X Button

Pressing the X Button

Scheduled Pinned Locked Moved C#
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheMajorRager
    wrote on last edited by
    #1

    Hello everyone, I was wondering if there was a way to know when the X button was pressed at the upper right hand part of the screen Thanks, The Major Rager

    D 1 Reply Last reply
    0
    • T TheMajorRager

      Hello everyone, I was wondering if there was a way to know when the X button was pressed at the upper right hand part of the screen Thanks, The Major Rager

      D Offline
      D Offline
      Dan Neely
      wrote on last edited by
      #2

      The Closing and Closed events of the form both are activated by this click. Closing happens first and will allow you to abort the shutdown.

      T 1 Reply Last reply
      0
      • D Dan Neely

        The Closing and Closed events of the form both are activated by this click. Closing happens first and will allow you to abort the shutdown.

        T Offline
        T Offline
        TheMajorRager
        wrote on last edited by
        #3

        Do you have an example of how I would code something in that particular function?

        J 1 Reply Last reply
        0
        • T TheMajorRager

          Do you have an example of how I would code something in that particular function?

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          You can do this inside the form itself, or using another object which has subscribed to the forms event. 1st within the form: using System.Windows.Forms; using System.ComponentModel; class MyForm : Form { private bool stopUserFromClosingForm = true; public MyForm() { } protected override void OnClosing(CancelEventArgs e) { if(stopUserFromClosingForm) { e.Cancel = true; } } } or if another object has a reference to the form static void Run() { Form myForm = new Form() myForm.Closing += new CancelEventHandler(this.HandleFormClose); myForm.Show() } private void HandleFormClose(object sender, CancelEventArgs e) { e.Cancel = true; // stop the form closing } There is also a Closed event, which is juts a normal EventHandler - it has no Cancel property.

          T 1 Reply Last reply
          0
          • J J4amieC

            You can do this inside the form itself, or using another object which has subscribed to the forms event. 1st within the form: using System.Windows.Forms; using System.ComponentModel; class MyForm : Form { private bool stopUserFromClosingForm = true; public MyForm() { } protected override void OnClosing(CancelEventArgs e) { if(stopUserFromClosingForm) { e.Cancel = true; } } } or if another object has a reference to the form static void Run() { Form myForm = new Form() myForm.Closing += new CancelEventHandler(this.HandleFormClose); myForm.Show() } private void HandleFormClose(object sender, CancelEventArgs e) { e.Cancel = true; // stop the form closing } There is also a Closed event, which is juts a normal EventHandler - it has no Cancel property.

            T Offline
            T Offline
            TheMajorRager
            wrote on last edited by
            #5

            Thanks for replying, but I am using ASP.NET, which means Web Forms instead of just a Window Form, do you have an example for that?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups