AeonOfTime
AeonOfTime

Reputation: 1006

Show autocomplete list after calling TextBox.AppendText()

I have a series of buttons in a winforms dialog (on .net4.5) that append snippets of text to a textbox called FieldDescription, like this:

private void SnippetButton_Click(object sender, EventArgs e)
{
    var btn = sender as Button;
    FieldDescription.AppendText(btn.Text);
    FieldDescription.Focus();
}

The textbox has an autocomplete source, and I would like the autocomplete to open after appending the text. The idea is to allow users to easily pre-fill the textbox with the beginning characters of the most used texts. In many cases this means they just have to select an entry from the autocomplete list after clicking the button, without having to use the keyboard.

Is there a way to trigger the autocomplete window after appending the text programmatically like this?

Upvotes: 2

Views: 305

Answers (1)

NibblyPig
NibblyPig

Reputation: 52932

Surprisingly I couldn't find a way to do this nicely.

If you can't find a way within .NET you can do it via a p/invoke.

[DllImport("user32.dll", CharSetCharSet = CharSet.Auto, SetLastError = false)]  
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);  
private const int CB_SHOWDROPDOWN = 0x014F;  

SendMessage(FieldDescription.Handle, CB_SHOWDROPDOWN, (IntPtr)1, (IntPtr)0);

You can put it into the OnFocus event.

Upvotes: 1

Related Questions