开发者

Visual Studio AutoComplete simulates enter being pressed when selecting

开发者 https://www.devze.com 2022-12-13 07:00 出处:网络
Visual studio nicely providesbuilt-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

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开发者_如何学C

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消