tags:

views:

24

answers:

1

Simple code C# winform app (visual studio 2010):

A simple form with one text box here is a keyPress event:

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            // The keypressed method uses the KeyChar property to check 
            // whether the ENTER key is pressed. 


            if (e.KeyChar == (char)Keys.Return)
            {
                Process.Start("http://yahoo.com", null);
            }

        }

Works fine if I type in some text in the text box and hit enter, it opens up my default web browser and takes me to a site.

I need to change the text box to autocompletemode = suggestappend and autocompletesource = customsource. And then I fill it like so:

  private void Form1_Load(object sender, EventArgs e)
        {
            AutoCompleteStringCollection s = new AutoCompleteStringCollection();
            s.Add("Jon ");
            s.Add("2 Jon");
            textBox1.AutoCompleteCustomSource = s;
        }

It does the autocomplete as right when I type in a J it comes up with Jon, but now when I hit enter it doesnt fireoff the KeyPress event. It just sits there. I tried putting in a break point and when I type in the first letter J it fires the KeyPress event. Then when i select the string "Jon" from the autocomplete source and then I hit enter the event no longer fires.

Confused :).

+1  A: 

It would seem the Return key press is being handled because of the auto-complete suggestion drop-down list - you could always use the KeyDown event which still fires in this scenario.

AaronY
Agreed, this should be handled at KeyDown.
Hans Passant