Combobox and Restrict Entry

Not sure I can do this, if so I can’t see how.

I have a combobox with an associated list. What I want to do is ensure that the user can only select / enter a value from the list, restricting what he can type into the edit box of the list.

Can anyone tell me how I do this?

Thanks

Phil

Just off the top of my head here:

In the KeyDown event you can see what the current text is and if it matches anything in the list let them keep typing. As soon as there’s no match return true which I think will show the beep.

If you want autocomplete you could set the text when there is one and only one match from the list.

that’s what a PopupMenu is for.

ComboBox is for when a user can add new entries that are not in the list.

How then does one create a PopupMenu with autocomplete?

That question makes absolutely no sense.

I believe there are two approaches : “à la Xojo” and “à la Google”.

A la Xojo where the first possible termination appears as grey right after the caret is possible with a canvas laid over the TextField showing the grey text.

All you need is this kind of code in the paint event displayed if the entered text matches the first part of the probable complete :

Sub Paint(g As Graphics, areas() As REALbasic.Rect) g.ForeColor = &cB3B3B300 g.textsize = 0 g.textfont = "System" g.DrawString("tsointsoin", 48,16) End Sub

Now the tricky part is to use TextWidth to see where the entered text ends, so you can display the grey proposition. In the example above I did it “hard coded” just for demo purposes.

The à la Google way requires using a listbox underneath to display the different propositions, essentially the way the drop down menu would. But since I found no way to drop the list by code, that is the way I would do it.

That said, I agree with Markus. It seems a bit futile not to use simply a PopupMenu.

For a user interface the Combobox works best with a large number of items as the user can start to type in and use auto complete to find what they want. With a PopupMenu a long list makes the UI not so easy. Agree for a shortlist (like a menu) then this control works fine.

Phil

[quote=182789:@Phil Corley]For a user interface the Combobox works best with a large number of items as the user can start to type in and use auto complete to find what they want. With a PopupMenu a long list makes the UI not so easy. Agree for a shortlist (like a menu) then this control works fine.

Phil[/quote]

I see. Sort of what I called the Google way. In that case, you may not even want a Combobox. As far as I know, there is no way to have the menu drop to present available selections.

You could instead use a simple textfield, and show a ListBox, or a ContextualMenu when the selection has become short enough to pick an item.

The tip I posted still remains valid to display the first possibility, though.

You could use the keydown to test if the existing text + the new key would make for the beginning of one of the items in the list.
if it does allow the key
if not reject it

Actually, it makes perfect sense - to me at least :stuck_out_tongue:

What the OP, and others including me, are wanting is a control which allows the user to enter keystrokes and take advantage of autocomplete (à la ComboBox) but without the user being able to enter anything that’s not already in the list.

Your response [quote]that’s what a PopupMenu is for.[/quote] is what’ seems nonsensical to me…

“A PopupMenu with autocomplete” is non-sensical.

A ComboBox with autocomplete is possible.

A TextField with autocomplete and a custom menu control is possible.

Btw is ComboBox.Autocomplete not working?

http://documentation.xojo.com/index.php/ComboBox.AutoComplete

[quote=182990:@Markus Winter]Btw is ComboBox.Autocomplete not working?

http://documentation.xojo.com/index.php/ComboBox.AutoComplete[/quote]

This works, but does not restrict the user from filling in data that is not in the list.

[quote=182921:@Peter Truskier]Actually, it makes perfect sense - to me at least :stuck_out_tongue:

What the OP, and others including me, are wanting is a control which allows the user to enter keystrokes and take advantage of autocomplete (à la ComboBox) but without the user being able to enter anything that’s not already in the list.

Your response is what’ seems nonsensical to me…[/quote]

It still seems a bit nonsense to me, and it will peeve users off (who are used to popup menus giving a limited choice and ComboBox allowing entry of new data too), but just use a ComboBox and on loosing focus check that the entry is indeed in the list.

Nothing will (be it a ComboBox or a TextField). That’s up to you. You will always need to check the entry against your list.

Assume you only have two entries with A: Anna and Anne

What stops the user to just type An and then click somewhere else? What do you do now?

[quote=309593:@Markus Winter]Nothing will (be it a ComboBox or a TextField). That’s up to you. You will always need to check the entry against your list.

Assume you only have two entries with A: Anna and Anne

What stops the user to just type An and then click somewhere else? What do you do now?[/quote]

Handle it with KeyDown event. I’m a subclass right now. But I have to go now. Will work on it this evening. I will share it with anyone who wants it.

I doubt that will work. You’ll see.

(I know this thread is old, but it got bumped - I’m just offering User Experience tips)

When a user pops open a PopupMenu they can start typing to select an item.

That is not the point of a ComboBox. Use a PopupMenu.

Overall (not directed at anybody in particular)
The point of a ComboBox is to offer a combination of TextField and PopupMenu. It allows you as a developer to provide a list of common examples that could save the user time, but allows them to fill in a value you might not have accounted for. By not allowing them to enter their own value, you’re tossing out the purpose of the control.

For example, you might see a ComboBox on a form for an Operating System field. It could start with Mac and Windows, but would allow a Linux user to type in their system. This is a super simplified example. We of course would know to add Linux to the list, and it may seem trivial to select from 1 of 3 options - but you have other UI issues if your PopupMenu is so long it can’t be navigated.

I have to agree that using a combobox that way is, IMHO, perverting entirely the notion.

Just as saying “you can use Textfield, but don’t type anything but a specific list of words”…

Well… It is not my software…