Link to home
Start Free TrialLog in
Avatar of Matthew Clark
Matthew ClarkFlag for United States of America

asked on

TextBox with AutoComplete enabled clears itself and keeps focus when pressing [Enter]

I've successfully implemented AutoCompleteCustomSource on a TextBox control (single line, no drop-down) , which is saved in My.Settings.  Also on this TextBox, I have a KeyPress event which, when triggered by the [Enter] key, a .PerformClick() is called on a button.

However, as long as the AutoCompleteMode and AutoCompleteSource are set, then when the [Enter] key is pressed, the TextBox is cleared and the KeyPress event never fires.  If I remove the AutoComplete features (set them to None), the TextBox behaves as expected and the KeyPress event fires correctly.

Why is this happening?
Avatar of ROMA CHAUHAN
ROMA CHAUHAN
Flag of India image

You can handle the enter event in Textbox_Keydown event.
Use the KeyUp event instead of KeyPress
http://www.xtremevbtalk.com/showthread.php?t=301468
Avatar of Matthew Clark

ASKER

They KeyUp event does fire, which is good, however the TextBox still clears before the event is fired.  Why is the TextBox clearing out?
That is down to the implementation of the autocomplete feature i guess. Does it clear even when there are no autocomplete suggestions?
@CodeCruiser: yes, even if there are no suggestions the TextBox clear itself.  It's very strange, and it only happens when the AutoCompleteMode and AutoCompleteSource are set.
Changing the event from KeyPress to KeyUp got the [Enter] key functionality I was looking for, but I just can't explain why the TextBox is clearing itself.  I've looked all through the documentation I could find on AutoComplete, and no one else has mentioned this problem.
Do you have any other events of the textbox handled or any other code related to this textbox?
This is a fairly simple, single-form application designed to serve a very simple purpose.  There are no other events on the TextBox.  As I mentioned, if I set the AutoCompleteMode and AutoCompleteSource to "None", everything works perfectly.  As soon as I set those, the problem returns.
I'm starting to think that I ought to just not use AutoComplete and create my own logic that populates a drop-down.  But I don't like to give up, and I'll be damned if a software project is gonna beat me!
Well, crap.  I decided to whip out a brand new Windows Forms Application with just a TextBox, Button, and the accompanying AutoComplte features.  It worked flawlessly.
Below is the code I wrote, which is basically the same as my real application, just simplified.  I'll keep looking at what I have...

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TextBox1.AutoCompleteCustomSource = My.Settings.TextBox_AC
    End Sub
 
    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        My.Settings.TextBox_AC = Me.TextBox1.AutoCompleteCustomSource
    End Sub
 
    Private Sub TextBox_Enter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
        If e.KeyCode = 13 Then
            Button1.PerformClick()
            e.Handled = True
        End If
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not Me.TextBox1.AutoCompleteCustomSource.Contains(Me.TextBox1.Text) Then
            Me.TextBox1.AutoCompleteCustomSource.Add(Me.TextBox1.Text)
        End If
        MsgBox("Hello, World!" & vbNewLine & vbNewLine & Me.TextBox1.Text)
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Matthew Clark
Matthew Clark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial