Jump to content

Visual Basic - Prevent Closing without Key Command


Recommended Posts

Posted

Hi All,

 

Maybe trying to do something that's not quite possible in VB, however hopefully someone here can confirm/offer a way of achieving it.

 

I've created a basic Windows form in Visual Studio 2013 (Visual Basic), that shows full screen and always on top. I'm wanting to prevent it from being closed without using a key command (not entirely fussed what key command, but was thinking Ctrl+Shift+C or something similar).

 

I've prevented the form from closing with the following code:

    
Private Sub Image_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
       If (e.CloseReason = CloseReason.UserClosing) Then
           e.Cancel = True
           MessageBox.Show("To close this window, please speak to your teacher.", "Panic!", MessageBoxButtons.OK, MessageBoxIcon.Error)
       End If
   End Sub

 

Then tried to close the form using a key command with:

   Private Sub Image_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
       If e.KeyCode = Keys.Escape Then Me.Close()
   End Sub

 

Unfortunately, the first bit of code prevents the second from working. Does anyone know of a better way of achieving this / integrating the two pieces of code? I'm not exactly an expert at this and my Googling expertise has let me down on this one too.

 

Any suggestions would be appreciated (preferably suggestions that don't include using C# or other instead - I wish I'd started with something else but this is the final thing I need to implement :rolleyes: ).

 

Cheers!

Posted

It's a while since I did VB, but the logic approach I would use would be:

 

In the key handler, set a variable AllowClose = True.

 

Then in the close handler, add "if AllowClose then....".

 

Peter

  • Thanks 1
Posted
Hi All,

 

Maybe trying to do something that's not quite possible in VB, however hopefully someone here can confirm/offer a way of achieving it.

 

I've created a basic Windows form in Visual Studio 2013 (Visual Basic), that shows full screen and always on top. I'm wanting to prevent it from being closed without using a key command (not entirely fussed what key command, but was thinking Ctrl+Shift+C or something similar).

 

I've prevented the form from closing with the following code:

    
Private Sub Image_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
       If (e.CloseReason = CloseReason.UserClosing) Then
           e.Cancel = True
           MessageBox.Show("To close this window, please speak to your teacher.", "Panic!", MessageBoxButtons.OK, MessageBoxIcon.Error)
       End If
   End Sub

 

Then tried to close the form using a key command with:

   Private Sub Image_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
       If e.KeyCode = Keys.Escape Then Me.Close()
   End Sub

 

Unfortunately, the first bit of code prevents the second from working. Does anyone know of a better way of achieving this / integrating the two pieces of code? I'm not exactly an expert at this and my Googling expertise has let me down on this one too.

 

Any suggestions would be appreciated (preferably suggestions that don't include using C# or other instead - I wish I'd started with something else but this is the final thing I need to implement :rolleyes: ).

 

Cheers!

 

I use:

 

Environment.Exit(1)

 

To forcibly close the software..

  • Thanks 2
Posted
You could add a variable to the keypress and then add a check to see if its more than one and if it is dont do anything on the other bit of code?
  • Thanks 1
Posted

Thanks for the suggestions everyone - really appreciate it.

 

It's a while since I did VB, but the logic approach I would use would be:

 

In the key handler, set a variable AllowClose = True.

 

Then in the close handler, add "if AllowClose then....".

 

Peter

 

I tried this but couldn't get it working, think I may have missed something, so will revisit.

 

 

 

I use:

 

Environment.Exit(1)

 

To forcibly close the software..

 

Basically I'm launching a second form from a form and it's just the second form I'm wanting to close rather than the whole application (however this has given me the idea to put a keyboard shortcut into the initial form to kill the whole application for staff - so thanks!)

 

 

 

You could add a variable to the keypress and then add a check to see if its more than one and if it is dont do anything on the other bit of code?

 

Not quite grasping this, sorry... As I say, I'm no expert

Posted
Not quite grasping this, sorry... As I say, I'm no expert

He's suggesting same as me, except he's incrementing a variable from 0 to 1, 2, 3 and testing if it's >0 rather than setting it true/false.

 

Peter

  • Thanks 1
Posted
He's suggesting same as me, except he's incrementing a variable from 0 to 1, 2, 3 and testing if it's >0 rather than setting it true/false.

 

Peter

 

Ah OK, thanks. That makes sense now...

 

 

Thanks guys - this gives me a couple of things to look at :)

Posted

Something like:

 

If (e.CloseReason = CloseReason.UserClosing) and (MagicKeyPressed = False) Then
           e.Cancel = True
           MessageBox.Show("To close this window, please speak to your teacher.", "Panic!", MessageBoxButtons.OK, MessageBoxIcon.Error)
       End If

 

and then in the code checking for the magic key pressed, set MagicKeyPressed = True

 

Don't forget to set MagicKeyPressed = False when the window is first opened.

  • Thanks 1
Posted
He's suggesting same as me, except he's incrementing a variable from 0 to 1, 2, 3 and testing if it's >0 rather than setting it true/false.

 

Peter

 

I just went and read your comment, yeah, mines abit more janky than yours lol

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...