Share via


Textbox autocomplete, chr(13), Enter key, carrage return

Hi everyone :-)
This is a simple solution to a simple problem that many of us come across.
When we are developing applications such as a simple webbrowser, we often find problems with the textbox that we enter the web address into.
For example when you enter the webaddress for say Microsoft.com or aol.com you want to save that so at a later date you can select it from the list that is displayed once the autocomplete property's of the textbox is set enabled and initialized.

Like most other people i found that the keydown and keyup events for a textbox where nunified if the AutoComplete properties were set to anything other than default.
Eventually after hours of testing i was able to find a work-around solution that fixed the problem for me.

Obviously if you are developing a webbrowser and you want to save a list of places you been and not have to retype the entire path to that website then using the textbox's autocomplete properties is the solution. So what i found at the end of the day was that i could use the textbox's. PREVIEKEYDOWN event as a substitute and specifically. If the End-User pressed the carage-return or Enter Key, then it was easy to capture that key and forward the outcome to the relevant procedure. As in my case i called a Button_Click event.

Visual Basic:
Private Sub txt_URLAdress_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles txt_URLAdress.PreviewKeyDown
        If e.KeyCode = Keys.Enter Then Call btn_Navigate_Click(sender, e)
End Sub

C#:
private void txt_URLAdress_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    if (e.KeyCode ==  Keys.Enter && e.Modifiers == Keys.None)
        btn_Navigate_Click(sender, e);
}

This made it possible for me to enter a URL into my textbox which had autoComplete properties enabled and set. And then once pressing then Enter key enable my webbrowser to navigate to the URL in the textbox.

And they all lived happy ever after