I have an app I'm working on and I've given the user a button with the option to close the app at any time - but I'm not sure if this is the best way to go about it so I'm asking for some feedback just to double check.
Here's what I have:
Private Sub CloseButton_Click(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles CloseButton.Click
End
End Sub
For all I know this could be a really bad way to achieve this. Please be gentle!
Firstly, you need to close/dispose all the objects you have been instantiating.
Then you can close your application. And to do so, it is better to use Application.Exit Method.
According to MSDN, It should informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.
Private Sub CloseButton_Click(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles CloseButton.Click
Application.Exit
End Sub
Related
I'm writing an examination piece of software for my workplace and would like to know how I can trap and cancel key-presses such as:
ALT+F4
WIN+TAB
ALT+TAB
WIN
CTRL+ALT+DEL
I'm aware CTRL+ALT+DEL may not be possible, but if any of this is it'll be a step in the right direction!
Ideally I want to prevent the action, and then open a new form I've created saying 'Unauthorised keypress'
as #SQLHound link relates... us the FormClosing event to handle what happens when a user attempts to close. But if you want to block a boot attempt, then #Plutonix suggestion may help. Something along these lines...
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim splashScreen as New Form
splashScreen.OpenDialog
e.Cancel=True
End Sub
I am having a bit problems trying this. I must admit that I am still novice programming. Well, I managed for do this in this way (VB.NET):
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If My.Computer.Keyboard.CtrlKeyDown Then
MORE IRRELEVANT CODE HERE
End If
End Sub
But with this way, even if it work, it dont let me customice the key shortcut (only Control, shift and others).
I did this too for try differents things:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = GlobalVariables.own_key Then
MORE IRRELEVANT CODE HERE
End If
End Sub
I give value to GlobalVariables.own_key from other button. This work perfectly, but only if program have the focus.
So... With the first code program work even if it haven't the focus but only with few keys... And with the second it let me use any key, but don't work if it haven't the focus.
I tryed understand the "keyboard hook" but I must admit that I didn't understad it and couldn't manage for work any of them.
Using VB.net 2012.
Really thanks so much for your help
It may not be an answer you like but it is the answer. In .NET, you cannot get notification of keys when your application does not have focus and you cannot get mouse events outside your applications windows. If you looked into the "Keyboard Hook", you should have read that little important fact regarding VB.
Global hooks are not supported in the .NET Framework
I'd need a way to cause the program to crash on purpose when i click a button. But nothing comes to my mind that would still allow me to compile the program. any code that causes a hard crash for whatever reason. in particular i need it to close and not be able to continue. My beta testers need to test the recovery after crash feature. Thanks!
these things never happen when they should..
How about just throwing an unhandled exception?
Private Sub btnCrash_Click(sender As System.Object, e As System.EventArgs) Handles btnCrash.Click
Throw New System.Exception("The program has crashed catastrophically!")
End Sub
To effectively kill the process use Environment.FailFast() in a button click handler, like this:
Protected Sub buttonCrash_Click(sender As Object, e As EventArgs) Handles buttonCrash.Click
Environment.FailFast()
End Sub
This will not generate any exceptions, etc., it is the same as going to Windows Task Manager and killing the process.
Here is the documentation for Environment.FailFast Method (String)
How about the Environment.FailFast method?
Private Sub btn_click(...)
Environment.FailFast("Failure!")
End Sub
I want to make myself a simple webapp using vb.net.I am trying to make a desktop app by which users can login and send mail or post wall(facebook) with out going into the browser. I have created the forms and all other thing using this tutorial( http://howtostartprogramming.com/vb-net/vb-net-tutorial-41-website-login/ ).This original code is working properly.
I have two problems.
1)I have made some changes of my own to the above code; to click_botton1 i have added this code.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate("http://login.yahoo.com/")
WebBrowser1.Document.GetElementById("login").SetAttribute("value", TextBox1.Text)
WebBrowser1.Document.GetElementById("passwd").SetAttribute("value", TextBox2.Text)
WebBrowser1.Document.GetElementById(".save").InvokeMember("click")
End Sub
but this code is showing error in vb.net(not compiling)
2)After login i want to go straight to the "compose mail page" with out browsing in the web browser window. So i have added this code to the original code.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
WebBrowser1.Document.GetElementById("login").SetAttribute("value", TextBox1.Text)
WebBrowser1.Document.GetElementById("passwd").SetAttribute("value", TextBox2.Text)
WebBrowser1.Document.GetElementById(".save").InvokeMember("click")
'NEWCODE'
webBrowser1.Navigate("http://in.mg50.mail.yahoo.com/neo/launch?.rand=de5jqdp66atmb")
End Sub
but this code is showing error. It will not go to the compose page but will redirect to login page(may be problem with cookies).
how i circumvent this problems
Advance thanks for help
I think a better approach would be to use .Net's System.Web.Mail class.
You will find plenty of resources if you Google it. You will need some simple things like the Yahoo (or other) SMTP server name and the account login info.
This approach will also be immune to changes in the Yahoo mail web page.
I'm working with VB.net.
I’m having problems while I connect my application to my database today so I wanted to add an error handling to close the form.
The problem is that when I put Me.close in a form, this form still open. I used the Form.Closing event handler to verify that it was called, and then ran my application in step by step which showed that the event handler was called, but the application continues and the errors appears to the user.
Does anyone knows how to close a form properly without closing the application?
Close will close a form, but only if it has no more code to run.
That is, there are two conditions that need to be fulfilled for a form to close:
Close must be called
Any method still running must be left
I suspect that another method is still running code, for instance a loop or other code that causes the form to remain open.
Furthermore, the form will get re-opened automatically once you start accessing its members form elsewhere, due to an infuriating property of VB to auto-instantiate forms.
You can check for what reason the form don't get closed.
Private Sub Form1_Closing(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
MsgBox(e.CloseReason.ToString)
End Sub
You can add to the Form_Closing event the following
The e.Cancel will close the open operation. But first check the reason.
Private Sub Form1_Closing(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
e.Cancel = True
End Sub
Technically the form is closed but not disposed, which means you can still reach the object but all controls in it are no longer reachable.
So you will have to call dispose from somewhere to get rid of it completely.