Visual Studio AutoComplete simulates enter being pressed when selecting - vb.net

Visual studio nicely provides built-in auto-complete functionality for text boxes and combo boxes. I am using the suggest method of auto complete that provides the user with a list of choices filteres by what they have type. The problem I am having is that when the user makes a selection (via the mouse) VS places the selected text into the textbox and also simulates the enter key being pressed on the textbox.
In my program the enter keyup event is used to send the text entered in the text box to the a COM port. In order for this to work as desired the user must be able to select an auto-complete option and then add to it so that they can change settings.
Is it possible to stop it from triggering that event or to intercept it? I am unsure if it is possible or how to determine the origin of a keypress.
Here is the code for the KeyUp even as well as for the KeyPress Event:
Private Sub txtInput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInput.KeyPress
If e.KeyChar = Chr(13) Then
e.Handled = True
End If
End Sub
Private Sub txtInput_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtInput.KeyUp
If e.KeyValue = Keys.Enter Then
If txtInput.Text.ToLower = "cls" Or txtInput.Text.ToLower = "clr" Or txtInput.Text.ToLower = "clear" Then
txtOutput.Text = ""
Else
If SerialPort1.IsOpen Then
SerialPort1.Write(txtInput.Text)
Else
MsgBox("The serial port is not open")
End If
End If
txtInput.Text = ""
e.Handled = True
e.SuppressKeyPress = True
End If
End Sub
The auto-complete functionality was accomplished through the properties of the control, the only code for that was to generate the auto-complete list.
Thanks in advance for any help.
P.S. I am using VB.net, but if necessary I can figure out how to get it from another .net language to VB

After researching it, I haven't been able to find a standard way to do what you want to do without overriding the functionality of the built-in AutoComplete.
You will have to create your own AutoComplete class for Textboxes and just don't implement the MouseEventHandler like you normally would in the example code below for a traditional AutoComplete list:
private void List_MouseDown(object sender, MouseEventArgs e)
{
for (int i=0; i<this.list.Items.Count; i++)
{
if (this.list.GetItemRectangle(i).Contains(e.X, e.Y))
{
this.list.SelectedIndex = i;
this.SelectCurrentItem();
}
}
this.HideList();
}
CodeProject has a good example of a custom AutoComplete TextBox in C#. Good luck.

Related

OnRead event from scanner (Motorla EMDK) fires continuously

(I'm using VS2008 with EMDK v2.9 with Fix1)
I have a form, where I declare my reader:
Private WithEvents barcodeReader As Barcode.Barcode = New Barcode.Barcode
I want it to be active only in one of the controls on the form, so I do this:
Private Sub txbAccount_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txbAccount.GotFocus
barcodeReader.EnableScanner = True
End Sub
And turn it off the same way in the Lost Focus event of that textbox.
Here is the OnRead sub:
Private Sub barcodeReader_OnRead(ByVal sender As Object, ByVal readerData as Symbol.Barcode.ReaderData) Handles barcodeReader.OnRead
If (readerData.HasReader) Then
Try
Dim ctrl As TextBox = Ctype(GetActiveControl(), TextBox)
If (ctrl.Name = "txbAccount") Then
ctrl.Text = readerData.Text
End If
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
End If
End Sub
The problem is: as soon as I enable scanner in the GotFocus event of the textbox, the OnRead event will fire over and over (with empty data) until I actually press the scan key, scan actual data - then it stops.
I've read that maybe the Handled property is not getting set properly, however I don't see property like that for this.
Quick question to answer is the handledis usualy within the e eventargs (although I dont see any in that routine just readerdata it may be in there!?
On a side, with barcode scanners they usualy use the sendkeys commands to simply pass string through to the system. These can be trapped easily by using any of the OnKeyPress / OnKeyDown... etc. If you wanted to go down this route, you'll need to take each keypress / down as aan individual character, whereas your barcode.OnRead might do all that for you. (Again I havent used the EMDK you reference).
Lastly 'usualy' barcode scanners end with a cr (carriage return) some barcode scanners can turn this off, or change these in settings. If not this might be where the e.handled referenced elsewhere is talking about. This would be something like ..
if e.KeyChar = Chr(Keys.Enter) then
e.handled = true
end if
This would stop the send going to the object (textbox) and therfore not losing focus (as the enter key wasn't passed)
Hope this helps.. a bit :)
Chicken

Is there a way to override which control a keystroke is sent to?

For the server version of my chat app I have two controls in the form: an Input TextBox to type in for chatting and a ClientList CheckedListBox where I can select connected clients in order to do various actions to them (such as muting, kicking, etc).
Here are the Handlers for the events associated with these controls:
Private Sub Input_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Input.KeyDown
If e.KeyCode = Keys.Escape Then
e.SuppressKeyPress = True
End If
If e.KeyCode = Keys.Enter Then
Send.PerformClick()
e.SuppressKeyPress = True
End If
End Sub
Private Sub ClientList_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ClientList.KeyPress
If e.KeyChar <> " " Then
Input.Select()
SendKeys.Send(e.KeyChar)
End If
End Sub
(Note that Send is a button which sends the input to the chat)
While ClientList has focus: As you can see in ClientList_KeyPress, I want any KeyPresses which aren't related to ClientList to be sent to Input. (Only the spacebar and arrow keys are needed to navigate and select items in ClientList)
Although the above code works as desired, I feel that it is an inelegant way of doing this. Using SendKeys doesn't delegate the KeyPress — it just simply sends another keystroke to the application and it feels like poor coding. If the code is changed slightly so that it is more complex and runs async, then there could be the possibility of endless keystrokes being sent if Input.Select fails for whatever reason or ClientList ends up being focused again.
So I would like a suggestion which intercepts the KeyStroke before it is sent to the focused control or a suggestion which triggers the Input.KeyPress event in ClientList_KeyPress (ie. delegation).
I haven't been able to find anything from my own research — the above code using SendKeys is all I can find.

Determine which textbox on winform has cursor in it

I am working on a Windows Form via VB.net. The form contains two textboxes and an onscreen keyboard that I have programmed myself.
How can I determine which textbox the cursor is currently in, so that the onscreen keyboard will type in the correct textbox?
The application is meant to be entirely touch based.
I'm going to guess that your onscreen keyboard is a series a buttons representing a keyboard.
Since you have two text boxes, you have to have a flag for both text boxes to indicate that they have focus, and when one has it the other doesn't.
Private Sub TextBox1_GotFocus(sender as Object, e as EventArgs) _
Handles TextBox1.GotFocus
// These flags are class level variables
textbox1HasFocus = true;
textbox2HasFocus = false;
End Sub
Private Sub TextBox2_GotFocus(sender as Object, e as EventArgs) _
Handles TextBox2.GotFocus
// These flags are class level variables
textbox2HasFocus = true;
textbox1HasFocus = false;
End Sub
For your keyboard events
Private Sub KeyBoard_Click(sender As Object, e As EventArgs) Handles ButtonA.Click, ButtonB.Click, ButtonC.Click, etc...
' I don't know how your using your keyboard, but buttonClickLetter could be determined in multiple ways. I would use either the Name of the control or even the Tag property for retrieving which letter to use
String buttonClickLetter = DirectCast(sender, Button).Tag.ToString()
If textbox1HasFocus Then
TextBox1.Text += buttonClickLetter
ElseIf textbox2HadFocus
TextBox2.Text += buttonClickLetter
End If
End Sub

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

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