Close program when the user presses Alt+X - vb.net

I have an assignment from school where the program is supposed to close when the user presses Alt X.
I have done this using Java but that was a long time ago and I can't remember how I did it. How do you do it in VB?
Thanks for the help
Edit:
Thank you for all the help.
But i cant get it to work:
If e.KeyCode = Keys.Alt AndAlso e.KeyCode = Keys.X Then
Application.Exit()
End If

Try it like this (and for a form, make sure KeyPreview=True):
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
Application.Exit()
End If
End Sub

Have a look at the following events, one them should suit your needds. You then should be able to alter the examples given in the MSDN pages to do what you want -
KeyDown -
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx
KeyPress -
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx
KeyUp -
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keyup.aspx

you want some thing like this.
http://www.dreamincode.net/forums/topic/100504-key-pressed-event/

You have keydown, keyup and some other events you can use to check what keys is being pressed

LarsTech's answer is more versatile, but there is this quick method.
Put a button on the form with Text = "E&xit". This will underline the x in Exit and make it work with the Alt+X key combo. Then you can add code to the ExitButton.Click event to exit the application. This has the added benefit of providing a clickable button as an alternate method of closing the application.

Related

I want to show a form by pressing the f5 key but it doesn't work in vb.net

hello I'm just writing a program and I need to show a new form when I'm on a text_box and press the f5 key
but its seem that the program doesn't recognize the f5 key, because I eve did a mini-program to find out the keychar of the f5 key and didn't work
I don't know if someone could help me with this
this is my code:
Private Sub txt_numero_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txt_numero.KeyPress
If e.KeyChar = Convert.ToChar(Keys.F5) Then
fmrBuscarByName.Show()
End If
You may use the KeyUp event too!
... txt_numero.KeyUp
If e.KeyCode = Keys.F5 Then
Form1.ShowDialog()
End If
End Sub
Handle KeyDown rather than KeyPress:
If e.KeyCode = Keys.F5 Then

How do i detect Alt + Tab Key and then close the form?

I want to close my form when Alt + Tab is pressed. However, The form is somehow not registering the key combination.
i have tried to use the Me.KeyUp event to detect the keys
Private Sub Menu_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyData
Case (Keys.Alt + Keys.Tab)
Close()
End Select
End Sub
How can it be done ?
Try this: As in my comment Alt+Tab would not work. So try something else like Alt+Q. Put this code under the Form's keydown event.
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If (e.KeyCode = Keys.Q AndAlso e.Modifiers = Keys.Alt) Then
Me.Close()
End If
End Sub
You need to set the form's Set KeyPreview to True for it to respond to the key events, but since Alt + Tab is a special windows combination the key events will not be fired.
Try in this way
Dim myval As Integer 'this is global variable declare
Private Sub Menu_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyData
Case (Keys.Alt)
myval = 1
Case (Keys.Tab)
If myval = 1 Then
Me.Close()
End If
End Select
End Sub
May be my syntax in not correct, please edit it if anything is notok.
I managed to work around this problem using this method-
That's usually the ESC key. One key press. A common one. Maybe you want to handle the Form's deactivate event, instead. – Jimi
I used the forms deactivate event to close it, since Alt+Tab is basically deactivating the form!
(A workaround because Alt+Tab as keystrokes were not detected by any means, as mentioned by
the last two answers.)
My final code looks like this -
Private Sub Menu_Deactivate(sender As Object, e As EventArgs) Handles Me.Deactivate
If loaded = true Then
Close()
End If
End Sub
I had to check if loaded (a boolean i declared to be set to true when everything is loaded)
to be true because my form opens one other form while loading, which inadvertently deactivated the main form and closed it!
Thank you so much for helping me everyone!!
Edit: I needed to clarify that the second form is always behind my main form so it is never really clicked on. I did this as a workaround to have aero blur in my application without a bug i experienced while applying it to the main form - it is too light, so text is unreadable. So i set another form to show aero blur and always stay behind my main form, whose opacity I have set to 0.88. This gives me a lot of control over the look of the blur.

VB 2008 net keycode

Can anyone help on how to disable the alt + ctrlkey in vb.net. I am not using any button to disable. But by default it should start function disable to keys once the application starts.
You can use the SuppressKeyPress Property. I'm not sure what you are trying to do, but lets say that you dont want a certain key to effect a textbox change. Lets say you don't want the key G to be written in the text box. You can write to following code:
Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.G Then
e.SuppressKeyPress = True
End If
End Sub
This code will prevent the key G to be written in the text box. Hoped it helped :)

How to make a KeyDown work twice

I am working on a little story type thing in visual basic. Nothing too complicated but i have run into a problem. I am trying to make a pause feature. I have it all working to when i press escape, the form goes into a menu type thing. The only problem is that it only works once. The first press works, but if i hit continue, it continues from where i left off. But if i press it again, nothing happens. Here is the code i am using:
Private Sub Story_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
Text.Hide()
Background.Hide
ContinueButton.Show
OptionsButton.Show
End If
End Sub
Is there any way for me to make this work more than just once? I am using a KeyDown event in a private sub.
Hope this made sense, but thanks for any help guys!
You're using Form.KeyPreview = true to capture the form keystrokes. I think Text.Hide() or one of Show operations could be the problem such as getting the TabStop's out of order or leaving a gap in the tab sequence.
Try resetting the controls or using SendKeys to reset the tab sequence index (to the approriate a control to have focus), For example, Tab forward and Alt Tab back:
SendKeys.Send("{TAB}");
or
SendKeys.Send("%{TAB}");
Ref:
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=vs.110).aspx
Using a class level boolean as toggle should work:
Dim Hide As Boolean = False
Private Sub Story_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
Hide = Not Hide
If Hide Then
Text.Hide()
Background.Hide
ContinueButton.Show
OptionsButton.Show
Else
Text.Show()
Background.Show
ContinueButton.Hide
OptionsButton.Hide
End If
End Sub
If nothing else helps, you can always have a private class-level variable like _processingKeyDown, set to True at handler start, and then to False when done. The first line in the handler would be:
If _processingKeyDown Then Return

Multiple Keyboard Shortcuts

I am using the following code to try and get Ctrl+S to press a toolstrip button:
Private Sub take_register_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.S And Keys.Control Then
ToolStripButton20.PerformClick()
End If
End Sub
I am a newbie at this, so I dont understand millions of lines of coding, so can you please keep it as simple as possible :-) .
Total guesswork here since there is no actual question. First, in order to get something like that work, you need to set KeyPreview = True for the form. Next, you probably want to use the KeyDown event instead of KeyPress:
Private Sub Form1_KeyDown(...)
' when possible use AndAlso for speed and to avoid some errors in
' some situations. if e.Control is False, the second part wont be evaluated.
If e.Control AndAlso e.KeyCode = Keys.S Then
ToolStripButton20.PerformClick()
End If
End Sub
To repeat: you can simply assign a shortcut key combo to the menu object in the designer and let .NET do all the work. ...and I don't know where "multiple" comes in to play unless Ctrl+S counts as multiple somehow.