Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting handle of Autocomplete dropdown box of textbox in winforms

I wanted to adjust the width of the Autocomplete dropdown box of a textbox. I dont want to adjust the width of that textbox, but only Autocomplete dropdown. I know that there is no way I can increase the width of the Autocomplete dropdown by using properties provided with textbox.

Hence I wanted to know whether there is any way to get the handle of that Autocomplete box and then increase the width of that drop downlist without changing the textbox width?

If this is not possible then I would like to create my custom textbox with autocomplete, in this case how to use the existing autocomplete functionality provided by microsoft? Is there any way to do it. Are there any libraries available for this?

like image 337
JPReddy Avatar asked Feb 25 '23 23:02

JPReddy


2 Answers

I don't think you can use Microsoft's implementation of autocomplete, which does not have an option to adjust dropdown width.

Create a background thread to not get into the way of typing, and hook up the text change event of a combobox box or a textbox to update the candidate list (assuming autosuggest mode since you mention a dropdown). You can probably add/remove the combobox items on the fly if you have a combobox. But for dropdown list and textbox items you need a popup window

It is easy to get a popup to show, but you need to not use a fixed position so it won't go off the screen when the textbox is close to the edge of the screen. And the focus logic is a little bit complex. you need to keep focus on the textbox unless the user press the arrow keys to make a selection.

so when focus is on textbox: arrow keys move the focus to the popup other keys goes to the textbox, if not handled by the dialog itself, except for the delete key when mouse is over the popup. when focus is on popup: arrow keys move the focus to the sibling candidate item or the textbox other keys goes to the textbox, if not handled by the dialog itself, except for the delete key

mouse clicks: dismiss the popup outside of the popup or the popup. update the value of textbox if a candidate item in the popup is clicked on

It takes a lot of effort to get the focus/threading right. If you can afford some form space, you can just add a fixed width listbox to the form instead, like Visual Studio help viewer's index pane.

like image 103
Sheng Jiang 蒋晟 Avatar answered Feb 28 '23 13:02

Sheng Jiang 蒋晟


After going through lot of blog posts and different articles, I came to a consensus that it is next to impossible to get a solution to my problem in the way I wanted. So I've decided either to come up with a custom solution or as Sheng Jiang said I need to implement my own autocomplete object.

I've come up with a solution which fits my requirement by increasing the width of the textbox as per the largest string in the autocomplete string list while I'm adding the autocomplete custom source. As I said I cant increase the width of the text box because of the size constraint on the form, so I decided to keep this textbox in a panel and increase the size of the textbox inside that. Panel will not grow with the textbox so that solved my problem.

I know this is not perfect solution but it fits my requirement.

like image 37
JPReddy Avatar answered Feb 28 '23 12:02

JPReddy