gtg93 Posted December 11, 2019 Posted December 11, 2019 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 ). Cheers!
howartp Posted December 11, 2019 Posted December 11, 2019 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 1
ass17 Posted December 12, 2019 Posted December 12, 2019 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 ). Cheers! I use: Environment.Exit(1) To forcibly close the software.. 2
Knil92 Posted December 13, 2019 Posted December 13, 2019 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? 1
gtg93 Posted December 17, 2019 Author Posted December 17, 2019 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
howartp Posted December 17, 2019 Posted December 17, 2019 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 1
gtg93 Posted December 17, 2019 Author Posted December 17, 2019 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
mavhc Posted December 17, 2019 Posted December 17, 2019 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. 1
Knil92 Posted December 17, 2019 Posted December 17, 2019 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now