How to tell when Tab is pressed in TextBox - vb.net

How can I tell if the Tab key has been pressed inside a certain textbox.
I tried enabling the AcceptsTab property to true and creating a function that handles texstboxname.KeyPress event, but when I tab while inside it it just tabs out of the box, instead of hitting my event. (normal keys fire my event, but tab never even fires it)

The Multiline property needs to be true as well.
From MSDN (emphasis in bold)
Gets or sets a value indicating whether pressing the TAB key in a multiline text box control types a TAB character in the control instead of moving the focus to the next control in the tab order.
so set the Multiline property to true.
As MiniTech also pointed out, KeyDown is easier to handle since it provides you with an e.KeyCode property whereas the KeyPress event only provides an e.KeyChar property.
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) _
Handles TextBox1.KeyDown
If e.KeyCode = Keys.Tab Then
'user pressed the Tab key...
End If
End Sub

It seems that you can either Additionally
Enable the multiline property on the TextBox
You can override IsInputKey before the Keydown or Keypress event first.
To do that you could derive a class from TextBox and ovverride it there, ( as Hans Passat suggested)
or you can also handle the PreviewKeyDown event and ovverride the IsInputKey to true in there.
I went with the second part of option two, so that I wouldn't have to worry about the multiline property being on and could Leave my code with the keypress event
Private Sub txtEntryBar_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles txtEntryBar.PreviewKeyDown
If e.KeyData = Keys.Tab Then
e.IsInputKey = True
End If
End Sub
Ref: http://www.vbforums.com/showthread.php?670904-Detecting-when-the-tab-key-is-pressed-in-a-textbox&p=4124911&viewfull=1#post4124911

Related

Strange TextBox behavior using AutoCompleteMode.Append: Ctrl+A clears the text

I use AutoCompleteMode set to AutoCompleteMode.Append in TextBox control.
After the auto-complete feature appends text to the existing and selects it, if I press Ctrl+A to select all the text, the textBox is cleared.
It happens with any textbox, you can test it by your own.
Do you know how to fix it?
It can be annoying, if you use this shortcut, that is.
That combination is intercepted along the way and misbehaves when AutoCompleteMode = AutoCompleteMode.Append. You can see it when a word in the list is partially selected; press ENTER (the CTRL+A equivalent here) to select it all, then press END and BACKSPACE: the text Selection wasn't actually cleared, the last letter is magically re-selected instead of deleted.
As a simple workaround, you can suppress the key presses when you detect that combination and use SelectAll() to select the text yourself:
(as noted, pressing Enter would to the same thing)
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.Control AndAlso e.KeyCode = Keys.A Then
e.SuppressKeyPress = True
TextBox1.SelectAll()
End If
End Sub

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.

How do I override a keypress event in a Custom Base Winforms in vb.net

I have downloaded a dll for a custom base Winforms as my base form, the developer of the custom form is no longer available and all of my forms inherits on this base form as it has some functionalities common to all of my other forms, my problem here is that the event when I press the ESC key exits or closes the active form immediately without a warning, I wanted to override the keydown event of the ESC key but it does not work. I tried this code below but they are not working, When I press ESC the form closes since the ESC Event which closes the form is embedded in the custom base form.
Private Sub frmMain_KeyPress(sender As Object, e As KeyPressEventArgs)
If e.KeyChar = Chr(27) Then
e.Handled = True
End If
End Sub
Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.Escape Then
e.Handled = True
End If
End Sub
try to use overrides event,
Overriding Event Handlers with Visual Basic .NET
It seems that your base form has the property KeyPreview set to True. You need to set it to false, probably the best place will be at the end of your constructor.
'frmMain.vb
Public Sub New()
InitializeComponent()
MyBase.KeyPreview = False
End Sub
More information on KeyPreview property here

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

My Form is not registering Enter key presses

I am writing an educational program. I have a button which I want to be "clicked" when the user presses enter. I have set the form's accept button property to true and the KeyPreview property is also set to true. I have tried creating a custom Sub to replicate AcceptButton functionality, this does not work either, the sub runs on all keys other than when the enter key is pressed.
Private Sub TextBoxAnswer_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBoxAnswer.KeyDown
If e.KeyCode = System.Windows.Forms.Keys.Enter Then
CheckQ()
End If
End Sub
I have spent the last 3 days searching for an answer and have tried everything on google I could find. I have also just tried setting the textbox MultiLine property to true and the enter key doesn't even create a new line, just to clarify I have set the Multiline property back to false now as it should be.
The AcceptButton property should be set = to the button you want "clicked" when enter is pressed, not a boolean value. Me.AcceptButton = buttonSubmit
Did you add a handler for the form's Keydown event to go along with setting the KeyPreview property?
Private Sub form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles form1.KeyDown
If e.KeyCode = System.Windows.Forms.Keys.Enter Then
CheckQ()
e.Handled = true
End If
End Sub
Your code as you have it written for the textbox keydown event should work as well. I tried it in a test app and didn't have any issues. Do you hit a breakpoint if you place it on the CheckQ() method call?
Any one of these methods should accomplish what you are trying to do.