VB 2008 net keycode - vb.net

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 :)

Related

Single code for Tab Key functionality using Enter Key in Vb.Net for a Windows Application

I write this code for tab-like behavior when user presses ENTER on textboxes for every form, which works fine.
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
However, I need to write the code once, perhaps as a sub or function in a module so that I do not have to write the same for every form. I have checked various forums including
Detecting Enter keypress on VB.NET and
Tab Key Functionality Using Enter Key in VB.Net
BUT all I get is either to write code for each textbox or for every individual form.
Has anyone tried out a single code for ALL or may be several selected forms of the application? If yes, please share with me. Writing for every form still works fine for me but i need to take advantage of OOP. Thanks
There are many ways to implement this, one of those is to create a custom textbox User Control
Adding a control to your project is very easy just right click in your project in the solution explorer ->Add->User Control
Give a name to your control ex. "tabedtextbox"
Add a textbox control to your User Control
Place the code in the keypress event of textbox1 of the UserControl
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
SendKeys.Send("{TAB}")
e.Handled = True
End If
End Sub
Compile once in order to update the whole project with the new user control.
Goto to your form and add your new User Control, you can locate it at the Toolbox panel.
Run your program and you will see the behavior of TAB when you press enter on each textbox.
If You wish to allow the user to press enter in a TextBox, instead of pressing a specific button,
you can use the acceptbutton property.
One good way to use it, is to change the property, on the Enter and Leave events of the TextBox. That you call Click event of the Button, when User press Enter inside the TextBox.
Try this code:
Private Sub tbProject_Enter(sender As Object, e As EventArgs) Handles tbProject.Enter
Me.AcceptButton = bSearch
End Sub
Private Sub tbProject_Leave(sender As Object, e As EventArgs) Handles tbProject.Leave
Me.AcceptButton = Nothing
End Sub
Private Sub bSearch_Click(sender As Object, e As EventArgs) Handles bSearch.Click
'.... actions to perfom
End Sub
Screen

On key release handler

So, what I'm trying to do is run a piece of code when a button is pressed (I'll use the right-arrow key for the time being), and keep running it in a loop until that key is released.
So, my current code looks something like this (I've simplified it, because it's using SerialPorts, and is not easy to understand code):
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Right
Do Until (CODE LOOKING FOR RIGHT-ARROW KEY RELEASE)
SerialPort1.Write("right")
Loop
e.Handled = True
SerialPort1.Close()
End Sub
Ultimately, what I'm trying to do is control and arduino through the serial port function. I've wired it up to a cheap rc car (I usually have a few arduino projects on the go), and have code which allows me to control it by arrow keys in the IDE itself.
However, I have limited VB.NET knowledge (I've only been at it a few years), so I'm kinda struggling along with the help of Google, which was a god end while trying to work out how to keep the serialport open (it kept closing due to a logic error in my code).
The goal is that once I can control it by arrow keys in VB.NET, I can set up a more advanced program which will allow pre-programming routes (for example, I can try to "teach" the car sets of instructions so that I can get it to automatically follow a circuit, and load various pre-programmed routes into it). I'd also plan an on-screen gui to show what button was pressed, and whatever else I think of.
Also, given this is a prototype, I intend to eventually hook up the arduino and VB program to my hobby grade rc, and see if I can get the computer program, with pre-programmed isntructions, to beat my round my track.
Well, after that little essay, I guess it's just thanks in advance, and any advice in other areas of my project is also appreciated =)
Why make this complicated. It looks like you want to send something as long as the key is down, and then close the port when it is released.
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Right AndAlso keydelay >= 1 Then
Debug.WriteLine("DWN") 'SerialPort1.Write("right")
e.Handled = True
ElseIf e.KeyCode = Keys.Right Then
keydelay += 1
End If
End Sub
Dim keydelay As Integer = 0
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Right Then
keydelay = 0
Debug.WriteLine("UP") 'SerialPort1.Close()
End If
End Sub

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.

Multi Key Hotkeys in VB 2010

I've written a simple WAV player in VB 2010 - a Windows Form App with no menus. I'm trying to create a hotkey, like CTRL-D that would make a text box visible. Typing the correct password in the box will expand the form and show some Administrative functions. I'm having issues creating the hotkey with more than one key.
I've got the following code, but it does not work. I do have KeyPreview set to True.
Private Sub _KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Control And e.KeyCode = Keys.D Then
txtPWD.Visible = True
I've also tried
If e.Keycode = Keys.ControlKey & e.KeyCode = Keys.D
and many other variations including changing to ALT and SHIFT but nothing seems to work. If I remove one of the Keys, it works just fine. Where am I getting this wrong?
Check for Modifierkeys:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.modifierkeys.aspx

Close program when the user presses Alt+X

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.