views:

283

answers:

1

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

+1  A: 

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.

0A0D
The problem is that I do not want it sending to the COM port at all. It is sent to the COM port when the enter key is pressed inside the text box. It's not simply attaching a CR LF to the string, it's simulating the enter key being pressed.
bobert5064
Can you post some code so I can try it here?
0A0D
I have posted some code as well as edited my question, I hope that will give you more to go off.
bobert5064
I have your code running - so do you want enter to be sent when hit via keyboard or do you only want it sent when using a mouse?
0A0D
I ONLY want enter to be send when the user physically hits the enter key.
bobert5064
Thanks for you help. I agree, there is most likely no way to do this without rolling my own. So, that's what I will do, that CodeProject article looks really good so I will be looking into that. Thanks again.
bobert5064